public class Zhangchibang311 { public static Character process(String text) { Set<Character> set = new HashSet<Character>(); for (int i = 0; i < text.length(); i++) { Character ch = Character.valueOf(text.charAt(i)); if (!set.contains(ch)) { int nextPos = text.indexOf(ch.charValue(), i + 1); if (nextPos == -1) { return ch; } set.add(ch); } } return null; } public static void main(String[] args) { String a = "teeter"; Character ax = process(a); System.out.println(ax != null ? String.valueOf(ax) : "not found"); String b = "teeterh"; Character bx = process(b); System.out.println(bx != null ? String.valueOf(bx) : "not found"); } }
package resume; public class NotRepeatChar { /** * 用java实现 查找字符串中第一个没有重复出现的字符 */ public static void main(String[] args) { String str = "erabachhcgbceff"; int[] ins = new int[str.length()]; //数组与成员变量一样,会有默认零 //用相同长度的数组标识该位对应字符是否已被比较,是则赋为1 char c =' '; for(int i=0;i<str.length();i++){ if(ins[i]==0){ // 前面没有重复字符, 需要与之后的字符比较 boolean flag = false; for(int j=i+1;j<str.length();j++){ if(str.charAt(i)==str.charAt(j)){ ins[j]=1; flag = true; } } if(!flag){ //没有相等的字符 c = str.charAt(i); break; }
} } if(c==' '){ System.out.println("There is no char not repeated! "); }else{ System.out.println("the first notRepeated char is :"+c); } }} 这是我刚才写的实现。 功能好像是实现了。不过我还没有正式做过程序员,所以解决方案可能不是很好。 仅做参考吧。
public class Zhangchibang311 { public static int process(String text) { for (int i = 0; i < text.length(); i++) { char ch = text.charAt(i); int lastPos = -1; if (i > 0) lastPos = text.lastIndexOf(ch, i - 1); int nextPos = text.indexOf(ch, i + 1); if (lastPos < 0 && nextPos < 0) { return i; } } return -1; } public static void main(String[] args) { String a = "teeterr"; int ai = process(a); System.out.println(ai >= 0 ? a.substring(ai, ai + 1) : "not found"); String b = "teeterh"; int bi = process(b); System.out.println(bi >= 0 ? b.substring(bi, bi + 1) : "not found"); } }
String in = ""; //这个自己输入 char c = ' '; char ch; for (int j = 0; j < in.length(); j++) { ch = in.charAt(j); if (in.indexOf(ch) == in.lastIndexOf(ch)) { c = ch; break; } } System.out.println(c==' '?"not found":c);
如果是“teeterh” 还是输出“r”没有重复出现的字符中的第一个
public class Zhangchibang311 { public static Character process(String text) {
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < text.length(); i++) {
Character ch = Character.valueOf(text.charAt(i));
if (!set.contains(ch)) {
int nextPos = text.indexOf(ch.charValue(), i + 1);
if (nextPos == -1) {
return ch;
}
set.add(ch);
}
}
return null;
} public static void main(String[] args) {
String a = "teeter";
Character ax = process(a);
System.out.println(ax != null ? String.valueOf(ax) : "not found"); String b = "teeterh";
Character bx = process(b);
System.out.println(bx != null ? String.valueOf(bx) : "not found");
}
}
public class NotRepeatChar { /**
* 用java实现 查找字符串中第一个没有重复出现的字符
*/
public static void main(String[] args) {
String str = "erabachhcgbceff";
int[] ins = new int[str.length()]; //数组与成员变量一样,会有默认零
//用相同长度的数组标识该位对应字符是否已被比较,是则赋为1
char c =' ';
for(int i=0;i<str.length();i++){
if(ins[i]==0){ // 前面没有重复字符, 需要与之后的字符比较
boolean flag = false;
for(int j=i+1;j<str.length();j++){
if(str.charAt(i)==str.charAt(j)){
ins[j]=1;
flag = true;
}
}
if(!flag){ //没有相等的字符
c = str.charAt(i);
break;
}
}
}
if(c==' '){
System.out.println("There is no char not repeated! ");
}else{
System.out.println("the first notRepeated char is :"+c);
}
}}
这是我刚才写的实现。 功能好像是实现了。不过我还没有正式做过程序员,所以解决方案可能不是很好。 仅做参考吧。
if((str.indexof(str.charat(i),i+1)==-1){
system.out.println(str.charat(i));
}
}
for(int i=0;i <str.length;i++){
if((str.indexof(str.charat(i),i+1)==-1){
system.out.println(str.charat(i));
break;//
}
}
public class Zhangchibang311 { public static int process(String text) {
for (int i = 0; i < text.length(); i++) {
char ch = text.charAt(i);
int lastPos = -1;
if (i > 0) lastPos = text.lastIndexOf(ch, i - 1);
int nextPos = text.indexOf(ch, i + 1);
if (lastPos < 0 && nextPos < 0) {
return i;
}
}
return -1;
} public static void main(String[] args) {
String a = "teeterr";
int ai = process(a);
System.out.println(ai >= 0 ? a.substring(ai, ai + 1) : "not found"); String b = "teeterh";
int bi = process(b);
System.out.println(bi >= 0 ? b.substring(bi, bi + 1) : "not found");
}
}
char c = ' ';
char ch;
for (int j = 0; j < in.length(); j++) {
ch = in.charAt(j);
if (in.indexOf(ch) == in.lastIndexOf(ch)) {
c = ch;
break;
}
}
System.out.println(c==' '?"not found":c);
int i=0;
while(i<str.length()){
if((new String(str.substring(0, i)+str.substring(i+1))).indexOf(str.charAt(i))==-1)
return str.charAt(i);
i++;
}
return '9';
}
sharlist的算法也相当不错啊,学习了
不过好像if中多了个左括号
sharlist的算法也相当不错啊,学习了
不过好像if中多了个左括号
-------------------------------
不好意思,我是直接在回帖里面输入的,没有怎么检查.以后会注意的..