LeetCode—387. First Unique Character in a String

本文提供了一种解决LeetCode中“第一个唯一字符在字符串中的索引”问题的方法。通过使用列表来存储唯一字符并借助映射记录字符出现的索引,从而高效地找到第一个不重复的字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

First Unique Character in a String思路:非常简单,一个list存,一个map记录下标。


GitHub地址https://github.com/corpsepiges/leetcode

点此进入如果可以的话,请点一下star,谢谢。



<span style="font-size:12px;">public class Solution {
    public int firstUniqChar(String s) {
        char[] cs=s.toCharArray();
        List<Character> list=new ArrayList<Character>();
        Map<Character,Integer> map=new HashMap<Character,Integer>();
        for (int i = 0; i < cs.length; i++) {
            Character c=cs[i];
            if (map.containsKey(c)) {
                list.remove(c);
            }else{
                list.add(c);
                map.put(c,i);
            }
        }
        return list.size()==0?-1:map.get(list.get(0));
    }
}</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值