第一个只出现一次的字符

第一个只出现一次的字符

题目:在字符串中找出第一个只出现一次的字符。如果输入“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;
				
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值