多分隔符+双指针 | HJ74 参数解析
@ 最优解 s = input() flag = 0 p1 = p2 = sum = 0 res = [] while p2 < len(s): if s[p2] == ' ': if p2 - p1 > 0: res.append(s[p1:p2]) p1 = p2+1 elif s[p2] == '"': flag = True p1 = p2 + 1 while flag: p2 += 1 if s[p2] == '"': flag = False res.append(s[p1:p2]) p1 = p2+1 p2 += 1 if p2 - p1 > 0: res.append(s[p1:p2]) print(len(res)) for i in res: print(i) # 我的代码 s = input() res = [] p1, p2 = 0, 0 flag = False for i in range(len(s)): if not flag: if s[p2] == '"': p1 = p2+1 flag = True elif s[p2] == ' ' and p2-p1>0: # p2-p1>0,两指针之间有包住参数再添加 res.append(s[p1:p2]) p1 = p2+1 else: if s[p2] == '"': res.append(s[p1:p2]) p1 = p2+1 flag = False p2 += 1 if p2-p1>0: res.append(s[p1:p2]) print(len(res)) for i in res: print(i.strip())
用时:50min
#双指针#华为笔试刷题 文章被收录于专栏
高质量题: 1~40:HJ16,HJ22,HJ24,HJ26,HJ27,HJ28,HJ35,HJ37,HJ39; 40~80:HJ41,HJ42,HJ43,HJ44,HJ48,HJ50,HJ52,HJ53,HJ57,HJ61,HJ63,HJ64,HJ70,HJ71,HJ74,HJ77; 80~108:HJ82,HJ85,HJ88,HJ89,HJ93,HJ95,HJ98,HJ103,HJ107