题解 | #两数之和#
单词消消乐
http://www.nowcoder.com/practice/abb14fd6e1a34b0fb8016dfd7a99dfc5
思路:
- 如果输入为空直接返回空
- 循环取Words中字符串,如果取到的为空,则上次处理结果不变。
- 两个字串拼接过程:查看res末尾与strs头是否相等,相等则抵消,python数组截取相当强大。直到末尾和头不相等,拼接res和strs剩余部分,将得到新串重新赋值res。
- 最终拼接结果即res
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param Words string字符串一维数组 # @return string字符串 # class Solution: def WordsMerge(self , Words ): # write code here if len(Words) == 0: return "" #strall = ''.join(Words) res = "" for strs in Words: if res == "": res = strs elif strs == "": continue else: while len(res) >0 and res[len(res) - 1] == strs[0]: res = res[:-1] strs = strs[1:] res = res + strs return res
