package 数据流中的中位数;
import java.util.ArrayList;
import java.util.HashMap;
//import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
public int FirstNotRepeatingChar(String str) {
// if(//str.length())
int len = str.length();
if(len == 0){
return -1;
}
//ArrayList<Integer> hashs = new ArrayList<Integer>();
HashMap<Character, Integer> nums = new HashMap<Character,Integer>();
HashMap<Character, Integer> pos = new HashMap<Character,Integer>();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
Integer val = nums.get(ch);
if(val == null){
nums.put(ch, 1);
pos.put(ch, i);
} else {
pos.put(ch, -1);
}
}
int res = 10001;
for(Character ch : pos.keySet()){
if(pos.get(ch).intValue() != -1){
res = Math.min(res, pos.get(ch));
}
}
if(res==10001) res=-1;
return res;
}
public static void main(String[] args) {
System.out.println(new Solution().FirstNotRepeatingChar("addddddffah"));
}
}
字符串中第一个字符出现的位置
最新推荐文章于 2023-06-14 02:50:02 发布