查找字符串中第一个非重复字符

 

题目描述:编写一个高效函数,找到字符串中首个非重复字符。如"total"首个非重复字符为'o',"teeter"为'r'。(时间复杂度最好为o(n))。代码如下:

 

Java代码 复制代码
  1. package cn.lifx.test;   
  2.   
  3. import java.util.Hashtable;   
  4.   
  5. public class FindFirstUnique    
  6. {   
  7.     private static final int N = 26;   
  8.     private static final String letters = "abcdefghigklmnopqrstuvwxyz";   
  9.        
  10.     public static void main(String[] args)   
  11.     {   
  12.         String str = "totalo";   
  13.            
  14.         findFirstUnique(str);   
  15.            
  16.         findFirstUnique2(str);   
  17.            
  18.         findFirstUnique3(str);   
  19.     }   
  20.        
  21.     public static void findFirstUnique(String str)   
  22.     {   
  23.         char ch = 'a';   
  24.         int index = 0;   
  25.         int[] a = new int[N];   
  26.            
  27.         for(int i=0; i<a.length; i++)   
  28.             a[i] = 0;   
  29.            
  30.         for(int i=0; i<str.length(); i++)   
  31.         {   
  32.             ch = str.charAt(i);   
  33.                
  34.             index = 25 - 'z' + ch;   
  35.                
  36.             a[index]++;   
  37.         }   
  38.            
  39.         int min = Integer.MAX_VALUE;   
  40.         for(int i=0; i<a.length; i++)   
  41.         {   
  42.             if(a[i] == 1)   
  43.             {   
  44.                 ch = letters.charAt(i);   
  45.                 index = str.indexOf(ch);   
  46.                    
  47.                 if(index < min)   
  48.                     min = index;   
  49.             }   
  50.         }   
  51.            
  52.         if(min < Integer.MAX_VALUE)   
  53.         {   
  54.             System.out.println(str.charAt(min));   
  55.         }   
  56.         else  
  57.         {   
  58.             System.out.println("字符串中没有唯一不同的字符!");   
  59.         }   
  60.     }   
  61.        
  62.     public static void findFirstUnique2(String str)   
  63.     {   
  64.         char ch;   
  65.         int index;   
  66.         int lastindex;   
  67.            
  68.         for(int i=0; i<str.length(); i++)   
  69.         {   
  70.             ch = str.charAt(i);   
  71.             index = str.indexOf(ch);   
  72.             lastindex = str.lastIndexOf(ch);   
  73.                
  74.             if(index == lastindex)   
  75.             {   
  76.                 System.out.println(ch);   
  77.                 break;   
  78.             }   
  79.         }   
  80.     }   
  81.        
  82.     public static void findFirstUnique3(String str)   
  83.     {    
  84.         Character c;    
  85.         Object seenOnce = new Object();    
  86.         Object seenTwice = new Object();    
  87.            
  88.         Hashtable<Character, Object> charHash = new Hashtable<Character, Object>();    
  89.   
  90.         for(int i=0; i<str.length(); i++)   
  91.         {    
  92.             c = new Character(str.charAt(i));    
  93.             Object object = charHash.get(c);    
  94.             if(object == null)   
  95.             {    
  96.                 charHash.put(c, seenOnce);    
  97.             }    
  98.             else if(object.equals(seenOnce))   
  99.             {    
  100.                 charHash.put(c, seenTwice);    
  101.             }    
  102.         }    
  103.   
  104.         for(int i=0; i<str.length(); i++)   
  105.         {    
  106.             c = new Character(str.charAt(i));    
  107.             if(charHash.get(c)==seenOnce)   
  108.             {    
  109.                 System.out.println(c);   
  110.                 break;   
  111.             }    
  112.         }    
  113.     }    
  114. }  
package cn.lifx.test;

import java.util.Hashtable;

public class FindFirstUnique 
{
	private static final int N = 26;
	private static final String letters = "abcdefghigklmnopqrstuvwxyz";
	
	public static void main(String[] args)
	{
		String str = "totalo";
		
		findFirstUnique(str);
		
		findFirstUnique2(str);
		
		findFirstUnique3(str);
	}
	
	public static void findFirstUnique(String str)
	{
		char ch = 'a';
		int index = 0;
		int[] a = new int[N];
		
		for(int i=0; i<a.length; i++)
			a[i] = 0;
		
		for(int i=0; i<str.length(); i++)
		{
			ch = str.charAt(i);
			
			index = 25 - 'z' + ch;
			
			a[index]++;
		}
		
		int min = Integer.MAX_VALUE;
		for(int i=0; i<a.length; i++)
		{
			if(a[i] == 1)
			{
				ch = letters.charAt(i);
				index = str.indexOf(ch);
				
				if(index < min)
					min = index;
			}
		}
		
		if(min < Integer.MAX_VALUE)
		{
			System.out.println(str.charAt(min));
		}
		else
		{
			System.out.println("字符串中没有唯一不同的字符!");
		}
	}
	
	public static void findFirstUnique2(String str)
	{
		char ch;
		int index;
		int lastindex;
		
		for(int i=0; i<str.length(); i++)
		{
			ch = str.charAt(i);
			index = str.indexOf(ch);
			lastindex = str.lastIndexOf(ch);
			
			if(index == lastindex)
			{
				System.out.println(ch);
				break;
			}
		}
	}
	
	public static void findFirstUnique3(String str)
	{ 
        Character c; 
        Object seenOnce = new Object(); 
        Object seenTwice = new Object(); 
        
        Hashtable<Character, Object> charHash = new Hashtable<Character, Object>(); 

        for(int i=0; i<str.length(); i++)
        { 
        	c = new Character(str.charAt(i)); 
            Object object = charHash.get(c); 
            if(object == null)
            { 
            	charHash.put(c, seenOnce); 
            } 
            else if(object.equals(seenOnce))
            { 
            	charHash.put(c, seenTwice); 
            } 
        } 

        for(int i=0; i<str.length(); i++)
        { 
        	c = new Character(str.charAt(i)); 
            if(charHash.get(c)==seenOnce)
            { 
            	System.out.println(c);
            	break;
            } 
        } 
	} 
}

 

第一种方法不通用,或者说有问题:对于大写字母就会出错了,另外对于其他字符比如#$%等也会出错。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值