1 Leetcode
1.1 樣例
題目:
給定一個整數數組 nums
和一個目標值 target
,請你在該數組中找出和為目標值的那 兩個 整數,並返回他們的數組下標。
代碼:
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
hashmap = {}
for index, num in enumerate(nums):
another_num = target - num
if another_num in hashmap:
return [hashmap[another_num], index]
hashmap[num] = index
return None
測試用例:
[2,7,11,15]
9
測試用例執行結果:
提交結果:

2 牛客網
2.1 樣例
題目:
字節跳動2018 萬萬沒想到之聰明的編輯
我叫王大錘,是一家出版社的編輯。我負責校對投稿來的英文稿件,這份工作非常煩人,因為每天都要去修正無數的拼寫錯誤。但是,優秀的人總能在平凡的工作中發現真理。我發現一個發現拼寫錯誤的捷徑:
1 三個同樣的字母連在一起,一定是拼寫錯誤,去掉一個的就好啦:比如 helllo -> hello
2 兩對一樣的字母(AABB型)連在一起,一定是拼寫錯誤,去掉第二對的一個字母就好啦:比如 helloo -> hello
3 上面的規則優先“從左到右”匹配,即如果是AABBCC,雖然AABB和BBCC都是錯誤拼寫,應該優先考慮修復AABB,結果為AABCC
我特喵是個天才!我在藍翔學過挖掘機和程序設計,按照這個原理寫了一個自動校對器,工作效率從此起飛。用不了多久,我就會出任CEO,當上董事長,迎娶白富美,走上人生巔峰,想想都有點小激動呢!
……
萬萬沒想到,我被開除了,臨走時老板對我說: “做人做事要兢兢業業、勤勤懇懇、本本分分,人要是行,干一行行一行。一行行行行行;要是不行,干一行不行一行,一行不行行行不行。” 我現在整個人紅紅火火恍恍惚惚的……
請聽題:請實現大錘的自動校對程序
代碼:
def fun(s):
if not s:
return s
else:
tmp, res = '', ''
for i in range(len(s)):
if tmp == '':
tmp += s[i]
elif len(tmp) == 1:
if s[i] != tmp[0]:
res += tmp
tmp = s[i]
else:
tmp += s[i]
elif len(tmp) == 2:
if s[i] == tmp[0]:
continue
else:
tmp += s[i]
elif len(tmp) == 3:
if s[i] != tmp[-1]:
res += tmp
tmp = s[i]
else:
continue
res += tmp
return res
if __name__ == '__main__':
for _ in range(int(input())):
s = input().strip()
res = fun(s)
print(res)
測試用例:
輸入
2
helloo
wooooooow
輸出
hello
woow
測試用例執行結果:

提交結果:
