@XQF
2018-03-07T22:59:25.000000Z
字数 1802
阅读 953
数据结构与算法
应该是要保留原数组元素的相对位置
,。,。,。选择排序类似的筛选法没问题。
public class Solution {
public String removeDuplicate(String string) {
if(string==null||string.length()==0){
return null;
}
char[] chars = string.toCharArray();
List<Character> charList = new ArrayList<>();
for (int i = 0; i < chars.length; i++) {
charList.add(chars[i]);
}
for (int i = 0; i < chars.length - 1; i++) {
for (int j = i + 1; j < chars.length; j++) {
if (chars[i] == chars[j]) {
charList.remove(j);
}
}
}
return charList.toString();
}
public static void main(String[] args) {
Solution solution = new Solution();
String string = "abac";
System.out.println(solution.removeDuplicate(string));
}
}
记录出现的字符
public class Solution {
public String removeDuplicate(String string) {
if (string == null || string.length() == 0) {
return null;
}
char[] chars = string.toCharArray();
List<Character> charList = new ArrayList<>();
for (int i = 0; i < chars.length; i++) {
charList.add(chars[i]);
}
int len = 'z' - 'A' + 1;
boolean[] book = new boolean[len];
int temp;
for (int i = 0; i < chars.length; i++) {
temp = chars[i] - 'A';
if (book[temp] == false) {
book[temp] = true;
} else {
charList.remove(i);
}
}
return charList.toString();
}
public static void main(String[] args) {
Solution solution = new Solution();
String string = "abac";
System.out.println(solution.removeDuplicate(string));
}
}
,,。,就是直接置为'',.,.,.,
public class Solution {
public String removeDuplicate(String string) {
if (string == null || string.length() == 0) {
return null;
}
char[] chars = string.toCharArray();
int len = 'z' - 'A' + 1;
boolean[] book = new boolean[len];
int temp;
for (int i = 0; i < chars.length; i++) {
temp = chars[i] - 'A';
if (book[temp] == false) {
book[temp] = true;
} else {
chars[i] = ' ';
}
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
if (chars[i] != ' ') {
sb.append(chars[i]);
}
}
return sb.toString();
}
public static void main(String[] args) {
Solution solution = new Solution();
String string = "abac";
System.out.println(solution.removeDuplicate(string));
}
}