Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range[1, n]that do not appear innums.
Given two non-negative integers, num1 and num2 represented as string, return the sum ofnum1andnum2as a string.
You must solve the problem without using any built-in library for handling large integers (such as BigInteger). You must also not convert the inputs to integers directly.
Example 1:
1 2
Input: num1 = "11", num2 = "123" Output: "134"
Example 2:
1 2
Input: num1 = "456", num2 = "77" Output: "533"
Example 3:
1 2
Input: num1 = "0", num2 = "0" Output: "0"
时间复杂度:O(max(len1,len2)
空间复杂度:O(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: string addStrings(string num1, string num2){ int i = num1.length() - 1, j = num2.length() - 1, add = 0; string ans = ""; while (i >= 0 || j >= 0 || add != 0) { int x = i >= 0 ? num1[i] - '0' : 0; int y = j >= 0 ? num2[j] - '0' : 0; int result = x + y + add; ans.push_back('0' + result % 10); add = result / 10; i -= 1; j -= 1; } reverse(ans.begin(), ans.end()); return ans; } };
Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.
Example 1:
1 2 3 4 5 6
Input: nums = [3,2,1] Output: 1 Explanation: The first distinct maximum is 3. The second distinct maximum is 2. The third distinct maximum is 1.
Example 2:
1 2 3 4 5 6
Input: nums = [1,2] Output: 2 Explanation: The first distinct maximum is 2. The second distinct maximum is 1. The third distinct maximum does not exist, so the maximum (2) is returned instead.
Example 3:
1 2 3 4 5 6
Input: nums = [2,2,3,1] Output: 1 Explanation: The first distinct maximum is 3. The second distinct maximum is 2 (both 2's are counted together since they have the same value). The third distinct maximum is 1.
排序:
时间复杂度:O(nlogn)
空间复杂度:O(logn)
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: intthirdMax(vector<int> &nums){ sort(nums.begin(), nums.end(), greater<>()); for (int i = 1, diff = 1; i < nums.size(); ++i) { if (nums[i] != nums[i - 1] && ++diff == 3) { return nums[i]; } } return nums[0]; } };
classSolution { public: intthirdMax(vector<int> &nums){ long a = LONG_MIN, b = LONG_MIN, c = LONG_MIN; for (long num : nums) { if (num > a) { c = b; b = a; a = num; } elseif (a > num && num > b) { c = b; b = num; } elseif (b > num && num > c) { c = num; } } return c == LONG_MIN ? a : c; } };
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){ int *a = nullptr, *b = nullptr, *c = nullptr; for (int &num : nums) { if (a == nullptr || num > *a) { c = b; b = a; a = # } elseif (*a > num && (b == nullptr || num > *b)) { c = b; b = # } elseif (b != nullptr && *b > num && (c == nullptr || num > *c)) { c = # } } return c == nullptr ? *a : *c; } };
Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.
Letters are case sensitive, for example, "Aa" is not considered a palindrome here.
Example 1:
1 2 3
Input: s = "abccccdd" Output: 7 Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
Example 2:
1 2 3
Input: s = "a" Output: 1 Explanation: The longest palindrome that can be built is "a", whose length is 1.
贪心:
时间复杂度:O(n)
空间复杂度:O(S),S为字符集大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intlongestPalindrome(string s){ unordered_map<char, int> count; int ans = 0; for (char c : s) ++count[c]; for (auto p : count) { int v = p.second; ans += v / 2 * 2; if (v % 2 == 1 && ans % 2 == 0) ++ans; } return ans; } };
Given an integer num, return a string representing its hexadecimal representation. For negative integers, two’s complement method is used.
All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer except for the zero itself.
Note: You are not allowed to use any built-in library method to directly solve this problem.
intdfs(TreeNode* node){ int ans = 0; if (node->left) { ans += isLeafNode(node->left) ? node->left->val : dfs(node->left); } if (node->right && !isLeafNode(node->right)) { ans += dfs(node->right); } return ans; }
A binary watch has 4 LEDs on the top to represent the hours (0-11), and 6 LEDs on the bottom to represent the minutes (0-59). Each LED represents a zero or one, with the least significant bit on the right.
For example, the below binary watch reads "4:51".
Given an integer turnedOn which represents the number of LEDs that are currently on (ignoring the PM), return all possible times the watch could represent. You may return the answer in any order.
The hour must not contain a leading zero.
For example, "01:00" is not valid. It should be "1:00".
The minute must be consist of two digits and may contain a leading zero.
For example, "10:2" is not valid. It should be "10:02".