2023每日刷题(四十八)
Leetcode—409.最长回文串
强烈吐槽!!!
非常不理解,同样的代码,为什么C跑不了C++就跑得了,力扣编译器是对C语言有歧视吗???看之前我发的博客,就出现了这种问题,调试出来结果是对的,但是运行出来却不对。。。
C实现代码
int arr[26] = {0};
int arr2[26] = {0};int calculate(int a, int A) {int sum = 0;int odd = 0;if(a) {for(int i = 0; i < 26; i++) {if(arr[i] % 2 != 0) {odd = 1;}sum += (arr[i] / 2) * 2;}}if(A) {for(int i = 0; i < 26; i++) {if(arr2[i] % 2 != 0) {odd = 1;}sum += (arr2[i] / 2) * 2;}}if(odd) {sum += 1;}return sum;
}int longestPalindrome(char * s){int n = strlen(s);int flaga = 0, flagA = 0;for(int i = 0; i < n; i++) {if(s[i] >= 'a' && s[i] <= 'z') {int idx = s[i] - 'a';flaga = 1;arr[idx] += 1;} else {int idx = s[i] - 'A';flagA = 1;arr2[idx] += 1;}}int res = calculate(flaga, flagA);return res;
}
调试结果明明是对的,res输出1
C++实现代码
class Solution {
public:int arr[26] = {0};int arr2[26] = {0};int calculate(int a, int A) {int sum = 0;int odd = 0;if(a) {for(int i = 0; i < 26; i++) {if(arr[i] % 2 != 0) {odd = 1;}sum += (arr[i] / 2) * 2;}}if(A) {for(int i = 0; i < 26; i++) {if(arr2[i] % 2 != 0) {odd = 1;}sum += (arr2[i] / 2) * 2;}}if(odd) {sum += 1;}return sum;}int longestPalindrome(string s) {int n = s.size();int flaga = 0, flagA = 0;for(int i = 0; i < n; i++) {if(s[i] >= 'a' && s[i] <= 'z') {int idx = s[i] - 'a';flaga = 1;arr[idx] += 1;} else {int idx = s[i] - 'A';flagA = 1;arr2[idx] += 1;}}int res = calculate(flaga, flagA);return res;}
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!