友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

387. First Unique Character in a String

还是要善用 Map 来统计字符数量!

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.

思路分析

通过解法是再次遍历字符串,思考如何遍历字母表来获取第一个唯一字符的位置。

  • 一刷

  • 二刷

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * Runtime: 34 ms, faster than 54.19% of Java online submissions for First Unique Character in a String.
 *
 * Memory Usage: 37.4 MB, less than 100.00% of Java online submissions for First Unique Character in a String.
 *
 * Copy from: https://leetcode.com/problems/first-unique-character-in-a-string/solution/[First Unique Character in a String solution - LeetCode]
 *
 * @author D瓜哥 · https://www.diguage.com
 * @since 2020-01-11 10:48
 */
public int firstUniqChar(String s) {
    if (Objects.isNull(s) || s.length() == 0) {
        return -1;
    }
    char[] chars = s.toCharArray();
    Map<Character, Integer> charToCountMap = new HashMap<>();
    for (char aChar : chars) {
        Integer count = charToCountMap.getOrDefault(aChar, 0);
        charToCountMap.put(aChar, ++count);
    }
    for (int i = 0; i < chars.length; i++) {
        if (charToCountMap.get(chars[i]) == 1) {
            return i;
        }
    }
    return -1;
}
/**
 * Runtime: 53 ms, faster than 10.48% of Java online submissions for First Unique Character in a String.
 *
 * Memory Usage: 38.4 MB, less than 96.43% of Java online submissions for First Unique Character in a String.
 */
public int firstUniqCharArray(String s) {
    if (Objects.isNull(s) || s.length() == 0) {
        return -1;
    }
    char[] chars = s.toCharArray();
    boolean[] unique = new boolean[chars.length];
    Arrays.fill(unique, true);
    for (int i = 0; i < chars.length; i++) {
        if (!unique[i]) {
            continue;
        }
        for (int j = i + 1; j < chars.length; j++) {
            if (!unique[j]) {
                continue;
            }
            if (chars[i] == chars[j]) {
                unique[i] = false;
                unique[j] = false;
            }
        }
    }
    for (int i = 0; i < unique.length; i++) {
        if (unique[i]) {
            return i;
        }
    }
    return -1;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/**
 * @author D瓜哥 · https://www.diguage.com
 * @since 2020-01-11 10:48
 */
public int firstUniqChar(String s) {
  int[] cnt = new int[26];
  for (int i = 0; i < s.length(); i++) {
    int idx = s.charAt(i) - 'a';
    cnt[idx]++;
  }
  for (int i = 0; i < s.length(); i++) {
    int idx = s.charAt(i) - 'a';
    if (cnt[idx] == 1) {
      return i;
    }
  }
  return -1;
}