Skip to content

Commit dcbaa89

Browse files
committed
2019-5-20
1 parent 2b833f2 commit dcbaa89

File tree

285 files changed

+7252
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

285 files changed

+7252
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
for (int i = 0; i< nums.length; i++){
5+
int complement = target - nums[i];
6+
if (map.containsKey(complement)){
7+
return new int[]{map.get(complement), i};
8+
}
9+
map.put(nums[i], i);
10+
}
11+
return new int[]{};
12+
}
13+
14+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from heapq import *
2+
class MedianFinder(object):
3+
# 维护两个堆,一个大顶堆,一个小顶堆,小顶堆里的数比大顶堆里的数都要大,
4+
# 如果有两个潜在的中位数(两个堆size相同),数据流的中位数就是两个堆顶之和除以2
5+
# 如果只有一个中位数,就看size更小的那个堆的堆顶
6+
# 如果新进来的数比小顶堆的数要小,就把它插入大顶堆
7+
# 如果新进来的数比小顶堆的数要大,就把它插入小顶堆
8+
# 调整两个堆,使得size 差最大为1
9+
def __init__(self):
10+
"""
11+
initialize your data structure here.
12+
"""
13+
self.max_h = list()
14+
self.min_h = list()
15+
heapify(self.max_h)
16+
heapify(self.min_h)
17+
18+
19+
def addNum(self, num):
20+
"""
21+
:type num: int
22+
:rtype: None
23+
"""
24+
heappush(self.min_h, num)
25+
heappush(self.max_h, -heappop(self.min_h))
26+
if len(self.max_h) > len(self.min_h):
27+
heappush(self.min_h, -heappop(self.max_h))
28+
29+
def findMedian(self):
30+
"""
31+
:rtype: float
32+
"""
33+
max_len = len(self.max_h)
34+
min_len = len(self.min_h)
35+
if max_len == min_len: #有两个候选中位数
36+
return (self.min_h[0] + -self.max_h[0]) / 2.
37+
else:#小顶堆的size 一定 >= 大顶堆的size,所以答案就是小顶堆的堆顶
38+
return self.min_h[0] / 1.
39+
40+
# Your MedianFinder object will be instantiated and called as such:
41+
# obj = MedianFinder()
42+
# obj.addNum(num)
43+
# param_2 = obj.findMedian()
44+
45+
class Solution(object):
46+
def findMedianSortedArrays(self, nums1, nums2):
47+
"""
48+
:type nums1: List[int]
49+
:type nums2: List[int]
50+
:rtype: float
51+
"""
52+
mf = MedianFinder()
53+
for num in nums1:
54+
mf.addNum(num)
55+
for num in nums2:
56+
mf.addNum(num)
57+
return mf.findMedian()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution(object):
2+
def longestPalindrome(self, s):
3+
"""
4+
:type s: str
5+
:rtype: str
6+
"""
7+
max_l = 0
8+
res = ""
9+
for i in range(0, len(s)):
10+
#以s[i] 为中心向左右扩散
11+
left, right = i, i
12+
while(left >= 0 and right < len(s) and s[left] == s[right]):
13+
if max_l < right - left + 1:
14+
max_l = right - left + 1
15+
res = s[left:right + 1]
16+
left -= 1
17+
right += 1
18+
19+
#以s[i],s[i+1]为中心向左右扩散
20+
left, right = i, i + 1
21+
while(left >= 0 and right < len(s) and s[left] == s[right]):
22+
if max_l < right - left + 1:
23+
max_l = right - left + 1
24+
res = s[left:right + 1]
25+
left -= 1
26+
right += 1
27+
return res
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution(object):
2+
def convert(self, s, n):
3+
"""
4+
:type s: str
5+
:type numRows: int
6+
:rtype: str
7+
"""
8+
#ÕÒ¹æÂÉ
9+
if n <= 1:
10+
return s
11+
l = len(s)
12+
res = ""
13+
for i in range(n):
14+
tmp, index = "", i
15+
if i in [0, n - 1]:
16+
while(index < l):
17+
18+
tmp += s[index]
19+
index += 2 * (n - 1)
20+
else:
21+
state = "down"
22+
while(index < l):
23+
tmp += s[index]
24+
if state == "down":
25+
state = "up"
26+
index += 2 * (n - 1 - i)
27+
else:
28+
state = "down"
29+
index += 2 * i
30+
res += tmp
31+
32+
return res
33+
34+
35+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def reverse(self, x):
3+
"""
4+
:type x: int
5+
:rtype: int
6+
"""
7+
8+
flag = 0
9+
if x < 0:
10+
flag = 1
11+
if flag:
12+
s = str(x)[1:]
13+
s = s[::-1]
14+
x = -1 *int(s)
15+
else:
16+
s = str(x)
17+
s = s[::-1]
18+
x = int(s)
19+
20+
if x < -1 * 2 **31 or x > 2** 31 -1:
21+
return 0
22+
return x
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution(object):
2+
def myAtoi(self, s):
3+
"""
4+
:type str: str
5+
:rtype: int
6+
"""
7+
s = s.strip(" ")
8+
if not len(s): #排除空
9+
return 0
10+
if s[0] not in ["+", "-"] and not s[0].isdigit(): #排除第一个非空字符不是数字
11+
return 0
12+
op = 1
13+
res = ""
14+
for i, char in enumerate(s):
15+
if i == 0 :
16+
if char == "-":
17+
op = -1
18+
continue
19+
elif char == "+":
20+
continue
21+
if char == " " or not char.isdigit():
22+
break
23+
res += char
24+
# print res, op
25+
if len(res) > 0:
26+
res = op * int(res)
27+
else:
28+
return 0
29+
INT_MIN = -2 **31
30+
INT_MAX = 2 **31 - 1
31+
if res > INT_MAX:
32+
return INT_MAX
33+
elif res < INT_MIN:
34+
return INT_MIN
35+
return res
36+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def maxArea(self, height):
3+
"""
4+
:type height: List[int]
5+
:rtype: int
6+
"""
7+
lo, hi = 0, len(height) - 1
8+
res = 0
9+
while(lo < hi):
10+
if height[lo] > height[hi]:
11+
area = height[hi] * (hi - lo)
12+
hi -= 1
13+
else:
14+
area = height[lo] * (hi - lo)
15+
lo += 1
16+
# print area
17+
res = max(area, res)
18+
19+
return res
20+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
def intToRoman(self, num):
3+
"""
4+
:type num: int
5+
:rtype: str
6+
"""
7+
digit = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
8+
mapping = {1000:"M", 900:"CM", 500:"D",400:"CD", 100:"C", 90: "XC", 50:"L",40: "XL", 10:"X", 9:"IX", 5:"V", 4:"IV", 1:"I"}
9+
res = ""
10+
for i in digit:
11+
res += (num / i) * mapping[i]
12+
num -= i * (num / i)
13+
if num == 0:
14+
break
15+
return res
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def romanToInt(self, s):
3+
"""
4+
:type s: str
5+
:rtype: int
6+
"""
7+
dic = {"I": 1, "V":5, "X": 10, "L":50, "C":100, "D": 500, "M": 1000}
8+
stack = []
9+
res = 0
10+
for inx, item in enumerate(s):
11+
res += dic[item]
12+
# print res
13+
# s.append(item)
14+
if item == "V" or item == "X":
15+
if stack and stack[-1] == "I":
16+
res -= 2
17+
elif item == "L" or item == "C":
18+
if stack and stack[-1] == "X":
19+
res -= 20
20+
elif item == "D" or item == "M":
21+
if stack and stack[-1] == "C":
22+
res -= 200
23+
stack.append(item)
24+
return res
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def longestCommonPrefix(self, strs):
3+
"""
4+
:type strs: List[str]
5+
:rtype: str
6+
"""
7+
if not strs:
8+
return ""
9+
10+
strs.sort()
11+
res = ""
12+
13+
for x, y in zip(strs[0], strs[-1]):
14+
if x == y:
15+
res += x
16+
else:
17+
break
18+
19+
return res
20+

0 commit comments

Comments
 (0)