[LeetCode]--387. First Unique Character in a String

简介: Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.Not

Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

Note: You may assume the string contain only lowercase letters.

用数组,简单粗暴,把字符个数都记录下来,最后遍历一下原来的字符串,判断再这个数组里面的个数就行。

public int firstUniqChar1(String s) {
        if (s == null || s.length() == 0)
            return -1;
        int[] count = new int[256];

        for (int i = 0; i < s.length(); i++) {
            int index = s.charAt(i) - 'a';
            count[index]++;
        }

        for (int i = 0; i < s.length(); i++) {
            int index = s.charAt(i) - 'a';
            if (count[index] == 1)
                return i;
        }
        return -1;
    }

利用Map来记录。

public int firstUniqChar(String s) {
        if (s == null || s.length() == 0) {
            return -1;
        }

        HashMap<Character, Integer> hashmap = new HashMap<Character, Integer>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (hashmap.containsKey(c)) {
                hashmap.put(c, hashmap.get(c) + 1);
            } else {
                hashmap.put(c, 1);
            }
        }

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (hashmap.get(c) == 1) {
                return i;
            }
        }
        return -1;
    }

利用一个List一个Map来记录。用更多的空间来换取时间。

public int firstUniqChar(String s) {  
            char[] cs=s.toCharArray();  
            List<Character> list=new ArrayList<Character>();  
            Map<Character,Integer> map=new HashMap<Character,Integer>();  
            for (int i = 0; i < cs.length; i++) {  
                Character c=cs[i];  
                if (map.containsKey(c)) {  
                    list.remove(c);  
                }else{  
                    list.add(c);  
                    map.put(c,i);  
                }  
            }  
            return list.size()==0?-1:map.get(list.get(0));  
        }  
目录
相关文章
|
算法 C++
【LeetCode】【C++】string OJ必刷题
【LeetCode】【C++】string OJ必刷题
218 0
|
JSON Java 关系型数据库
Java更新数据库报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
在Java中,使用mybatis-plus更新实体类对象到mysql,其中一个字段对应数据库中json数据类型,更新时报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
1245 4
Java更新数据库报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
|
机器学习/深度学习 canal NoSQL
从C语言到C++_12(string相关OJ题)(leetcode力扣)
从C语言到C++_12(string相关OJ题)(leetcode力扣)
142 0
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
Java
Leetcode 467. Unique Substrings in Wraparound String
大概翻译下题意,有个无限长的字符串s,是由无数个「abcdefghijklmnopqrstuvwxy」组成的。现在给你一个字符串p,求多少个p的非重复子串在s中出现了?
140 0
|
算法 索引
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
【LeetCode】string 类的几道简单题
LeetCode 438. Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter.
204 0
LeetCode 438. Find All Anagrams in a String
|
存储 C语言 C++
Leetcode17. 电话号码的字母组合:递归树深度遍历(C++vector和string的小练习)
Leetcode17. 电话号码的字母组合:递归树深度遍历(C++vector和string的小练习)
|
存储 canal 算法
leetcode:43. 字符串相乘(附加一些C++string其他小练习)
leetcode:43. 字符串相乘(附加一些C++string其他小练习)
|
机器学习/深度学习 NoSQL 算法
LeetCode 344. 反转字符串 Reverse String
LeetCode 344. 反转字符串 Reverse String

热门文章

最新文章