Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9676de

Browse files
committed
Update
1 parent 5a0c22f commit d9676de

File tree

7 files changed

+269
-29
lines changed

7 files changed

+269
-29
lines changed

ReverseWordsInStringIII.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Given a string, you need to reverse the order of characters in each word
2+
# within a sentence while still preserving whitespace and initial word order.
3+
#
4+
# Example 1:
5+
# Input: "Let's take LeetCode contest"
6+
# Output: "s'teL ekat edoCteeL tsetnoc"
7+
# Note: In the string, each word is separated by single space and there will
8+
# not be any extra space in the string.
9+
#
10+
11+
12+
class Solution(object):
13+
def reverseWords(self, s):
14+
"""
15+
:type s: str
16+
:rtype: str
17+
"""
18+
return " ".join(i[::-1] for i in s.split(" "))

countSmaller.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# You are given an integer array nums and you have to return a new counts array. The counts array has the property where counts[i] is the number of smaller elements to the right of nums[i].
1+
# You are given an integer array nums and you have to return a new counts array
2+
# The counts array has the property where counts[i] is the number of smaller
3+
# elements to the right of nums[i].
24
#
35
# Example:
46
#
@@ -9,37 +11,22 @@
911
# To the right of 6 there is 1 smaller element (1).
1012
# To the right of 1 there is 0 smaller element.
1113
# Return the array [2, 1, 1, 0].
14+
15+
1216
class Solution(object):
1317
def countSmaller(self, nums):
1418
"""
1519
:type nums: List[int]
1620
:rtype: List[int]
1721
"""
18-
19-
20-
l = []
21-
22-
for val in nums:
23-
idx = self.binarySearch(l, val)
24-
if l[idx] != val:
25-
l = l[:idx] + [val] + l[idx:]
26-
27-
d = {}
28-
res = [0] * len(nums)
29-
for i in range(len(nums), -1, -1):
30-
val = nums[i]
31-
res[i]= d.get(val, 0)
32-
idx = self.binarySearch(l, val)
33-
34-
def binarySearch(self, nums, val):
35-
l = 0
36-
r = len(nums) - 1
37-
while(l <= r):
38-
mid = l + (r - l) / 2
39-
if nums[mid] < val:
40-
l = mid + 1
41-
elif nums[mid] > val:
42-
r = mid - 1
43-
else:
44-
return mid
45-
return l
22+
import bisect
23+
vals = []
24+
n = len(nums)
25+
if n == 0:
26+
return []
27+
smaller = [0] * n
28+
for i in range(n - 1, -1, -1):
29+
j = bisect.bisect_left(vals, nums[i])
30+
smaller[i] = j
31+
vals.insert(j, nums[i])
32+
return smaller

greyCode.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# The gray code is a binary numeral system where two successive values differ
2+
# in only one bit.
3+
#
4+
# Given a non-negative integer n representing the total number of bits in the
5+
# code, print the sequence of gray code. A gray code sequence must begin with
6+
# 0.
7+
#
8+
# For example, given n = 2, return [0,1,3,2]. Its gray code sequence is:
9+
#
10+
# 00 - 0
11+
# 01 - 1
12+
# 11 - 3
13+
# 10 - 2
14+
# Note:
15+
# For a given n, a gray code sequence is not uniquely defined.
16+
#
17+
# For example, [0,2,3,1] is also a valid gray code sequence according to the
18+
# above definition.
19+
#
20+
# For now, the judge is able to judge based on one instance of gray code
21+
# sequence. Sorry about that.
22+
23+
24+
class Solution(object):
25+
def grayCode(self, n):
26+
if n < 2:
27+
return range(n)
28+
l = self.grayCode(n - 1)
29+
offset = 1 << (n-1)
30+
return l + [offset + i for i in l[::-1]]

isSubstring.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Given a string s and a string t, check if s is subsequence of t.
2+
#
3+
# You may assume that there is only lower case English letters in both s and t.
4+
# t is potentially a very long (length ~= 500,000) string, and s is a short
5+
# string (<=100).
6+
#
7+
# A subsequence of a string is a new string which is formed from the original
8+
# string by deleting some (can be none) of the characters without disturbing
9+
# the relative positions of the remaining characters. (ie, "ace" is a
10+
# subsequence of "abcde" while "aec" is not).
11+
#
12+
# Example 1:
13+
# s = "abc", t = "ahbgdc"
14+
#
15+
# Return true.
16+
#
17+
# Example 2:
18+
# s = "axc", t = "ahbgdc"
19+
#
20+
# Return false.
21+
22+
23+
class Solution(object):
24+
def isSubsequence(self, s, t):
25+
"""
26+
:type s: str
27+
:type t: str
28+
:rtype: bool
29+
"""
30+
i = len(s) - 1
31+
j = len(t) - 1
32+
while(i >= 0 and j >= 0):
33+
if s[i] == t[j]:
34+
i -= 1
35+
j -= 1
36+
return i < 0

mergeIntervals.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Given a collection of intervals, merge all overlapping intervals.
2+
#
3+
# For example,
4+
# Given [1,3],[2,6],[8,10],[15,18],
5+
# return [1,6],[8,10],[15,18].
6+
7+
# Definition for an interval.
8+
9+
10+
class Interval(object):
11+
def __init__(self, s=0, e=0):
12+
self.start = s
13+
self.end = e
14+
15+
16+
class Solution(object):
17+
def merge(self, intervals):
18+
"""
19+
:type intervals: List[Interval]
20+
:rtype: List[Interval]
21+
"""
22+
23+
n = len(intervals)
24+
if n <= 1:
25+
return intervals
26+
27+
ints = sorted([(i.start, i.end) for i in intervals])
28+
s, e = ints[0]
29+
res = []
30+
for (a, b) in ints[1:]:
31+
if b <= e:
32+
continue
33+
34+
if a > e:
35+
res.append(Interval(s, e))
36+
s = a
37+
38+
e = b
39+
40+
res.append(Interval(s, e))
41+
return res

onesAndZeros.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# In the computer world, use restricted resource you have to generate maximum
2+
# benefit is what we always want to pursue.
3+
#
4+
# For now, suppose you are a dominator of m 0s and n 1s respectively. On the
5+
# other hand, there is an array with strings consisting of only 0s and 1s.
6+
#
7+
# Now your task is to find the maximum number of strings that you can form with
8+
# given m 0s and n 1s. Each 0 and 1 can be used at most once.
9+
#
10+
# Note:
11+
# The given numbers of 0s and 1s will both not exceed 100
12+
# The size of given string array won't exceed 600.
13+
# Example 1:
14+
# Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
15+
# Output: 4
16+
#
17+
# Explanation: This are totally 4 strings can be formed by the using of 5 0s
18+
# and 3 1s, which are “10,”0001”,”1”,”0”
19+
# Example 2:
20+
# Input: Array = {"10", "0", "1"}, m = 1, n = 1
21+
# Output: 2
22+
#
23+
# Explanation: You could form "10", but then you'd have nothing left. Better
24+
# form "0" and "1".
25+
26+
27+
class Solution(object):
28+
def findMaxForm(self, strs, m, n):
29+
"""
30+
:type strs: List[str]
31+
:type m: int
32+
:type n: int
33+
:rtype: int
34+
"""
35+
from collections import Counter
36+
37+
l = len(strs)
38+
dp = [[0] * (n + 1) for _ in range(m + 1)]
39+
40+
for k in range(l):
41+
c = Counter(strs[k])
42+
a, b = c['0'], c['1']
43+
for i in range(m, a - 1, -1):
44+
for j in range(n, b - 1, -1):
45+
dp[i][j] = max(dp[i][j], dp[i - a][j - b] + 1)
46+
47+
return dp[m][n]

weeklyContest25.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/python -tt
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution():
6+
def checkPerfectNumber(self, num):
7+
"""
8+
:type num: int
9+
:rtype: bool
10+
"""
11+
if num <= 1:
12+
return False
13+
from math import sqrt
14+
divisors = set([1])
15+
for i in range(2, int(sqrt(num)) + 1):
16+
if num % i == 0:
17+
divisors.add(i)
18+
divisors.add(num/i)
19+
# print divisors
20+
return num == sum(divisors)
21+
22+
def complexNumberMultiply(self, a, b):
23+
"""
24+
:type a: str
25+
:type b: str
26+
:rtype: str
27+
"""
28+
ar, ai = self.strToComp(a)
29+
br, bi = self.strToComp(b)
30+
sr = ar * br - ai * bi
31+
si = ar * bi + ai * br
32+
return str(sr) + "+" + str(si) + "i"
33+
34+
def strToComp(self, s):
35+
a, b = s.split("+")
36+
return (int(a), int(b[:-1]))
37+
38+
def boundaryOfBinaryTree(self, root):
39+
"""
40+
:type root: TreeNode
41+
:rtype: List[int]
42+
"""
43+
if root is None:
44+
return []
45+
if (root.left is None) and (root.right is None):
46+
return [root.val]
47+
48+
if (root.left is None):
49+
l, v, r = self.boundary(root.right)
50+
print v, r
51+
return [root.val] + v[:-1] + r
52+
53+
if (root.right) is None:
54+
l, v, r = self.boundary(root.left)
55+
return [root.val] + l[:-1] + v
56+
57+
l, v, r = self.boundary(root)
58+
return l[:-1] + v[:-1] + r[:-1]
59+
60+
def boundary(self, root):
61+
if root is None:
62+
return ([], [], [])
63+
if (root.left is None) and (root.right is None):
64+
return ([root.val], [root.val], [root.val])
65+
if (root.left is None):
66+
rl, rv, rr = self.boundary(root.right)
67+
return ([root.val] + rl, rv, rr + [root.val])
68+
if (root.right is None):
69+
ll, lv, lr = self.boundary(root.left)
70+
return ([root.val] + ll, lv, lr + [root.val])
71+
72+
ll, lv, lr = self.boundary(root.left)
73+
rl, rv, rr = self.boundary(root.right)
74+
return ([root.val] + ll, lv + rv, rr + [root.val])
75+
76+
def removeBoxes(self, boxes):
77+
"""
78+
:type boxes: List[int]
79+
:rtype: int
80+
"""
81+
pass

0 commit comments

Comments
 (0)