Given an integer array nums, handle multiple queries of the following type:
Calculate the sum of the elements of nums between indices left and rightinclusive where left <= right.
Implement the NumArray class:
NumArray(int[] nums) Initializes the object with the integer array nums.
int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and rightinclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).
You are playing the following Nim Game with your friend:
Initially, there is a heap of stones on the table.
You and your friend will alternate taking turns, and you go first.
On each turn, the person whose turn it is will remove 1 to 3 stones from the heap.
The one who removes the last stone is the winner.
Given n, the number of stones in the heap, return trueif you can win the game assuming both you and your friend play optimally, otherwise returnfalse.
Example 1:
1 2 3 4 5 6 7
Input: n = 4 Output: false Explanation: These are the possible outcomes: 1. You remove 1 stone. Your friend removes 3 stones, including the last stone. Your friend wins. 2. You remove 2 stones. Your friend removes 2 stones, including the last stone. Your friend wins. 3. You remove 3 stones. Your friend removes the last stone. Your friend wins. In all outcomes, your friend wins.
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Example 1:
1 2 3 4 5 6 7
Input: n = 5, bad = 4 Output: 4 Explanation: call isBadVersion(3) -> false call isBadVersion(5) -> true call isBadVersion(4) -> true Then 4 is the first bad version.
Example 2:
1 2
Input: n = 1, bad = 1 Output: 1
二分法:
时间复杂度:O(logn)
空间复杂度:O(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// The API isBadVersion is defined for you. // bool isBadVersion(int version);
classSolution { public: intfirstBadVersion(int n){ int left = 1, right = n; while (left < right) { int mid = left + (right - left) / 2; if (isBadVersion(mid)) { right = mid; } else { left = mid + 1; } } return left; } };
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
Example 1:
1 2 3
Input: nums = [3,0,1] Output: 2 Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing number in the range since it does not appear in nums.
Example 2:
1 2 3
Input: nums = [0,1] Output: 2 Explanation: n = 2 since there are 2 numbers, so all numbers are in the range [0,2]. 2 is the missing number in the range since it does not appear in nums.
Example 3:
1 2 3
Input: nums = [9,6,4,2,3,5,7,0,1] Output: 8 Explanation: n = 9 since there are 9 numbers, so all numbers are in the range [0,9]. 8 is the missing number in the range since it does not appear in nums.
排序法:
时间复杂度:O(nlogn)
空间复杂度:O(1)
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: intmissingNumber(vector<int>& nums){ int nums_size = nums.size(); sort(nums.begin(), nums.end()); for (int i = 0; i < nums_size-1; i++) { if (nums[i] != nums[i + 1] - 1)return nums[i] + 1; } if (nums[0] != 0)return0; elsereturn nums_size; } };
哈希表:
时间复杂度:O(n)
空间复杂度:O(n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: intmissingNumber(vector<int>& nums){ unordered_set<int> hash; int nums_size = nums.size(); for (int i = 0; i < nums_size; i++) { hash.insert(nums[i]); } int missing = -1; for (int i = 0; i <= nums_size; i++) { if (!hash.count(i)) { missing = i; break; } } return missing; } };
数学:
时间复杂度:O(n)
空间复杂度:O(1)
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { public: intmissingNumber(vector<int>& nums){ int n = nums.size(); int total = n * (n + 1) / 2; int arrSum = 0; for (int i = 0; i < n; i++) { arrSum += nums[i]; } return total - arrSum; } };
位运算:
时间复杂度:O(n)
空间复杂度:O(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: intmissingNumber(vector<int>& nums){ int res = 0; int n = nums.size(); for (int i = 0; i < n; i++) { res ^= nums[i]; } for (int i = 0; i <= n; i++) { res ^= i; } return res; } };