Skip to content

Commit 04f9df0

Browse files
committed
try
1 parent 6509d3a commit 04f9df0

6 files changed

+1055
-752
lines changed

Python/detect-capital.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Given a word, you need to judge whether the usage of capitals in it is right or not.
3+
4+
We define the usage of capitals in a word to be right when one of the following cases holds:
5+
6+
All letters in this word are capitals, like "USA".
7+
All letters in this word are not capitals, like "leetcode".
8+
Only the first letter in this word is capital if it has more than one letter, like "Google".
9+
Otherwise, we define that this word doesn't use capitals in a right way.
10+
11+
Example 1:
12+
Input: "USA"
13+
Output: True
14+
15+
Example 2:
16+
Input: "FlaG"
17+
Output: False
18+
19+
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
20+
'''
21+
22+
class Solution:
23+
def detectCapitalUse(self, word):
24+
"""
25+
:type word: str
26+
:rtype: bool
27+
"""
28+
29+
# Approach #1
30+
# Time: O(1)
31+
# Space: O(1)
32+
#
33+
# if word.lower() == word or word.capitalize() == word or word == word.upper():
34+
# return True
35+
# return False
36+
37+
# Approach #2 直接在return 中判断,进一步简化语句 注意特殊条件''
38+
# Time: O(1)
39+
# Space: O(1)
40+
#
41+
return word.islower() or word.isupper() or word.istitle() or word == ''

Python/implement-strstr.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
'''
2+
Implement strStr().
3+
4+
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
5+
6+
Example 1:
7+
Input: haystack = "hello", needle = "ll"
8+
Output: 2
9+
10+
Example 2:
11+
Input: haystack = "aaaaa", needle = "bba"
12+
Output: -1
13+
Clarification:
14+
15+
What should we return when needle is an empty string? This is a great question to ask during an interview.
16+
17+
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().
18+
'''
19+
20+
class Solution:
21+
def strStr(self, haystack, needle):
22+
"""
23+
:type haystack: str
24+
:type needle: str
25+
:rtype: int
26+
"""
27+
28+
# Approach #1 以主串为主干,进行一次遍历。边界条件比较多,不容易一次考虑周全。
29+
# Time: O()
30+
# Space: O()
31+
#
32+
# substr_len = len(needle)
33+
# str_len = len(haystack)
34+
35+
# if substr_len == 0: return 0
36+
# if substr_len <= str_len: # 考虑子串比主串更长的情况
37+
# for i in range(str_len): # 主字符串遍历一次
38+
# if haystack[i] == needle[0] and str_len - i >= substr_len:
39+
# for j in range(substr_len):
40+
# if i+j >= str_len or haystack[i+j] != needle[j] : # 小心主串越界
41+
# break
42+
# elif haystack[i+j] == needle[j] and j == substr_len - 1:
43+
# return i
44+
# elif str_len - i < substr_len or i == str_len-1: # 主串过短,无法完成匹配,提前终止。 主串匹配到了最后一个字符也无法匹配的特殊情况 "mississippi" "a"
45+
# return -1
46+
# else:
47+
# return -1
48+
49+
50+
# Approach #2 是不是有点投机的嫌疑? 直接用字符串的内建函数s.find()
51+
# Time: O(1)
52+
# Space: O(1)
53+
#
54+
# if(len(needle)==0):
55+
# return 0
56+
# else:
57+
# return haystack.find(needle)
58+
59+
60+
# Approach #3
61+
# Time: O(n)
62+
# Space: O(1)
63+
#
64+
for i in range(len(haystack)-len(needle)+1): # 有效减少比较的次数
65+
if haystack[i:i+len(needle)]==needle: # 字符串切片操作可以直接比较一个子串,不用每个字符都比较
66+
return i # i 为 0 的时候, haystack[0:0] 表示为 0
67+
return -1
68+
69+
70+
71+

Python/keyboard-row.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'''
2+
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
3+
4+
5+
American keyboard
6+
7+
8+
Example 1:
9+
Input: ["Hello", "Alaska", "Dad", "Peace"]
10+
Output: ["Alaska", "Dad"]
11+
12+
Note:
13+
You may use one character in the keyboard more than once.
14+
You may assume the input string will only contain letters of alphabet.
15+
16+
17+
'''
18+
19+
class Solution:
20+
def findWords(self, words):
21+
"""
22+
:type words: List[str]
23+
:rtype: List[str]
24+
"""
25+
26+
27+
# method once 初始化太复杂了
28+
# Time: O(n^2)
29+
# Space: O(n)
30+
#
31+
# res = []
32+
# ref = [['q','w','e','r','t','y','u','i','o','p'],
33+
# ['a','s','d','f','g','h','j','k','l'],
34+
# ['z','x','c','v','b','n','m']]
35+
# for i in words:
36+
# ss = "".join(set(i.lower())) # 要处理大小写问题
37+
# for j in ref:
38+
# for k in ss:
39+
# if k not in j:
40+
# break
41+
# elif k == ss[-1]:
42+
# res.append(i)
43+
# return res
44+
45+
46+
# method twice 用str的内置函数str.issubset()更加简洁
47+
# Time: O(n)
48+
# Space: O(n)
49+
#
50+
# line1, line2, line3 = set('qwertyuiop'), set('asdfghjkl'), set('zxcvbnm')
51+
# res = []
52+
# for word in words:
53+
# ss = set(word.lower())
54+
# if ss.issubset(line1) or ss.issubset(line2) or ss.issubset(line3):
55+
# res.append(word)
56+
# return res
57+
58+
59+
60+
# method third 本质上和方法二是一样的,只是用all()函数替换了issubset()函数
61+
# Time: O(n)
62+
# Space: O(n)
63+
#
64+
line1, line2, line3 = set('qwertyuiop'), set('asdfghjkl'), set('zxcvbnm')
65+
res = []
66+
for word in words:
67+
smallword = word.lower()
68+
if all([x in line1 for x in smallword]) or all([x in line2 for x in smallword]) or all([x in line3 for x in smallword]):
69+
res.append(word)
70+
return res
71+
72+
73+

Python/next-greater-element-i.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'''
2+
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
3+
4+
The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
5+
6+
Example 1:
7+
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
8+
Output: [-1,3,-1]
9+
Explanation:
10+
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
11+
For number 1 in the first array, the next greater number for it in the second array is 3.
12+
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
13+
14+
Example 2:
15+
Input: nums1 = [2,4], nums2 = [1,2,3,4].
16+
Output: [3,-1]
17+
Explanation:
18+
For number 2 in the first array, the next greater number for it in the second array is 3.
19+
For number 4 in the first array, there is no next greater number for it in the second array, so output -1.
20+
21+
Note:
22+
All elements in nums1 and nums2 are unique.
23+
The length of both nums1 and nums2 would not exceed 1000.
24+
25+
'''
26+
27+
28+
29+
30+
31+
class Solution:
32+
def nextGreaterElement(self, nums1, nums2):
33+
"""
34+
:type nums1: List[int]
35+
:type nums2: List[int]
36+
:rtype: List[int]
37+
"""
38+
# method once 双重循环效率较低
39+
# Time: O(nlogn)
40+
# Space: O(n)
41+
#
42+
# res = []
43+
# length = len(nums2)
44+
# for i in nums1:
45+
# if (nums2.index(i)+1) < length:
46+
# for j in range(nums2.index(i)+1,length):
47+
# if nums2[j] > i:
48+
# res.append(nums2[j])
49+
# break
50+
# elif j == length-1:
51+
# res.append(-1)
52+
# else:
53+
# res.append(-1)
54+
# return res
55+
56+
# method twice 利用字典提升效率
57+
# Time: O(nlogn)
58+
# Space: O(n)
59+
#
60+
# if nums2 != []:
61+
# dic = {nums2[-1]:-1}
62+
# for index in range(len(nums2)) :
63+
# for j in range(index+1,len(nums2)):
64+
# if nums2[index] < nums2[j]:
65+
# dic[ nums2[index]] = nums2[j]
66+
# break
67+
# elif j == len(nums2)-1:
68+
# dic[nums2[index]] = -1
69+
# return [dic.get(i,-1) for i in nums1]
70+
# return []
71+
72+
73+
74+
# method third 利用栈结构提升效率
75+
# Time: O(n)
76+
# Space: O(n)
77+
#
78+
dic , stack = {} ,[]
79+
for number in nums2:
80+
while stack and stack[-1] < number:
81+
dic[stack.pop()] = number
82+
stack.append(number)
83+
return [dic.get(i , -1) for i in nums1]
84+
85+
86+
87+

Python/reverse-string-ii.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
3+
4+
Example:
5+
Input: s = "abcdefg", k = 2
6+
Output: "bacdfeg"
7+
8+
Restrictions:
9+
The string consists of lower English letters only.
10+
Length of the given string and k will in the range [1, 10000]
11+
12+
'''
13+
14+
class Solution:
15+
def reverseStr(self, s, k):
16+
"""
17+
:type s: str
18+
:type k: int
19+
:rtype: str
20+
"""
21+
# Approach #1 字符串的切片和反序操作不能同时进行
22+
# Time: O(n)
23+
# Space: O(1)
24+
25+
res = ''
26+
for i in range(0,len(s),2*k):
27+
res = res + s[i:i+k][::-1] + s[i+k:i+2*k]
28+
return res

0 commit comments

Comments
 (0)