leetcode:First Unique Character in a String

原创 2016年08月28日 14:11:45

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
然后找出最小的值

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;
    }

}
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

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

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

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

387. First Unique Character in a String

找出字符串中第一个无重复字符,哈希表,数组计数。

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

386. Lexicographical Numbers\387. First Unique Character in a String

题目描述 代码实现题目描述Given an integer n, return 1 - n in lexicographical order.For example, given 13, return...

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
  • 99

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

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

关于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 387. First Unique Character in a String 解题报告

LeetCode 387. First Unique Character in a String 解题报告
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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