题解 | #参数解析#
参数解析
http://www.nowcoder.com/practice/668603dc307e4ef4bb07bcd0615ea677
#1.一开始用了复杂度为O(n)的解法,但是只能通过7组用例,有的情况还是没能考虑到;
#2.目前只想到O(n^2)的暴力解法
while True: try: str = input().split(' ') #先处理输入,按空格划分 temp = list() #临时列表,用于存放处理后的元素 for i in str: if i.count('"') == 1 and i.startswith('"'): #因为双引号成对存在,所以先找到左引号所在的元素,这里判断引号个数==1,是为了排除诸如"d:\"这种中间没有空格的情况 index_front = str.index(i)#找到左引号元素的位置 for j in str[index_front:]: #开始找配对,从左引号后面的元素开始搜索 if j.endswith('"'): index_end = str.index(j) #找到右引号的元素的位置 new_element = ' '.join(str[index_front:index_end+1])#把引号中间的元素重新添加空格,组成单个元素 del str[index_front:index_end] #把中间的元素删除 temp.append(new_element) #添加到临时列表 else: continue else: temp.append(i) #如果是不带引号的元素或者不是引号中间的元素,直接添加到临时列表中,不改变其顺序,这也是前面要把引号中间的元素删除的原因 except: break print(len(str)) for element in temp: if '"' in element: #这一步处理诸如"d:\"这种元素,把两端引号删除 element = element.strip('"') print(element)