string addBinary(string a, string b) { int alen = a.size(); int blen = b.size(); ) return b; ) return a; ; ; ; string res; || j >= || carry > ) { ) carry += a[i--] - '; ) carry += b[j--] - '; res = ( + ') + res; //res不用逆序处理了 carry = carry / ; } retu…
string countAndSay(int n) { string res; ) return res; res = "; ) { int len = res.size(); int i, j; string resTmp; ; i < len; i = j) { char ch = res[i]; ; j < len; ++j) { if (ch != res[j]) break; } resTmp = resTmp + (') + ch; } res = resTmp; } r…
Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0. Example 1: Input: a = "11", b = "1" Output: "100" Example 2: Input: a = "1010&qu…
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 解题思路: 从两个子字符尾部遍历字符: 每次得到新的字符插入结果字符串的头部: 解题步骤: 1.建立返回string.从后向前遍历的idx:idx_a / idx_b.进位量2.循环开始,当idx_a或者idx_b任意不为0时,循环继…
66 - Plus One Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. Solution 1 : 十进制加法 class Solution { public: vector<int> plusO…
, INVALID}; int g_status; long long SubStrToInt(const char* str, bool minus) { ; : ; while (*str != '\0') { ') { num = num * + flag * (*str - '); if ((!minus && num > 0x7FFFFFFF) || (minus && num < (signed int)0x80000000)) { num = ;…
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 题解:简单的二进制加法模拟.a,b的最后以为对齐开始进行加法,用carries保存进位,如果加完后最高位还有进位,那么要在结果的最前面加一个1. 代码如下: public class Solution { public String…
解法一:非递归 vector<int> preorderTraversal(TreeNode* root) { vector<int> res; if (root == NULL) return res; stack<TreeNode*> node_stack; TreeNode *p = root; while (p || !node_stack.empty()) { if (p) { res.push_back(p->val); node_stack.push…
解法一:Brute-force int strStr(string haystack, string needle) { int m = haystack.size(); int n = needle.size(); if (!n) ; ; i < m - n + ; ++i) { ; while (j < n) { if (needle[j] == haystack[k]) { j++; k++; } else { break; } } if (j == n) return i; } ; }…
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/problems/add-strings/description/ 67 Add Binary: https://leetcode.com/problems/add-binary/description/ 43 Multiply Strings:https://leetcode.com/problems/…