如何打印String中的第一个非重复字符

非重复字符

“hello”中,除了'l'之外都是非重复的,但'h'是第一个非重复字符

解决方案


        我们的第一个解决方案使用LinkedHashMap 来存储字符数,因为LinkedHashMap维护插入顺序并且我们按照字符在 String中出现的顺序插入字符,一旦我们扫描了 String,我们只需要遍历LinkedHashMap并选择长度为1的。此解决方案需要一个 LinkedHashMap 和两个 for 循环。

        我们的第二个解决方案是在时间和空间之间进行权衡,一次找到第一个非重复字符。这一次,我们使用了一个Set和一个List来分别保存重复字符和不重复字符。一旦我们完成对 String 的扫描,即O(n) ,我们就可以通过访问O(1)运算符的 List 来获取 。由于 List 是一个有序集合,get(0)返回第一个元素。

        我们的第三种解决方案也类似,但是这次我们使用了HashMap而不是LinkedHashMap然后我们再次遍历 String 以找到第一个非重复字符。

三种解决方案代码


    /*
     *使用LinkedHashMap查找字符串中第一个不重复的字符

     *算法:

     *步骤1:获取字符数组并循环遍历它来构建一个

     *包含字符及其计数的散列表。

     *第二步:遍历LinkedHashMap,找到一个元素

     *值1,这是你的第一个非重复字符,

     *因为LinkedHashMap维护了插入顺序。
     */
    public static char getFirstNonRepeatedChar(String str) {
        Map<Character,Integer> counts = new LinkedHashMap<>(str.length());

        for (char c : str.toCharArray()) {
            counts.put(c, counts.containsKey(c) ? counts.get(c) + 1 : 1);
        }

        for (Map.Entry<Character,Integer> entry : counts.entrySet()) {
            if (entry.getValue() == 1) {
                return entry.getKey();
            }
        }
        throw new RuntimeException("didn't find any non repeated Character");
    }


    /*
     *仅在一次遍历中查找字符串中的第一个非重复字符。

     *它使用两个存储空间来减少一次迭代,标准的空间vs时间

     *权衡。由于我们将重复字符和非重复字符分开存储,

     *迭代结束时,列表中的第一个元素是第一个非列表元素

     *字符串中重复的字符。
     */
    public static char firstNonRepeatingChar(String word) {
        Set<Character> repeating = new HashSet<>();
        List<Character> nonRepeating = new ArrayList<>();
        for (int i = 0; i < word.length(); i++) {
            char letter = word.charAt(i);
            if (repeating.contains(letter)) {
                continue;
            }
            if (nonRepeating.contains(letter)) {
                nonRepeating.remove((Character) letter);
                repeating.add(letter);
            } else {
                nonRepeating.add(letter);
            }
        }
        return nonRepeating.get(0);
    }


    /*
     *在Java中使用HashMap从字符串中查找第一个不重复的字符。

     *算法:

     *第一步:扫描字符串并在HashMap中存储每个字符的计数

     *第二步:遍历String并从Map中获取每个字符的计数。

     *因为我们从第一个字符到最后一个字符遍历字符串,

     *当任何字符的计数为1时,我们中断,它是第一个字符

     *非重复字符。在这里,秩序是通过前进来实现的

     *再次遍历字符串。
     */
    public static char firstNonRepeatedCharacter(String word) {
        HashMap<Character,Integer> scoreboard = new HashMap<>();
        // build table [char -> count]
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (scoreboard.containsKey(c)) {
                scoreboard.put(c, scoreboard.get(c) + 1);
            } else {
                scoreboard.put(c, 1);
            }
        }
        // since HashMap doesn't maintain order, going through string again
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (scoreboard.get(c) == 1) {
                return c;
            }
        }
        throw new RuntimeException("Undefined behaviour");
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Axing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值