public List<List<Integer>> threeSum(int[] num) { Arrays.sort(num); List<List<Integer>> res = new LinkedList<>(); for (int i = 0; i < num.length-2; i++) { if (i == 0 || (i > 0 && num[i] != num[i-1])) { int lo = i+1, hi = num.length-1, sum = 0 - num[i]; while (lo < hi) { if (num[lo] + num[hi] == sum) { res.add(Arrays.asList(num[i], num[lo], num[hi])); // Skip duplicate elements while (lo < hi && num[lo] == num[lo+1]) lo++; while (lo < hi && num[hi] == num[hi-1]) hi--; lo++; hi--; } elseif (num[lo] + num[hi] < sum) { lo++; } else { hi--; } } } } return res; }
intmajorityElement(vector<int>& nums){ int n = nums.size(); srand(unsigned(time(NULL))); while (true) { int idx = rand() % n; int candidate = nums[idx]; int counts = 0; for (int i = 0; i < n; i++) if (nums[i] == candidate) counts++; if (counts > n / 2) return candidate; } return-1; }
解法四:Moore Voting Algorithm
第一次做基本上很难想到这种解法,20ms,这个解法让我想起最大连续子列和的题目
1 2 3 4 5 6 7 8 9 10 11 12
intmajorityElement(vector<int>& nums){ int major, counts = 0, n = nums.size(); for (int i = 0; i < n; i++) { if (!counts) { major = nums[i]; counts = 1; } else { counts += (nums[i] == major) ? 1 : -1; } } return major; }
229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.
Note: The algorithm should run in linear time and in O(1) space.
classSolution { public: intfindTargetSumWays(constvector<int>& nums, int target){ int sum = 0; if (nums.empty()) return0; for (int val: nums) { sum += val; } if (sum < target || ((sum + target) & 1)) return0; else return findSubSet(nums, (sum + target) >> 1); } intfindSubSet(constvector<int>& nums, int target){ vector<vector<int>> dp(nums.size(), vector<int>(target + 1, 0)); dp[0][0] = 1; if (nums[0] <= target) dp[0][nums[0]] += 1; for (int i=1; i<nums.size(); i++) { for (int j=0; j<=target; j++) { dp[i][j] = dp[i - 1][j]; if (j - nums[i] >= 0) dp[i][j] += dp[i - 1][j - nums[i]]; } } return dp.back()[target]; } };
答案区里更简洁的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intfindTargetSumWays(vector<int>& nums, int s){ int sum = accumulate(nums.begin(), nums.end(), 0); return sum < s || (s + sum) & 1 ? 0 : subsetSum(nums, (s + sum) >> 1); }
intsubsetSum(vector<int>& nums, int s){ int dp[s + 1] = { 0 }; dp[0] = 1; for (int n : nums) for (int i = s; i >= n; i--) dp[i] += dp[i - n]; return dp[s]; } };
653. Two Sum IV - Input is a BST
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.
intcombinationSum4(vector<int>& nums, int target){ if (nums.empty()) return0; sort(nums.begin(), nums.end()); vector<int> dp(target + 1, 0); dp[0] = 1; for (int i=1; i<=target; i++) { for (int val: nums) { if (val > i) break; else dp[i] += dp[i - val]; } } return dp[target]; }
769. Max Chunks To Make Sorted
Given an array arr that is a permutation of [0, 1, …, arr.length - 1], we split the array into some number of “chunks” (partitions), and individually sort each chunk. After concatenating them, the result equals the sorted array.
What is the most number of chunks we could have made?
具体做法就是遍历数组中的每个数字,keep track of the maximum number。由于数组是从0到N-1的permutation,如果到下标i时最大值等于i,则说明之前的所有数字
1 2 3 4 5 6 7 8 9 10
intmaxChunksToSorted(vector<int>& arr){ int cnt = 0, mval = 0x80000000; for (int i=0; i<arr.size(); i++) { if (arr[i] > mval) mval = arr[i]; if (i == mval) cnt ++; } return cnt; }
768. Max Chunks To Make Sorted II
This question is the same as “Max Chunks to Make Sorted” except the integers of the given array are not necessarily distinct, the input array could be up to length 2000, and the elements could be up to 10**8.
看到这个答案无话可说,实实在在的智商差距
1 2 3 4 5 6 7 8 9 10 11 12
intmaxChunksToSorted(vector<int>& arr){ vector<int> arr_cp(arr.begin(), arr.end()); sort(arr_cp.begin(), arr_cp.end()); longlong ans = 0, sum1 = 0, sum2 = 0; for (int i=0; i<arr.size(); i++) { sum1 += arr[i]; sum2 += arr_cp[i]; if (sum1 == sum2) ans ++; } return ans; }