字符串去重的五种方法

# 方法1、for循环字符串去重
name='周吴王赵黄吴杨王李张陈杨侯'
newname=''
for i in name:
    if i not in newname:
        newname=i+newname
print(newname)
打印结果:
侯陈张李杨黄赵王吴周
name='周吴王赵黄吴杨王李张陈杨侯'
newname=''
i = len(name)-1
while True:
    if i>=0:
        if name[i] not in newname:
            newname+=(name[i])
            print(newname)
            i-=1
        else:
            break
print('*'*20)
print(newname)
打印结果:

侯杨
侯杨陈
侯杨陈张
侯杨陈张李
侯杨陈张李王
********************
侯杨陈张李王
# 方法3使用列表的方法
name='周吴王赵黄吴杨王李张陈杨侯'
print('打印原name:',name)
newname=set(name)
print('打印newname:',newname)
newname1=list(newname)
print('将集合转换为列表:',newname1)
print(' '.join(newname1))
print('*'*20)
newname1.sort(key=name.index)  #sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。
print(' '.join(newname1))
打印结果:
打印原name: 周吴王赵黄吴杨王李张陈杨侯
打印newname: {'赵', '王', '周', '黄', '杨', '李', '吴', '张', '陈', '侯'}
将集合转换为列表: ['赵', '王', '周', '黄', '杨', '李', '吴', '张', '陈', '侯']
赵 王 周 黄 杨 李 吴 张 陈 侯
*****************
周 吴 王 赵 黄 杨 李 张 陈 侯
# 方法4:在原字符串中直接删除
name='周吴王赵黄吴杨王李张陈杨侯'
l=len(name)
for s in name:
    print(s)
    if name[0] in name [1:l]:
        name = name[1:l]
    else:
        name=name[1:l]+name[0]
print(name)
打印结果:
D:\python3.6.5\python.exe C:/Users/issuser/PycharmProjects/untitled/yinchengPython/EXE/去重.py













周赵黄吴王李张陈杨侯
# 方法5:fromkeys()方法是把字符串str转成字典
name='周吴王赵黄吴杨王李张陈杨侯'
zd={}.fromkeys(name)            # fromkeys() 函数用于创建一个新字典,以序列 seq 中元素做字典的键,value 为字典所有键对应的初始值
print(zd)
mylist=list(zd.keys())
print(mylist)
print(' '.join(mylist))          #返回通过指定字符连接序列中元素后生成的新字符串
{'周': None, '吴': None, '王': None, '赵': None, '黄': None, '杨': None, '李': None, '张': None, '陈': None, '侯': None}
['周', '吴', '王', '赵', '黄', '杨', '李', '张', '陈', '侯']
周 吴 王 赵 黄 杨 李 张 陈 侯
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
除了使用哈希表和双指针的方法,还可以使用其他方法来实现字符串去重。下面介绍几种常见的方法: 1. 排序去重法 将字符串排序后,遍历字符串中的每个字符,如果该字符不等于前一个字符,则将该字符加入结果字符串中。这种方法的时间复杂度为 O(nlogn),空间复杂度为 O(n)。 代码如下: ```c++ #include <iostream> #include <string> #include <algorithm> using namespace std; string remove_duplicate_chars(string s) { sort(s.begin(), s.end()); string result; for (int i = 0; i < s.size(); i++) { if (i == 0 || s[i] != s[i-1]) { result += s[i]; } } return result; } int main() { string s = "abcaabbcc"; string result = remove_duplicate_chars(s); cout << result << endl; // 输出:abc return 0; } ``` 2. 暴力枚举法 遍历字符串中的每个字符,判断该字符在字符串中是否重复出现,如果没有则将该字符加入结果字符串中。这种方法的时间复杂度为 O(n^2),空间复杂度为 O(n)。 代码如下: ```c++ #include <iostream> #include <string> using namespace std; string remove_duplicate_chars(string s) { string result; for (int i = 0; i < s.size(); i++) { bool found = false; for (int j = 0; j < i; j++) { if (s[j] == s[i]) { found = true; break; } } if (!found) { result += s[i]; } } return result; } int main() { string s = "abcaabbcc"; string result = remove_duplicate_chars(s); cout << result << endl; // 输出:abc return 0; } ``` 3. 位图法 使用一个长度为 256 的布尔型数组,将字符串中每个字符的 ASCII 码作为数组下标,并将对应的数组元素置为 true。这种方法的时间复杂度为 O(n),空间复杂度为 O(1)。 代码如下: ```c++ #include <iostream> #include <string> using namespace std; string remove_duplicate_chars(string s) { bool bitmap[256] = {false}; string result; for (int i = 0; i < s.size(); i++) { int index = (int)s[i]; if (!bitmap[index]) { bitmap[index] = true; result += s[i]; } } return result; } int main() { string s = "abcaabbcc"; string result = remove_duplicate_chars(s); cout << result << endl; // 输出:abc return 0; } ``` 需要注意的是,这种方法只能处理 ASCII 码范围内的字符,无法处理 Unicode 码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值