算法题4——字符串
1、字符串反转
def reverse_string(s): return s[::-1] print(reverse_string('hello word'))
2、字符串去重
s = "hello world" new_s = [] for char in s: if char not in new_s: new_s.append(char) # 遍历字符串并加入非重复字符的列表 new_s = "".join(new_s) # 转换为字符 print(new_s)
3、返回第一个重复的字符的下标
s = "hello world" new_s = [] for char in s: if char not in new_s: new_s.append(char) # 遍历字符串并加入非重复字符的列表 else: print(char,s.index(char)) break