leetcode:First Unique Character in a String
2016-08-28 14:11
411 查看
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.
找出一个串里面第一个不重复的字母的下标
简单模拟题目
定义一个字母数组,用于保存该字母第一次出现的下标
如果该字母没有出现,为Integer.MAX_VALUE - 1
该字母出现了多次,定义为Integer.MAX_VALUE
然后找出最小的值
Examples:
s = “leetcode”
return 0.
s = “loveleetcode”,
return 2.
找出一个串里面第一个不重复的字母的下标
简单模拟题目
定义一个字母数组,用于保存该字母第一次出现的下标
如果该字母没有出现,为Integer.MAX_VALUE - 1
该字母出现了多次,定义为Integer.MAX_VALUE
然后找出最小的值
public class Solution { public int firstUniqChar(String s) { int[] vis = new int[26]; Arrays.fill(vis, Integer.MAX_VALUE - 1); for(int i = 0; i < s.length(); ++i){ int index = s.charAt(i) - 'a'; if(vis[index] == Integer.MAX_VALUE)continue; else if(vis[index] == Integer.MAX_VALUE - 1){ vis[index] = i; }else{ vis[index] = Integer.MAX_VALUE; } } int min = Integer.MAX_VALUE - 2; for(int i : vis){ if(i < min)min = i; } return min == Integer.MAX_VALUE - 2 ? -1 : min; } }
相关文章推荐
- Leetcode算法比赛----First Unique Character in a String
- LeetCode: First Unique Character in a String
- Leetcode: First Unique Character in a String
- 【LeetCode-387】First Unique Character in a String(C++)
- [Leetcode]First Unique Character in a String
- Leetcode 387 First Unique Character in a String
- LeetCode-First Unique Character in a String
- leetcode---First Unique Character in a String
- LeetCode 387:First Unique Character in a String
- 【LeetCode】387 First Unique Character in a String(java实现)
- [LeetCode] First Unique Character in a String(Java)
- [LeetCode] First Unique Character in a String 字符串第一个不同字符
- [Leetcode] First Unique Character in a String
- leetcode387:First Unique Character in a String
- LeetCode(387)First Unique Character in a String
- 384.[LeetCode]First Unique Character in a String
- leetcode 387 First Unique Character in a String C++
- LeetCode:First Unique Character in a String
- leetcode_387 First Unique Character in a String
- leetcode First Unique Character in a String