在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).
public static int FirstNotRepeatingChar(String str) {
if(str == null || str.length() == 0)
return -1 ;
int b = 0 ;
if(str.length() == 1)
return 1 ;
char a[] = str.toCharArray();
for(int i = 0 ; i < a.length ; i++) {
for(int j = 0 ; j < a.length ; j++) {
if(a[i] == a[j])
b++ ;
}
if(b == 1)
return i ;
else b = 0 ;
}
return 0 ;
}
请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。
import java.util.*;
public class Solution {
char a[] = new char[256] ;
StringBuffer sb =new StringBuffer() ;
public void Insert(char ch)
{
++a[ch] ;
sb.append(ch+"") ;
}
public char FirstAppearingOnce()
{
char b[] = sb.toString().toCharArray() ;
for(int i = 0 ; i < b.length ; i++){
if(a[b[i]] == 1)
return b[i] ;
}
return '#' ;
}
}