为了账号安全,请及时绑定邮箱和手机立即绑定

LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String

标签:
Java Python 算法

LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String

题目:

给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。

Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1.

案例:

s = "leetcode"
返回 0.

s = "loveleetcode",
返回 2.

**注意事项:**您可以假定该字符串只包含小写字母。

Note: You may assume the string contain only lowercase letters.

解题思路:

​ 很简单的题,无非就是对字符串的字母进行频率统计,找到出现频率为1 的字母索引。

​ 借助哈希映射两次遍历完成。第一次遍历进行字母频率统计,Hash Map 的Key 为字母,Value 为出现频率。第二次遍历找到频率为 1 的字母索引返回即可。

​ 不同于单词频率统计,字母一共只有 26 个,所以可以直接利用 ASii 码表里小写字母数值从 97~122,直接用 int 型数组映射。建立映射:索引为 小写字母的 ASii 码值,存储值为出现频率。

哈希映射解题:

Java:

class Solution {
    public int firstUniqChar(String s) {
        char[] chars = s.toCharArray();//转成 Char 数组
        Map<Character, Integer> map = new HashMap<>();
        for (Character c: chars) map.put(c, map.getOrDefault(c, 0) + 1);//频率统计
        for (int i = 0; i < chars.length; i++) {
            if(map.get(chars[i])==1) return i;//找到词频为1的字母(只出现一次)返回其索引
        }
        return -1;
    }
}

Python:

class Solution:
    def firstUniqChar(self, s):
        count = collections.Counter(s)# 该函数就是Python基础库里词频统计的集成函数
        index = 0
        for ch in s:
            if count[ch] == 1:
                return index
            else:
                index += 1       
        return -1

数组映射解题:

Java:

class Solution {
    public int firstUniqChar(String s) {
        char[] chars = s.toCharArray();
        int base = 97;
        int[] loc = new int[26];
        for (char c:chars) loc[c - base] += 1;
        for (int i = 0; i < chars.length; i++) {
            if(loc[chars[i]-base]==1) return i;
        }
        return -1;
    }
}

Python 基础数据结构里没有 char 型,强行使用chr(i)转换,只会导致效率更低

字符串函数解题:

Java:

利用 Java 字符串集成操作函数解题,很巧妙,效率也很高。

其中:

indexOf(): 返回该元素第一次出现的索引,没有则返回 -1

lastIndex(): 返回该元素最后一次出现的索引,没有则返回 -1

class Solution {
    public int firstUniqChar(String s) {
        int res = s.length();
        for (int i = 'a'; i <= 'z'; i++) {
            int firstIndex = s.indexOf((char)i);
            if (firstIndex == -1) continue;
            int lastIndex = s.lastIndexOf((char)i);
            if (firstIndex == lastIndex) {//两次索引值相同则证明该字母只出现一次
                res = Math.min(firstIndex, res);//res 为只出现一次的字母中索引值最小的
            }
        }
        return res == s.length() ? -1 : res;
    }
}
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
算法工程师
手记
粉丝
17
获赞与收藏
43

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消