// Forward declaration of guess API. // @param num, your guess // @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 intguess(int num);
classSolution { public: intguessNumber(int n){ int left = 1, right = n, mid = (n + 1)/2; while (left <= right) { mid = (left - right)/2 + right; switch (guess(mid)) { case-1 : right = mid - 1; break; case1 : left = mid + 1; break; case0 : return mid; } } return-1; } };
classSolution { public: intfirstUniqChar(string s){ int n[26] = {0}; for (char x : s) { n[x - 'a']++; } int len = s.length(); for (int i = 0; i < len; i++) { if (n[s[i] - 'a'] == 1) return i; } return-1; } };
classSolution { public: charfindTheDifference(string s, string t){ int m[26] = {0}, n[26] = {0}; for (char x : s) { m[x - 'a']++; } for (char x : t) { n[x - 'a']++; } for (char x : t) { if (m[x - 'a'] < n[x - 'a']) return x; } return-1; } };
AC代码
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: charfindTheDifference(string s, string t){ int len = s.length(); char c = t[0]; for (int i = 0; i < len; i++) { c ^= s[i]; c ^= t[i + 1];//t只比s多一个 } return c; } };
classSolution { public: intthirdMax(vector<int>& nums){ long i = LONG_MIN, j = LONG_MIN, k = LONG_MIN; for (int x : nums) { if (x > i) { i = x; } } for (int x : nums) { if (x > j && x != i) { j = x; } } for (int x : nums) { if (x > k && x != i && x != j) { k = x; } } if (k == LONG_MIN) return i; elsereturn k; } };
大佬思路
搜索一次找最大值
如果有最大值且大于最大的最大值,就把当前值先给了第二大值,第二大值给了第三大值
如果有最大值且小于最大的最大值大于第二大,往后顺延
如果有最大值且小于第二大的最大值大于第三大,往后顺延
大佬代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: intthirdMax(vector<int>& nums){ long first = LONG_MIN, second = LONG_MIN, third = LONG_MIN; for (int num : nums) { if (num > first) { third = second; second = first; first = num; } elseif (num > second && num < first) { third = second; second = num; } elseif (num > third && num < second) { third = num; } } return (third == LONG_MIN || third == second) ? first : third; } };
classSolution { public: string addStrings(string num1, string num2){ int carry = 0; int len1 = num1.length(); int len2 = num2.length(); int len = len1 > len2 ? len1 : len2; string zero; for (int i = 0; i < len - len1; i++) { zero += '0'; } num1.insert(0, zero); zero.clear(); for (int i = 0; i < len - len2; i++) { zero += '0'; } num2.insert(0, zero); for (int i = len - 1; i >= 0; i--) { int n = carry + num1[i] + num2[i] - 2*'0'; num1[i] = n % 10 + '0'; carry = n / 10; } if (carry) num1.insert(0, 1, carry + '0'); return num1; } };
classSolution { public: intnumberOfBoomerangs(vector<pair<int, int>>& points){ unordered_map<int ,int> m; int ans = 0; int len = points.size(); for (int i = 0; i < len; i++) { for (int j = 0; j < len; j++) { int dx = points[i].first - points[j].first; int dy = points[i].second - points[j].second; m[dx*dx + dy*dy]++; } for (auto c : m) { ans += c.second*(c.second-1); } m.clear(); } return ans; } };
classSolution { public: vector<int> findDisappearedNumbers(vector<int>& nums){ unordered_map<int, int> m; vector<int> ans; int len = nums.size(); for (int x : nums) { m[x]++; } for (int i = 1; i <= len; i++) { if (m[i] == 0) { ans.push_back(i); } } return ans; } };
大佬思路
i从0开始遍历数组,取nums[i]的绝对值Q(后期正数可能变负数)
把Q-1作为下标,把nums[Q-1]这个数编程负的(自己的绝对值的相反数)
最后正数出现的位置就是1~n没出现过的数
大佬代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: vector<int> findDisappearedNumbers(vector<int>& nums){ vector<int> ans; int len = nums.size(); for (int i = 0; i < len; i++) { nums[abs(nums[i]) - 1] = -abs(nums[abs(nums[i]) - 1]); } for (int i = 0; i < len; i++) { if (nums[i] > 0) { ans.push_back(i + 1); } } return ans; } };
classSolution { public: intfindContentChildren(vector<int>& g, vector<int>& s){ map<int, int> bit, chi; for (int x : g) { chi[x]++; } for (int x : s) { bit[x]++; } int ans = 0; auto i = chi.begin(), j = bit.begin(); for (; i != chi.end() && j != bit.end(); ) { if (i->first <= j->first && j->second > 0) { int a = i->second; int b = j->second; int min = a > b ? b : a; i->second -= min; j->second -= min; ans += min; if (i->second == 0) i++; } else { j++; } } return ans; } };
大佬思路
贪心 + 双指针
排序两个数组
其他思路和我的基本一样,但是人家的代码又简洁效率又高
大佬代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intfindContentChildren(vector<int>& g, vector<int>& s){ sort(g.begin(), g.end()); sort(s.begin(), s.end()); int ans = 0; int i = 0, j = 0; int len1 = g.size(), len2 = s.size(); for (; i < len1 && j < len2;j++) { if (g[i] <= s[j]) { i++; } } return i; } };
classSolution { public: intislandPerimeter(vector<vector<int>>& grid){ int ans = 0; int len = grid.size(); int wide = grid[0].size(); for(int i = 0; i < len; i++) { for (int j = 0; j < wide; j++) { if (grid[i][j]) { ans += 4; if (i + 1 < len && grid[i + 1][j]) { ans -= 2; } if (j + 1 < wide && grid[i][j + 1]) { ans -= 2; } } } } return ans; } };
classSolution { public: intfindComplement(int num){ int n = 0; longlong i = 1; while (num) { n += i * ((num % 2 + 1) % 2); i *= 2; num /= 2; } return n; } };
大佬思路
位运算,不懂
大佬代码
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: intfindComplement(int num){ int temp = num; int c = 0; while ( temp > 0 ) { temp >>= 1; c = ( c << 1 ) + 1; } return num ^ c; } };
classSolution { public: string licenseKeyFormatting(string S, int K){ string ans; int count = 0; int len = S.length(); for (int i = len; i >= 0; i--) { char t = S[i]; t = toupper(t); if (t != '-') { ans.push_back(t); if (count == K /*&& i != 0 && i != len*/) { ans.push_back('-'); count = 0; } count++; } } if (ans.back() == '-') ans.pop_back(); reverse(ans.begin(), ans.end()); if (ans.back() == '-') ans.pop_back(); return ans; } };
classSolution { public: intfindMaxConsecutiveOnes(vector<int>& nums){ int len = nums.size(); int count = 0, max = 0; for (int x : nums) { if (x) { count++; } else { //max = max > count ? max : count; if (max < count) max = count; count = 0; } } if (max < count) max = count; return max; } };
classSolution { public: boolcheckPerfectNumber(int num){ if (num <= 1) returnfalse; int ans = 1; for (int i = 2; i < sqrt(num); i++) { if (num % i == 0) { ans += i + num/i; } } return ans == num; } };