字符串中第一个字符出现的位置

来源:互联网 发布:苹果mac 笔记本电脑 编辑:程序博客网 时间:2024/03/29 14:23
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"));    }}
0 0
原创粉丝点击