本文主要是介绍C语言 | Leetcode C语言题解之第67题二进制求和,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
void reserve(char* s) {int len = strlen(s);for (int i = 0; i < len / 2; i++) {char t = s[i];s[i] = s[len - i - 1], s[len - i - 1] = t;}
}char* addBinary(char* a, char* b) {reserve(a);reserve(b);int len_a = strlen(a), len_b = strlen(b);int n = fmax(len_a, len_b), carry = 0, len = 0;char* ans = (char*)malloc(sizeof(char) * (n + 2));for (int i = 0; i < n; ++i) {carry += i < len_a ? (a[i] == '1') : 0;carry += i < len_b ? (b[i] == '1') : 0;ans[len++] = carry % 2 + '0';carry /= 2;}if (carry) {ans[len++] = '1';}ans[len] = '\0';reserve(ans);return ans;
}
这篇关于C语言 | Leetcode C语言题解之第67题二进制求和的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!