646

来源:互联网 发布:网络怎么走电线 编辑:程序博客网 时间:2024/06/15 00:46

5.15

新加的这几道题,简直就是用来增加自信心的啊。

基本都是用来判断一个字符串中相同的字符的,利用HashMap解决的很是开心呢。

public class Solution {    /**     * @param s a string     * @return it's index     */    public int firstUniqChar(String s) {        // Write your code here        int length = s.length();        HashMap<Character,Integer> map = new HashMap<Character,Integer>();         for(int i = 0; i < length; i++){              if(!map.containsKey(s.charAt(i))){                  map.put(s.charAt(i),1);              }              else{                map.put(s.charAt(i),map.get(s.charAt(i)) +1);            }        }        for(int i = 0; i < length; i++){              if(map.get(s.charAt(i)) == 1){                return i;            }          }                return -1;            }}


0 0
原创粉丝点击