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

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

LeetCode387—First Unique Character in a String

LeetCode387 First Unique Character in A String
  • zhangxiao93
  • zhangxiao93
  • 2016年08月26日 21:46
  • 898

387. First Unique Character in a String --Python

387. First Unique Character in a String Given a string, find the first non-repeating character in i...
  • Chris_zhangrx
  • Chris_zhangrx
  • 2018年01月22日 22:09
  • 46

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

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

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...
  • Al_xin
  • Al_xin
  • 2016年08月22日 22:18
  • 513

【python】【leetcode】【算法题目387—First Unique Character in a String】

一、题目描述 题目原文: Given a string, find the first non-repeating character in it and return it's in...
  • u014615155
  • u014615155
  • 2016年12月04日 14:18
  • 337

387. First Unique Character

leetcode编程
  • qq_33530388
  • qq_33530388
  • 2017年09月29日 14:47
  • 120

387. First Unique Character in a String Java

1、题目来源:点击打开链接 2、题目: Given a string, find the first non-repeating character in it and return it...
  • tzh476
  • tzh476
  • 2016年09月04日 18:37
  • 449

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...
  • ajiangfan
  • ajiangfan
  • 2016年09月22日 11:18
  • 778

LeetCode 387. First Unique Character in a String

描述找出字符串中第一个只出现过一次的字符解决利用数组存每个字符出现的次数,然后遍历判断class Solution { public: int firstUniqChar(string s) ...
  • MrSiz
  • MrSiz
  • 2016年09月13日 18:15
  • 160

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...
  • u012655441
  • u012655441
  • 2017年04月09日 15:27
  • 110
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:387. First Unique Character in a String#2(Done)
举报原因:
原因补充:

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