在字符串 s 中找出第一个只出现一次的字符。如果没有,返回一个单空格。 s 只包含小写字母。
示例:
s = "abaccdeff" 返回 "b" s = "" 返回 " "
限制:
0 <= s 的长度 <= 50000
//思路:第一个唯一字符,必然是从前往后和从后往前找首次出现都是自己的下标
1 class Solution { 2 public char firstUniqChar(String s) { 3 int len=s.length(); 4 for (int i = 0; i < len; i++) { 5 char c=s.charAt(i); 6 if(i==s.indexOf(c)&&i==s.lastIndexOf(c)){ 7 return c; 8 } 9 } 10 return ‘ ‘; 11 } 12 }
原文:https://www.cnblogs.com/SEU-ZCY/p/14638500.html