第一个只出现一次的字符
题目:在字符串中找出第一个只出现一次的字符。如果输入“abaccdeff”,则输出‘b’
方法一
public class FirstNotRepeatChar_1 {
public static void main(String[] args){
String str="abcbwaccdeff";
char ch= returnFirstChar(str);
System.out.println(ch);
}
public static char returnFirstChar(String str){
char [] ch=str.toCharArray();
//char c;
int i;
for(i=0;i<ch.length;i++){
boolean flag =false;
for(int j=i+1;j<ch.length;j++){
if(ch[i]==ch[j])
break;
//当ch[i]遍历了后面所有的字符后,则就找到了第一次出现的字符且出现一次
if(ch[i]!=ch[j] && j==ch.length-1){
//c=ch[i];
flag=true;
break;
//return c;
}
}
if(flag)
break;
}
return ch[i];
}
}
方法二:
import java.util.Scanner;
public class FirstNotRepeatChar_2 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNextLine()){
String str1=sc.nextLine();
String str2=firstNotRepeatChar(str1);
System.out.println(str2);
}
sc.close();
}
public static String firstNotRepeatChar(String str){
for(int i=0;i<str.length();i++){
String str1=str.replaceAll(str.substring(i,i+1), "");//遍历字符串中的字符,直接调用该方法,不需要转换成字符数组,然后在写循环
if((str.length()-str1.length())==1)
return str.substring(i,i+1);
}
return "没找到";
}
}
方法三:
import java.util.Scanner;
public class FirstNotRepeatChar_3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str=input.next();
char index=FirstNotRepeatingChar(str);
System.out.println(index);
}
public static char FirstNotRepeatingChar(String str) {
int[] hash=new int[256];
int len=str.length();
for(int i=0;i<len;i++){
char temp=str.charAt(i);
hash[temp]++;
//System.out.print(hash[temp]++);
}
System.out.println();
int i;
for(i=0;i<len;i++){
char temp=str.charAt(i);
if(hash[temp]==1)
return temp;
}
return '0';
}
}
方法四:
import java.util.*;
public class FirstNotRepeatChar_4 {
public static void main(String[] str){
Scanner sc=new Scanner(System.in);
while(sc.hasNextLine()){
String str1=sc.nextLine();
char ch = firstNotRepeatChar(str1);
System.out.println(ch);
}
}
public static Character firstNotRepeatChar(String str){
if(str==null||str.length()==0)
return null;
char [] ch =str.toCharArray();
LinkedHashMap<Character,Integer> hash=new LinkedHashMap<Character,Integer> ();
//先把字符数组ch中的一个字符赋值给temp
for(char temp :ch ){
//一开始集合LinkedHashMap默认初始化,判断集合中是否含有temp
if(hash.containsKey(temp))
//如果集合中已经含有temp,则把之前的键值对所对应的值加上1;
hash.put(temp, hash.get(temp)+1);
else
//集合中没有temp,则往集合中存储键值对
hash.put(temp, 1);
}
//取出键所对应值为1的字符
for(char key :hash.keySet()){
if(hash.get(key)==1)
return key;
}
return null;
}
}
方法五:
import java.util.Scanner;
public class FirstNotRepeatChar_5 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNextLine()){
String str1=sc.nextLine();
String str2=firstNotRepeatChar(str1);
System.out.println(str2);
}
sc.close();
}
public static String firstNotRepeatChar(String str) throws RuntimeException
{
//易错:str表示的是字符串的地址值,如果输入null,表示字符串的地址值有值,其地址对应的字符串是“null”,当字符串输入null,输出为n
if(str==null||str.length()==0)
return null;
String firstChar=str.substring(0,1);
String replace =str.replaceAll(firstChar, "");
if(str.length()-replace.length()!=1)
{
System.out.println("firstChar: "+firstChar+";replaceChar :"+ replace);
firstChar =firstNotRepeatChar(replace);
}
return firstChar;
}
}