原题在此 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.
原题在此 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has enough space (size that is equal to m + n) to hold additional elements from nums2.
Example 1: Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: [1,2,2,3,5,6]
Example 2: Input: nums1 = [1], m = 1, nums2 = [], n = 0 Output: [1]
Constraints:
$0 <= n, m <= 200$
$1 <= n + m <= 200$
$nums_1.length == m + n$
$nums_2.length == n$
$-10^9 <= nums_1[i], nums_2[i] <= 10^9$
解析
根据题意, arr1足够大, 所以从arr1[m+n]开始倒着依次写入较大的数既可.
代码
C++
1 2 3 4 5 6
classSolution { public: voidmerge(vector<int>& a, int m, vector<int>& b, int n){ while(n) a[m + n] = (m > 0 && a[m - 1] > b[n - 1])? a[--m] : b[--n]; } };
Golang
1 2 3 4 5 6 7 8 9 10 11
funcmerge(nums1 []int, m int, nums2 []int, n int) { for n > 0 { if m == 0 || nums2[n-1] > nums1[m-1] { nums1[m+n-1] = nums2[n-1] n-- } else { nums1[m+n-1] = nums1[m-1] m-- } } }
原题在此 Given an integer array instructions, you are asked to create a sorted array from the elements in instructions. You start with an empty container nums. For each element from left to right in instructions, insert it into nums. The cost of each insertion is the minimum of the following:
The number of elements currently in nums that are strictly less than instructions[i]. The number of elements currently in nums that are strictly greater than instructions[i]. For example, if inserting element 3 into nums = [1,2,3,5], the cost of insertion is min(2, 1) (elements 1 and 2 are less than 3, element 5 is greater than 3) and nums will become [1,2,3,3,5].
Return the total cost to insert all elements from instructions into nums. Since the answer may be large, return it modulo 109 + 7
Example 1: Input: instructions = [1,5,6,2] Output: 1 Explanation: Begin with nums = []. Insert 1 with cost min(0, 0) = 0, now nums = [1]. Insert 5 with cost min(1, 0) = 0, now nums = [1,5]. Insert 6 with cost min(2, 0) = 0, now nums = [1,5,6]. Insert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6]. The total cost is 0 + 0 + 0 + 1 = 1.
Example 2: Input: instructions = [1,2,3,6,5,4] Output: 3 Explanation: Begin with nums = []. Insert 1 with cost min(0, 0) = 0, now nums = [1]. Insert 2 with cost min(1, 0) = 0, now nums = [1,2]. Insert 3 with cost min(2, 0) = 0, now nums = [1,2,3]. Insert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6]. Insert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6]. Insert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6]. The total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.
Example 3: Input: instructions = [1,3,3,3,2,4,2,1,2] Output: 4 Explanation: Begin with nums = []. Insert 1 with cost min(0, 0) = 0, now nums = [1]. Insert 3 with cost min(1, 0) = 0, now nums = [1,3]. Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3]. Insert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3]. Insert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3]. Insert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4]. Insert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4]. Insert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4]. Insert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4]. The total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.
#pragma clang optimize on #pragma GCC optimize ("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") staticint __ = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return0; }(); classBIT { public: BIT(int _size): size(_size), tree(_size + 1){} voidupdate(int x){ for(; x <= size; x += lowbit(x)) ++tree[x]; } intquery(int x){ int sum = 0; for(; x > 0; x -= lowbit(x)) sum += tree[x]; return sum; }
private: int size; vector<longlong> tree; staticconstexprintlowbit(int x){ return x & (-x); } };
classSolution { public: intcreateSortedArray(constvector<int>& ins){ staticconstint M = 1e9 + 7; BIT bit(*max_element(ins.begin(), ins.end())); longlong res = 0; for(int i = 0; i < static_cast<int>(ins.size()); ++i) { res += min(bit.query(ins[i] - 1), i - bit.query(ins[i])); bit.update(ins[i]); } return res % M; } };
原题在此 Given two words beginWord and endWord, and a dictionary wordList, return the length of the shortest transformation sequence from beginWord to endWord, such that: Only one letter can be changed at a time. Each transformed word must exist in the word list. Return 0 if there is no such transformation sequence.
Example 1: Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] Output: 5 Explanation: As one shortest transformation is “hit” -> “hot” -> “dot” -> “dog” -> “cog”, return its length 5.
Example 2: Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"] Output: 0 Explanation: The endWord “cog” is not in wordList, therefore no possible transformation.
Constraints:
1 <= beginWord.length <= 100
endWord.length == beginWord.length
1 <= wordList.length <= 5000
wordList[i].length == beginWord.length
beginWord, endWord, and wordList[i] consist of lowercase English letters.
func(i *iterator)next()byte { var result byte if !i.hasNext() { return result } result = i.words[i.currentWord][i.currentIndex] i.currentIndex++ return result }
原题在此 Given a string s, find the length of the longest substring without repeating characters.
Example 1: Input: s = “abcabcbb” Output: 3 Explanation: The answer is “abc”, with the length of 3.
Example 2: Input: s = “bbbbb” Output: 1 Explanation: The answer is “b”, with the length of 1.
Example 3: Input: s = “pwwkew” Output: 3 Explanation: The answer is “wke”, with the length of 3. Notice that the answer must be a substring, “pwke” is a subsequence and not a substring.
Example 4: Input: s = “” Output: 0
Constraints:
0 <= s.length <= 5 * 104
s consists of English letters, digits, symbols and spaces.
原题在此 Given an array arr of positive integers sorted in a strictly increasing order, and an integer k. Find the kth positive integer that is missing from this array.
Example 1: Input: arr = [2,3,4,7,11], k = 5 Output: 9 Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,…]. The 5th missing positive integer is 9.
Example 2: Input: arr = [1,2,3,4], k = 2 Output: 6 Explanation: The missing positive integers are [5,6,7,…]. The 2nd missing positive integer is 6.
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 1000
1 <= k <= 1000
arr[i] < arr[j] for 1 <= i < j <= arr.length
解析
如果是一个没有missing数的数组,此时arr[i] == i + 1. 也就是说在一个missing了一些数的数组中,到下标i为止missing的个数为arr[i] - (i+1). 这个等式对于数组任意下标i都成立,所以可以用二分查找法快速检索. 当循环结束时 l == r,返回 l + k 即可.
代码
1 2 3 4 5 6 7 8 9 10 11 12
funcfindKthPositive(arr []int, k int)int { var l, r, m int = 0, len(arr), 0 for l < r { m = (l + r) / 2 if(arr[m] - m - 1 < k) { l = m + 1 } else { r = m } } return l + k }
原题在此 Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
Example 1:
Input: head = [1,2,3,3,4,4,5] Output: [1,2,5]
Example 2:
Input: head = [1,1,1,2,3] Output: [2,3]
Constraints:
The number of nodes in the list is in the range [0, 300].
-100 <= Node.val <= 100
The list is guaranteed to be sorted in ascending order.
解析
与Remove Duplicates from Sorted List I不同之处在于,这次要将重复值一个不留完全移除.