String去除重复字符两个方法
package cn.aresoft;
import java.util.ArrayList;
import java.util.List;
public class TestBasic {
public static void main(String[] args) {
String str = "kjkljklhuionsd";
System.out.println(sub(str));
System.out.println(sub1(str));
}
// 方法一:
static String sub(String str) {
StringBuffer result = new StringBuffer();
List list = new ArrayList();
char[] cs = str.toCharArray();
for (int i = 0; i < cs.length; i++) {
if (!list.contains(cs[i])) {
result.append(cs[i]);
list.add(cs[i]);
}
}
return result.toString();
}
// 方法二:
static String sub1(String str) {
List list = new ArrayList();
StringBuffer sb = new StringBuffer(str);
int j = 0;
for (int i = 0; i < str.length(); i++) {
if (list.contains(str.charAt(i))) {
sb.deleteCharAt(i - j); // String 是没有delete方法的
j++; // 因为删除了sb中的字符,有一个偏移
} else {
list.add(str.charAt(i));
}
}
return sb.toString();
}
}
String去除重复字符两个方法的更多相关文章
- string [] 去除重复字符两个方法
不废话直接看图 结果 代码: this.txtListHTML.Text = String.Join(",", list.Replace("\r\n", &qu ...
- Money类型转化为String去除小数点后0解决方法
Money类型转化为String去除小数点后0从数据库提取Money类型后,字符串如:1212.0000 如何使其成为1212 注:去掉了小数点 如果是:1212.0100 使 ...
- ArrayList去除重复元素(多种方法实现)
package other; import java.util.ArrayList; import java.util.HashSet; public class test4 { public sta ...
- [LeetCode] Repeated String Match 重复字符串匹配
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...
- Javascript一句代码实现JS字符串去除重复字符
需求: 原字符串:abcdabecd 去重后字符串:abcde JS字符串去重,一个简单需求,网上找案例发现都是一大堆代码,对于强迫症的我 实再无法忍受,于是自己手动写出一段代码,完美解决该问题. 代 ...
- C# char[]与string互相转换的两种方法
1.string转换为char[]:char[] string.ToCharArray(); static void Main(string[] args) { s ...
- Java中List集合去除重复数据的六种方法
1. 循环list中的所有元素然后删除重复 public static List removeDuplicate(List list) { for ( int i = 0 ; i < list. ...
- [LeetCode] 686. Repeated String Match 重复字符串匹配
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...
- sql去除重复的几种方法
所以用这样一句SQL就可以去掉重复项了: select * from msg group by terminal_id; SQL中distinct的用法(四种示例分析) 示例1 select dist ...
随机推荐
- HTML导航 - 点击更改背景
步骤一: 在须要添加效果的<li>标签中添加onclick事件:<li onclick="setcurrent(this)"> 步骤二: 加入JS代码: f ...
- java多线程设置优先级
package com.itbuluoge.mythread; class SimpleThread extends Thread { private int priority; public Sim ...
- ORACLE-017:SQL优化-is not null和nvl
今天在优化一段sql,原脚本大致例如以下: select a.字段n from tab_a a where a.字段2 is not null; a.字段2添加了索引的,可是查询速度很慢. 于是做了例 ...
- CreateWindowEx和CreateWindow的区别
CreateWindowEx 函数功能:该函数创建一个具有扩展风格的重叠式窗口.弹出式窗口或子窗口,其他与 CreateWindow函数相同.关于创建窗口和其他参数的内容,请参看CreateWindo ...
- Android之——常见Bug及其解决方式
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46942139 1.android.view.WindowManager$BadTo ...
- Android WiFi/WiFi热点开发总结
首先看一下WiFi的自我介绍: Wi-Fi是一种允许电子设备连接到一个无线局域网(WLAN)的技术,通常使用2.4G UHF或5G SHF ISM 射频频段.连接到无线局域网通常是有密码保护的:但也可 ...
- P2932 [USACO09JAN]地震造成的破坏Earthquake Damage 爆搜
这题怎么这么水~~~本来以为挺难的一道题,结果随便一写就过了...本来还不知道损坏的牛棚算不算,结果不明不白就过了... 题干: 农夫John的农场遭受了一场地震.有一些牛棚遭到了损坏,但幸运地,所有 ...
- LuoguP3621 [APIO2007]风铃
https://zybuluo.com/ysner/note/1140124 题面 题面复杂,戳我 解析 看着这道题... 似乎与[HNOI/AHOI2018]道路有不可言妙的相似之处. (题面吓人, ...
- 数据结构C++,线性表的实现
#include <iostream>#include <sstream>#include <fstream>#include <cmath>#incl ...
- php的self和this
<?phpclass ss{ public static $code; public function __construct( $code ) { self::$code=$code; //这 ...