387. First Unique Character in a String#2(Done)

原创 2017年01月03日 22:08:28

Solution#2

public class Solution {
    public int firstUniqChar(String s) {
        if (s == null || s.length() == 0) 
            return -1;
        if (s.length() == 1)
            return 0;
        int[] letters = new int[26];
        for (int i = 0; i < 26; i++) {
            letters[i] = 0;
        }
        char[] c = s.toCharArray();
        int slow = 0, fast = 1;
        letters[c[slow] - 'a'] = 1;
        while (fast < c.length) {
            while (c[fast] != c[slow]) {
                letters[c[fast++] - 'a']++;
                if (fast == c.length)
                    return slow;
            }
            letters[c[fast] - 'a']++;
            while (++slow < c.length) {
                if (letters[c[slow] - 'a'] <= 1) {
                    letters[c[slow] - 'a']++;
                    break;
                }
            }
            if (fast < slow)
                fast = slow + 1;
            else
                fast++;
        }
        return slow == c.length ? -1 : slow;
    }
}

Solution#1

public class Solution {
    public int firstUniqChar(String s) {
        if (s == null || s.length() == 0) 
            return -1;
        int[] position = new int[26];
        for (int i = 0; i < 26; i++) {
            position[i] = -1;
        }
        boolean[] notUnique = new boolean[26];
        for (int i = 0; i < s.length(); i++) {
            int index = s.charAt(i) - 'a';
            if (position[index] == -1) {
                position[index] = i;
            } else {
                notUnique[index] = true;
            }
        }
        int min = -1;
        for (int i = 0; i < 26; i++) {
            if (!notUnique[i]) {
                if (min == -1 || min > position[i] && position[i] != -1)
                    min = position[i];
            }
        }
        return min;
    }
}

Problem#2

*改用双指针法,并用数组代替hash表,效率大大增加(97.15%)

Problem#1

  • 双指针法更快,需捋清思路
版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode 387. First Unique Character in a String 解题报告

LeetCode 387. First Unique Character in a String 解题报告

LeetCode-387. First Unique Character in a String (Java)

Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex...
  • NOadu
  • NOadu
  • 2017年06月25日 09:58
  • 234

LeetCode之路:387. First Unique Character in a String

LeetCode之路:387. First Unique Character in a String 一、引言这道题题意非常简单,却让我思考了很久,或许难的并不是这道题,是纠结于解题思路的那种困扰吧。...

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 ex...

LeetCode笔记:387. First Unique Character in a String

找到一个字符串中第一个只出现一次的字母

Leetcode 387. First Unique Character in a String 第一个唯一字符 解题报告

1 解题思想这题的意思是,给一个字符串,找出第一个只出现一次的字符的位置。这道题限定了只有小写字符,所以用一个小写字符的数组去做标志位,记录某个字符是否出现,如果出现了一次那么位置在哪里,或者出现了多...
  • MebiuW
  • MebiuW
  • 2016年08月24日 13:02
  • 1335

关于LeetCode中First Unique Character in a String一题的理解

题目如下: Given a string, find the first non-repeating character in it and return it's index. If it do...

LeetCode - 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’...

End of string character in Java

原文转自:http://stackoverflow.com/questions/17815755/end-of-string-character-in-java Stack Over...

Detect Chinese character through out a string in Actionscript3.0

Base on the content of the references, just do a simple test, on a fla's timeline, add the code: ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:387. First Unique Character in a String#2(Done)
举报原因:
原因补充:

(最多只允许输入30个字)