Commit f922668d by bonnieyan

Merge remote-tracking branch 'origin/master'

parents 7ab1d2b3 7e520d96
# Created by .ignore support plugin (hsz.mobi)
......@@ -46,3 +46,6 @@ while True:
elif re.findall("再见|bye|拜拜", s):
print("再见")
break
#山脉数组
# 山脉数组
# 遍历了2次 时间复杂度是O(n^2)
def peakIndexInMountainArray(A):
index = 0
for i in range(1, len(A)):
if A[i-1] < A[i] > A[i+1]:
if A[i - 1] < A[i] > A[i + 1]:
index = i
# break 这里加上break 跳出语句减少循环次数
print(index)
if __name__ =='__main__':
if __name__ == '__main__':
peakIndexInMountainArray([0, 1, 0])
peakIndexInMountainArray([0, 2, 1, 0])
\ No newline at end of file
peakIndexInMountainArray([0, 2, 1, 0])
# 通过二分查找实现 O(log2n)
def peakIndexInMountainArray(mountain):
height = len(mountain)
low = 0
while low <= height:
mid = (height + low) // 2
if array[mid] > array[mid - 1] and array[mid] > array[mid + 1]:
print(mid)
break
elif array[mid] > array[mid + 1]:
height = mid - 1
else:
low = mid + 1
array = [0, 1, 0]
peakIndexInMountainArray(array)
array = [0, 2, 1, 0]
peakIndexInMountainArray(array)
array = [0, 1, 2, 3, 7, 6, 5, 4, 2, 1, 0]
peakIndexInMountainArray(array)
array = [0, 1, 2, 7, 6, 5, 4, 0]
peakIndexInMountainArray(array)
words = ["gin", "zen", "gig", "msg"]
mima = {"a":".-","b":"-...","c":"-.-.","d":"-..","e":".","f":"..-.","g":"--.","h":"....","i":"..","j":".---","k":"-.-","l":".-..","m":"--",
"n":"-.","o":"---","p":".--.","q":"--.-","r":".-.","s":"...","t":"-","u":"..-","v":"...-","w":".--","x":"-..-","y":"-.--","z":"--.."}
mima = {"a": ".-", "b": "-...", "c": "-.-.", "d": "-..", "e": ".", "f": "..-.", "g": "--.", "h": "....", "i": "..",
"j": ".---", "k": "-.-", "l": ".-..", "m": "--",
"n": "-.", "o": "---", "p": ".--.", "q": "--.-", "r": ".-.", "s": "...", "t": "-", "u": "..-", "v": "...-",
"w": ".--", "x": "-..-", "y": "-.--", "z": "--.."}
word = ""
list_code = []
count = 0
#外层循环获取字符gin
# 外层循环获取字符gin
for i in words:
#内层循环获取字母对应的摩斯密码
# 内层循环获取字母对应的摩斯密码
for j in i:
word += mima[j]
#将对应字符gin的摩斯密码组合放入s
# 将对应字符gin的摩斯密码组合放入s
s = word
if s not in list_code:
count += 1
#将不一样的摩斯密码组合放入list_code
# 将不一样的摩斯密码组合放入list_code
list_code.append(s)
#清空组合的摩斯密码以便外层循环存放新的摩斯密码
# 清空组合的摩斯密码以便外层循环存放新的摩斯密码
word = ""
print(count)
print(list_code)
\ No newline at end of file
print(list_code)
morse = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---",
".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
# 根据ascii 小写97 a 开始创建字典
morse_dict = {chr(i + 97): morse[i] for i in range(0, len(morse))}
def uniqueMorseRepresentations(words):
# 使用set 用来去重
unique = set()
for word in words:
unique.add(toMorseCode(word))
return len(unique)
def toMorseCode(word):
morse_code = ""
for char in word.lower():
morse_code += morse_dict.get(char)
return morse_code
check_words = ["gin", "zen", "gig", "msg"]
print(uniqueMorseRepresentations(check_words))
# 复杂度是较高
def numJewelsInStones(J, S):
J_length = len(J)
S_length = len(S)
......@@ -6,7 +7,32 @@ def numJewelsInStones(J, S):
count = 0
for s in S:
if s in J:
count = count + 1
count = count + 1
return count
print(numJewelsInStones("ba", "aAAbbbb"))
\ No newline at end of file
print(numJewelsInStones("ba", "aAAbbbb"))
# 复杂度O(n)
def numJewelsInStones(J, S):
stores = {}
for c in S:
if c in stores:
stores[c] = stores[c] + 1
else:
stores[c] = 1
count = 0
for c in J:
if c in stores:
count += stores[c]
print(count)
J = "aA"
S = "aAAbbbb"
numJewelsInStones(J, S)
J = "z"
S = "ZZ"
numJewelsInStones(J, S)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment