Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e3b29f6

Browse files
add 448.py
1 parent ca9e11d commit e3b29f6

9 files changed

+180
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def findDisappearedNumbers(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[int]
6+
"""
7+
return list(set([i for i in range(1, len(nums)+1)]) - set(nums))
8+
9+
10+
def main():
11+
s = Solution()
12+
print s.findDisappearedNumbers([4,3,2,7,8,2,3,1])
13+
14+
15+
if __name__ == '__main__':
16+
main()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def findMaxConsecutiveOnes(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
cnt = 0
8+
max_cnt = 0
9+
for i in range(len(nums)):
10+
if nums[i]:
11+
cnt += 1
12+
if cnt > max_cnt:
13+
max_cnt = cnt
14+
else:
15+
cnt = 0
16+
return max_cnt
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def nextGreaterElement(self, findNums, nums):
3+
"""
4+
:type findNums: List[int]
5+
:type nums: List[int]
6+
:rtype: List[int]
7+
"""
8+
dic = {}
9+
10+
for i in range(len(nums)):
11+
for j in range(i, len(nums)):
12+
if nums[j] > nums[i]:
13+
dic[nums[i]] = nums[j]
14+
break
15+
else:
16+
dic[nums[i]] = -1
17+
18+
return [dic[i] for i in findNums]

‎py_solution/506. Relative Ranks.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution(object):
2+
def findRelativeRanks(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[str]
6+
"""
7+
ret = []
8+
awarded = sorted(nums, reverse=True)
9+
10+
for i in nums:
11+
if i == awarded[0]:
12+
ret.append("Gold Medal")
13+
elif len(awarded) >= 2 and i == awarded[1]:
14+
ret.append("Silver Medal")
15+
elif len(awarded) >= 3 and i == awarded[2]:
16+
ret.append("Bronze Medal")
17+
else:
18+
ret.append(str(awarded.index(i)+1))
19+
return ret
20+
21+
22+
def main():
23+
s = Solution()
24+
ret = s.findRelativeRanks([5,4,3,2,1])
25+
print ret
26+
27+
28+
if __name__ == '__main__':
29+
main()

‎py_solution/520. Detect Capital.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution(object):
2+
def detectCapitalUse(self, word):
3+
"""
4+
:type word: str
5+
:rtype: bool
6+
"""
7+
8+
if all(map(lambda x: x.isupper(), word)):
9+
return True
10+
elif all(map(lambda x: x.islower(), word)):
11+
return True
12+
elif word[0].isupper() and all(map(lambda x: x.islower(), word[1:])):
13+
return True
14+
else:
15+
return False
16+
17+
def main():
18+
s = Solution()
19+
print s.detectCapitalUse("flaG")
20+
21+
if __name__ == '__main__':
22+
main()

‎py_solution/566. Reshape the Matrix.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution(object):
2+
def matrixReshape(self, nums, r, c):
3+
"""
4+
:type nums: List[List[int]]
5+
:type r: int
6+
:type c: int
7+
:rtype: List[List[int]]
8+
"""
9+
ret = []
10+
l = []
11+
12+
for row in nums:
13+
l.extend(row)
14+
if len(l) != r*c:
15+
return nums
16+
else:
17+
for i in range(r):
18+
if i*c >= len(l):
19+
break
20+
row = []
21+
for j in range(c):
22+
if i*c+j >= len(l):
23+
break
24+
row.append(l[i*c+j])
25+
ret.append(row)
26+
return ret
27+
28+
29+
def main():
30+
'''
31+
none
32+
'''
33+
s = Solution()
34+
ret = s.matrixReshape([[1,2],[3,4]], 2, 4)
35+
print ret
36+
37+
if __name__ == '__main__':
38+
main()

‎py_solution/575. Distribute Candies.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution(object):
2+
def distributeCandies(self, candies):
3+
"""
4+
:type candies: List[int]
5+
:rtype: int
6+
"""
7+
kinds = len(set(candies))
8+
maxmium = len(candies) / 2
9+
return kinds if kinds < maxmium else maxmium
10+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def tree2str(self, t):
10+
"""
11+
:type t: TreeNode
12+
:rtype: str
13+
"""
14+
s = ""
15+
if t:
16+
s = str(t.val)
17+
if t.left:
18+
s += "(" + self.tree2str(t.left) + ")"
19+
if not t.left and t.right:
20+
s += "()"
21+
if t.right:
22+
s += "(" + self.tree2str(t.right) + ")"
23+
return s
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution(object):
2+
def maximumProduct(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
sort = sorted(nums)
8+
return max(sort[-1]*sort[-2]*sort[-3], sort[-1]*sort[0]*sort[1])

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /