You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Example 1:
1 2 3 4 5
Input: n = 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps
Example 2:
1 2 3 4 5 6
Input: n = 3 Output: 3 Explanation: There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step
动态规划:
时间复杂度:O(n)
空间复杂度:O(1)
1 2 3 4 5 6 7 8 9
intclimbStairs(int n){ int p = 0, q = 0, r = 1; for (int i = 1; i <= n; ++i) { p = q; q = r; r = p + q;//爬到第r阶楼梯可能用1步或2步故第r阶楼梯等于第r-1阶加第r-2阶 } return r; }
intmySqrt(int x){ int l = 0, r = x, ans = -1; while (l <= r) { int mid = l + (r - l) / 2; if ((longlong)mid * mid <= x) { ans = mid; l = mid + 1; } else { r = mid - 1; } } return ans; }
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0‘s.
Increment the large integer by one and return the resulting array of digits.
Example 1:
1 2 3 4 5
Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. Thus, the result should be [1,2,4].
Example 2:
1 2 3 4 5
Input: digits = [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Incrementing by one gives 4321 + 1 = 4322. Thus, the result should be [4,3,2,2].
Example 3:
1 2 3 4 5
Input: digits = [9] Output: [1,0] Explanation: The array represents the integer 9. Incrementing by one gives 9 + 1 = 10. Thus, the result should be [1,0].
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
1 2
Input: nums = [1,3,5,6], target = 5 Output: 2
Example 2:
1 2
Input: nums = [1,3,5,6], target = 2 Output: 1
Example 3:
1 2
Input: nums = [1,3,5,6], target = 7 Output: 4
个人解答:
时间复杂度O(logn);
空间复杂度O(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
intsearchInsert(vector<int>& nums, int target){ int n = nums.size(); int left = 0, right = n - 1, ans = n; while (left <= right) { int mid = left + ((right - left) / 2); if (target <= nums[mid]) { ans = mid; right = mid - 1; } else { left = mid + 1; } } return ans; }
Given an integer array nums and an integer val, remove all occurrences of val in numsin-place. The relative order of the elements may be changed.
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
Return kafter placing the final result in the firstkslots ofnums.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
1 2 3 4
Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2,_,_] Explanation: Your function should return k = 2, with the first two elements of nums being 2. It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
1 2 3 4 5
Input: nums = [0,1,2,2,3,0,4,2], val = 2 Output: 5, nums = [0,1,4,0,3,_,_,_] Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4. Note that the five elements can be returned in any order. It does not matter what you leave beyond the returned k (hence they are underscores).
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.
return kafter placing the final result in the firstkslots ofnums.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
1 2 3 4
Input: nums = [1,1,2] Output: 2, nums = [1,2,_] Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
1 2 3 4
Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4,_,_,_,_,_] Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does not matter what you leave beyond the returned k (hence they are underscores).
intremoveDuplicates(vector<int>& nums){ int nums_size = nums.size(); if (nums_size == 1)return1; int left = 0; int right = 1; while (right < nums_size) { if (nums[right] != nums[left]) { if (right - left > 1) {//right和left相邻不用复制 nums[left + 1] = nums[right]; } left++; } right++; } return left+1; }
1 2 3 4 5 6 7 8 9 10
intremoveDuplicates(vector<int> arr){ if (arr.empty()) return0; int index = 0; for (int val : arr) { if (val != arr[index]) { arr[++index] = val; } } return index + 1; }