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

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
• 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
• 2018年01月22日 22:09
• 46

## LeetCode之路：387. First Unique Character in a String

LeetCode之路：387. First Unique Character in a String 一、引言这道题题意非常简单，却让我思考了很久，或许难的并不是这道题，是纠结于解题思路的那种困扰吧。...
• 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
• 2016年08月22日 22:18
• 513

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

• u014615155
• 2016年12月04日 14:18
• 337

## 387. First Unique Character

leetcode编程
• 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
• 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
• 2016年09月22日 11:18
• 778

## LeetCode 387. First Unique Character in a String

• 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
• 2017年04月09日 15:27
• 110

举报原因： 您举报文章：387. First Unique Character in a String#2(Done) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)