leetcode
leetcode-245. Shortest Word Distance III
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
word1 and word2 may be the same and they represent two individual words in the list.
For example,
Assume that words = [“practice”, “makes”, “perfect”, “coding”, “makes”].
Given word1 = “makes”, word2 = “coding”, return 1.
Given word1 = “makes”, word2 = “makes”, return 3.
Note:
You may assume word1 and word2 are both in the list.
原题地址
比Shortest Word Distance的第三种解法多一个判断。
leetcode-244. Shortest Word Distance II
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.
For example,
Assume that words = [“practice”, “makes”, “perfect”, “coding”, “makes”].
Given word1 = “coding”, word2 = “practice”, return 3.
Given word1 = “makes”, word2 = “coding”, return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
原题地址
哈哈哈,人生的大喜大悲不过如此。刚刚还在吐槽zz的解法正中这道题下怀,直接贴上代码。
leetcode-243. Shortest Word Distance
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
For example,
Assume that words = [“practice”, “makes”, “perfect”, “coding”, “makes”].
Given word1 = “coding”, word2 = “practice”, return 3.
Given word1 = “makes”, word2 = “coding”, return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
原题地址
感觉自己就跟zz一样。
首先扔出自己的2b解法。O(n) time, and O(n) space.
然后在网上看到比较标准的解法。
然后又看到了更为简洁的写法。
精益求精。
leetcode 167. Two Sum II - Input array is sorted
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
原题地址
思路:Two pointers,从两边向中间找。代码如下:
leetcode 370. Range Addition
Assume you have an array of length n initialized with all 0’s and are given k update operations.
Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex … endIndex] (startIndex and endIndex inclusive) with inc.
Return the modified array after all k operations were executed.
原题地址
Google OA题。思路就是先把每一步的操作加、减到开始、结束位,最后累积求和。
不知道为什么速度不是很快,代码如下:
leetcode 280. Wiggle Sort
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]….
For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4].
原题地址
原理很简单,如果发现有违反规则的就跟前一个交换。一开始理解这道题认为就是一大一小交替排列,跑了一遍发现第一个必须是小。代码如下:
看了网上的答案,发现我的脑回路果然跟主流的不一样XD。正常的思路应该是,奇数位的要比前面大,偶数位的要比前面小。代码如下:
leetcode三刷
之前断断续续刷了两波leetcode,最近面试发现又忘了差不多了。今天借到了一个会员账号,开始三刷之旅。借鉴一下其他博主的经验,这里把做过的题的思路都整理下来,方便以后回顾。我们优先从上锁题开始吧!