Skip to content

Commit 3b13a76

Browse files
committed
0615
1 parent 9989d43 commit 3b13a76

25 files changed

+738
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
3+
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
4+
5+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
6+
7+
Example:
8+
Given nums = [2, 7, 11, 15], target = 9,
9+
10+
Because nums[0] + nums[1] = 2 + 7 = 9,
11+
return [0, 1].
12+
13+
"""
14+
15+
class Solution(object):
16+
def twoSum(self, nums, target):
17+
"""
18+
:type nums: List[int]
19+
:type target: int
20+
:rtype: List[int]
21+
"""
22+
dic = dict()
23+
for index,value in enumerate(nums):
24+
sub = target - value
25+
if sub in dic:
26+
return [dic[sub],index]
27+
else:
28+
dic[value] = index
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
3+
Given numRows, generate the first numRows of Pascal's triangle.
4+
5+
For example, given numRows = 5,
6+
Return
7+
8+
[
9+
[1],
10+
[1,1],
11+
[1,2,1],
12+
[1,3,3,1],
13+
[1,4,6,4,1]
14+
]
15+
Subscribe to see which companies asked this question.
16+
17+
"""
18+
"""傻傻的使用循环的方式生成杨辉三角,其实有更简单的方法:
19+
注意判断输入为0行的情况
20+
1 3 3 1 0
21+
+ 0 1 3 3 1
22+
= 1 4 6 4 1
23+
"""
24+
class Solution(object):
25+
def generate(self, numRows):
26+
"""
27+
:type numRows: int
28+
:rtype: List[List[int]]
29+
"""
30+
if numRows == 0:return []
31+
res = [[1]]
32+
for i in range(1,numRows):
33+
res.append(map(lambda x,y:x+y,res[-1]+[0],[0]+res[-1]))
34+
return res
35+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""
2+
3+
Given an index k, return the kth row of the Pascal's triangle.
4+
5+
For example, given k = 3,
6+
Return [1,3,3,1].
7+
8+
"""
9+
"""利用118题的思路轻松求解"""
10+
11+
12+
class Solution(object):
13+
def getRow(self, rowIndex):
14+
"""
15+
:type rowIndex: int
16+
:rtype: List[int]
17+
"""
18+
res = [1]
19+
for i in range(1, rowIndex + 1):
20+
res = list(map(lambda x, y: x + y, res + [0], [0] + res))
21+
return res
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
3+
Say you have an array for which the ith element is the price of a given stock on day i.
4+
5+
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
6+
7+
Example 1:
8+
Input: [7, 1, 5, 3, 6, 4]
9+
Output: 5
10+
11+
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
12+
Example 2:
13+
Input: [7, 6, 4, 3, 1]
14+
Output: 0
15+
16+
In this case, no transaction is done, i.e. max profit = 0.
17+
18+
"""
19+
"""使用53题的思路,两个变量"""
20+
21+
class Solution(object):
22+
def maxProfit(self, prices):
23+
"""
24+
:type prices: List[int]
25+
:rtype: int
26+
"""
27+
curSum=maxSum=0
28+
for i in range(1,len(prices)):
29+
curSum=max(0,curSum+prices[i]-prices[i-1])
30+
maxSum = max(curSum,maxSum)
31+
return maxSum
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
3+
Say you have an array for which the ith element is the price of a given stock on day i.
4+
5+
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
6+
7+
"""
8+
"""这道题比较简单,因为没有买卖次数限制,也没有买卖时间限制,如果后面的股价比前面的大,我们就买卖"""
9+
class Solution(object):
10+
def maxProfit(self, prices):
11+
"""
12+
:type prices: List[int]
13+
:rtype: int
14+
"""
15+
return sum(max(prices[i+1]-prices[i],0) for i in range(len(prices)-1))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
3+
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.
4+
5+
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.
6+
7+
You may assume that each input would have exactly one solution and you may not use the same element twice.
8+
9+
Input: numbers={2, 7, 11, 15}, target=9
10+
Output: index1=1, index2=2
11+
12+
"""
13+
14+
"""其实跟第一题是一样的题目"""
15+
class Solution(object):
16+
def twoSum(self, numbers, target):
17+
"""
18+
:type numbers: List[int]
19+
:type target: int
20+
:rtype: List[int]
21+
"""
22+
res = dict()
23+
for i in range(0,len(numbers)):
24+
sub = target - numbers[i]
25+
if sub in res.keys():
26+
return [res[sub]+1,i+1]
27+
else:
28+
res[numbers[i]] = i
29+
return []
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
3+
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
4+
5+
You may assume that the array is non-empty and the majority element always exist in the array.
6+
7+
Credits:
8+
Special thanks to @ts for adding this problem and creating all test cases.
9+
10+
Subscribe to see which companies asked this question.
11+
12+
"""
13+
14+
"""思路,因为出现次数最多的元素大于元素个数的一半,所以使用count和cand,设目标值为a
15+
"""
16+
17+
18+
class Solution(object):
19+
def majorityElement(self, nums):
20+
"""
21+
:type nums: List[int]
22+
:rtype: int
23+
"""
24+
cand = nums[0]
25+
count = 1
26+
for i in nums[1:]:
27+
if count == 0:
28+
cand, count = i, 1
29+
else:
30+
if i == cand:
31+
count = count + 1
32+
else:
33+
count = count - 1
34+
return cand
35+
"""一行的思路"""
36+
class Solution(object):
37+
def majorityElement(self, nums):
38+
"""
39+
:type nums: List[int]
40+
:rtype: int
41+
"""
42+
return sorted(nums)[len(nums)/2]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
3+
Rotate an array of n elements to the right by k steps.
4+
5+
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
6+
7+
Note:
8+
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
9+
10+
[show hint]
11+
12+
Related problem: Reverse Words in a String II
13+
14+
Credits:
15+
Special thanks to @Freezen for adding this problem and creating all test cases.
16+
17+
Subscribe to see which companies asked this question.
18+
19+
"""
20+
"""直接拼接数组即可"""
21+
22+
23+
class Solution(object):
24+
def rotate(self, nums, k):
25+
"""
26+
:type nums: List[int]
27+
:type k: int
28+
:rtype: void Do not return anything, modify nums in-place instead.
29+
"""
30+
n = len(nums)
31+
nums[:] = nums[n - k:] + nums[:n - k]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
3+
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
4+
5+
"""
6+
"""使用set不能有重复值的特性,判断前后长度是否相同"""
7+
class Solution(object):
8+
def containsDuplicate(self, nums):
9+
"""
10+
:type nums: List[int]
11+
:rtype: bool
12+
"""
13+
return len(nums) != len(set(nums))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
3+
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
4+
5+
"""
6+
"""仍然用dict保存数组元素出现的位置,两种情况下更新"""
7+
class Solution(object):
8+
def containsNearbyDuplicate(self, nums, k):
9+
"""
10+
:type nums: List[int]
11+
:type k: int
12+
:rtype: bool
13+
"""
14+
dic = dict()
15+
for index,value in enumerate(nums):
16+
if value in dic and index - dic[value] <= k:
17+
return True
18+
dic[value] = index
19+
return False

0 commit comments

Comments
 (0)