删除元素 | HJ23 删除字符串中出现次数最少的字符
while True: try: s = input() r = {} for i, ch in enumerate(s): r.setdefault(ch, 0) r[ch] += 1 s_r = dict(sorted(r.items(), key=lambda x:x[1])) min_f = list(s_r.values())[0] d = [k for k,v in s_r.items() if v==min_f] # 法1:不用删除的元素加列表 # res = [] # for i in range(len(s)): # if s[i] not in d: # res.append(s[i]) # print(''.join(res)) # 法2:要删除的元素用replace for ch in d: s = s.replace(ch, '') print(s) except: break
用时:20min
华为笔试刷题 文章被收录于专栏
高质量题: 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