Skip to content

Commit 3addd7a

Browse files
committed
0613”
1 parent 705f520 commit 3addd7a

19 files changed

+979
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
3+
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
4+
5+
Given two integers x and y, calculate the Hamming distance.
6+
7+
Note:
8+
0 ≤ x, y < 231.
9+
10+
Example:
11+
12+
Input: x = 1, y = 4
13+
14+
Output: 2
15+
16+
Explanation:
17+
1 (0 0 0 1)
18+
4 (0 1 0 0)
19+
↑ ↑
20+
21+
The above arrows point to positions where the corresponding bits are different.
22+
23+
"""
24+
25+
"""
26+
my solution 二进制解法,异或之后计算1的个数
27+
"""
28+
class Solution(object):
29+
def hammingDistance(self, x, y):
30+
"""
31+
:type x: int
32+
:type y: int
33+
:rtype: int
34+
"""
35+
t = x ^ y
36+
count = 0
37+
while t != 0:
38+
count = count + 1
39+
t = t & (t-1)
40+
return count
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
3+
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
4+
5+
Note:
6+
The given integer is guaranteed to fit within the range of a 32-bit signed integer.
7+
You could assume no leading zero bit in the integer’s binary representation.
8+
Example 1:
9+
Input: 5
10+
Output: 2
11+
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
12+
Example 2:
13+
Input: 1
14+
Output: 0
15+
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
16+
17+
18+
"""
19+
"""与1进行按位与运算,保存每一位,存入stack中,随后转换为十进制"""
20+
class Solution(object):
21+
def findComplement(self, num):
22+
"""
23+
:type num: int
24+
:rtype: int
25+
"""
26+
res = 0
27+
stack = []
28+
while num > 0:
29+
stack.append(1-(num & 1))
30+
num = num >> 1
31+
for i in range(len(stack)-1,-1,-1):
32+
res = res * 2 + stack[i]
33+
return res
34+
35+
"""更巧妙的方法,要得到对位取反,其实就是用全1的数字进行按位异或,但是要位数刚刚好"""
36+
class Solution(object):
37+
def findComplement(self, num):
38+
"""
39+
:type num: int
40+
:rtype: int
41+
"""
42+
i=1
43+
while i<=num:
44+
i = i << 1
45+
return (i-1) ^ num
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
3+
For a web developer, it is very important to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L and width W satisfy the following requirements:
4+
5+
1. The area of the rectangular web page you designed must equal to the given target area.
6+
7+
2. The width W should not be larger than the length L, which means L >= W.
8+
9+
3. The difference between length L and width W should be as small as possible.
10+
You need to output the length L and the width W of the web page you designed in sequence.
11+
Example:
12+
Input: 4
13+
Output: [2, 2]
14+
Explanation: The target area is 4, and all the possible ways to construct it are [1,4], [2,2], [4,1].
15+
But according to requirement 2, [1,4] is illegal; according to requirement 3, [4,1] is not optimal compared to [2,2]. So the length L is 2, and the width W is 2.
16+
17+
"""
18+
"""
19+
暴力搜索
20+
"""
21+
import math
22+
class Solution(object):
23+
def constructRectangle(self, area):
24+
"""
25+
:type area: int
26+
:rtype: List[int]
27+
"""
28+
mid = int(math.sqrt(area))
29+
while mid > 0:
30+
if area % mid == 0:
31+
return [int(area/mid),int(mid)]
32+
mid -= 1
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
3+
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.
4+
5+
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
6+
7+
The order of output does not matter.
8+
9+
Example 1:
10+
11+
Input:
12+
s: "cbaebabacd" p: "abc"
13+
14+
Output:
15+
[0, 6]
16+
17+
Explanation:
18+
The substring with start index = 0 is "cba", which is an anagram of "abc".
19+
The substring with start index = 6 is "bac", which is an anagram of "abc".
20+
Example 2:
21+
22+
Input:
23+
s: "abab" p: "ab"
24+
25+
Output:
26+
[0, 1, 2]
27+
28+
Explanation:
29+
The substring with start index = 0 is "ab", which is an anagram of "ab".
30+
The substring with start index = 1 is "ba", which is an anagram of "ab".
31+
The substring with start index = 2 is "ab", which is an anagram of "ab".
32+
33+
"""
34+
35+
"""
36+
37+
使用一个字典保存应该出现的各字符的次数
38+
39+
"""
40+
41+
42+
class Solution(object):
43+
def findAnagrams(self, s, p):
44+
"""
45+
:type s: str
46+
:type p: str
47+
:rtype: List[int]
48+
"""
49+
res = []
50+
left = right = 0
51+
count = len(p)
52+
dic = dict()
53+
for i in p:
54+
dic[i] = dic.get(i, 0) + 1
55+
while right < len(s):
56+
if s[right] in dic.keys():
57+
if dic[s[right]] >= 1:
58+
count = count - 1
59+
dic[s[right]] = dic[s[right]] - 1
60+
right = right + 1
61+
62+
if count == 0:
63+
res.append(left)
64+
if right - left == len(p):
65+
if s[left] in dic.keys():
66+
if dic[s[left]] >= 0:
67+
count = count + 1
68+
dic[s[left]] += 1
69+
left = left + 1
70+
return res
71+
72+
"""使用counter会更简单"""
73+
from collections import Counter
74+
75+
class Solution(object):
76+
def findAnagrams(self, s, p):
77+
"""
78+
:type s: str
79+
:type p: str
80+
:rtype: List[int]
81+
"""
82+
res = []
83+
pCounter = Counter(p)
84+
sCounter = Counter(s[:len(p)-1])
85+
for i in range(len(p)-1,len(s)):
86+
sCounter[s[i]] += 1 # include a new char in the window
87+
if sCounter == pCounter: # This step is O(1), since there are at most 26 English letters
88+
res.append(i-len(p)+1) # append the starting index
89+
sCounter[s[i-len(p)+1]] -= 1 # decrease the count of oldest char in the window
90+
if sCounter[s[i-len(p)+1]] == 0:
91+
del sCounter[s[i-len(p)+1]] # remove the count if it is 0
92+
return res
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
3+
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
4+
5+
Example 1:
6+
Input: "abab"
7+
8+
Output: True
9+
10+
Explanation: It's the substring "ab" twice.
11+
Example 2:
12+
Input: "aba"
13+
14+
Output: False
15+
Example 3:
16+
Input: "abcabcabcabc"
17+
18+
Output: True
19+
20+
Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
21+
22+
"""
23+
"""my solution"""
24+
25+
class Solution(object):
26+
def repeatedSubstringPattern(self, s):
27+
"""
28+
:type s: str
29+
:rtype: bool
30+
"""
31+
for i in range(1, len(s) / 2 + 1):
32+
if len(s) % i == 0:
33+
leng = len(s) / i
34+
35+
if s[:i] * leng == s:
36+
return True
37+
return False
38+
39+
"""
40+
Basic idea:
41+
42+
First char of input string is first char of repeated substring
43+
Last char of input string is last char of repeated substring
44+
Let S1 = S + S (where S in input string)
45+
Remove 1 and last char of S1. Let this be S2
46+
If S exists in S2 then return true else false
47+
Let i be index in S2 where S starts then repeated substring length i + 1 and repeated substring S[0: i+1]
48+
49+
巧妙的思路,首先将字符串首尾想接,然后掐头去尾,如果字符串出现的话,说明是有重复字串的
50+
"""
51+
52+
53+
def repeatedSubstringPattern(self, str):
54+
"""
55+
:type str: str
56+
:rtype: bool
57+
"""
58+
if not str:
59+
return False
60+
61+
ss = (str + str)[1:-1]
62+
return ss.find(str) != -1
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
3+
Given a word, you need to judge whether the usage of capitals in it is right or not.
4+
5+
We define the usage of capitals in a word to be right when one of the following cases holds:
6+
7+
All letters in this word are capitals, like "USA".
8+
All letters in this word are not capitals, like "leetcode".
9+
Only the first letter in this word is capital if it has more than one letter, like "Google".
10+
Otherwise, we define that this word doesn't use capitals in a right way.
11+
Example 1:
12+
Input: "USA"
13+
Output: True
14+
Example 2:
15+
Input: "FlaG"
16+
Output: False
17+
18+
"""
19+
20+
class Solution(object):
21+
def detectCapitalUse(self, word):
22+
"""
23+
:type word: str
24+
:rtype: bool
25+
"""
26+
return word==word.lower() or word==word.upper() or word==(word[0].upper()+word[1:].lower())
27+
28+
def detectCapitalUse(self, word):
29+
return word.isupper() or word.islower() or word.istitle()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""
2+
3+
You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.
4+
5+
Given n, find the total number of full staircase rows that can be formed.
6+
7+
n is a non-negative integer and fits within the range of a 32-bit signed integer.
8+
9+
Example 1:
10+
11+
n = 5
12+
13+
The coins can form the following rows:
14+
¤
15+
¤ ¤
16+
¤ ¤
17+
18+
Because the 3rd row is incomplete, we return 2.
19+
Example 2:
20+
21+
n = 8
22+
23+
The coins can form the following rows:
24+
¤
25+
¤ ¤
26+
¤ ¤ ¤
27+
¤ ¤
28+
29+
Because the 4th row is incomplete, we return 3.
30+
31+
"""
32+
33+
"""binary search"""
34+
35+
class Solution(object):
36+
def arrangeCoins(self, n):
37+
"""
38+
:type n: int
39+
:rtype: int
40+
"""
41+
if n == 0:
42+
return 0
43+
left = 1
44+
right = n/2
45+
while left <= right:
46+
mid = (right-left)/2+left
47+
total = (mid * (mid+1)) / 2
48+
if total > n:
49+
right = mid - 1
50+
elif n - total < mid + 1:
51+
return mid
52+
elif n-total == mid + 1:
53+
return mid+1
54+
else:
55+
left = left + 1
56+
return left

0 commit comments

Comments
 (0)