0
点赞
收藏
分享

微信扫一扫

leetcode之哈希表八: 判定字符是否唯一

穆熙沐 2022-01-16 阅读 59

原题:

力扣icon-default.png?t=LBL2https://leetcode-cn.com/problems/is-unique-lcci/

一、题目要求

实现一个算法,确定一个字符串 s 的所有字符是否全都不同。

示例 1:


示例 2:


限制:

0 <= len(s) <= 100
如果你不使用额外的数据结构,会很加分。

二、解题

package com.leetcode.hash;

public class Solution21 {
    public static void main(String[] args) {
        String s = "leetcode";
        System.out.println(isUnique(s));
    }

    public static boolean isUnique(String astr) {
        for (int i = 0; i < astr.length(); i++) {
            char c = astr.charAt(i);
            if (astr.indexOf(c) != astr.lastIndexOf(c)) {
                return false;
            }
        }

        return true;
    }
}

三、运行结果

四、提交结果

 

 

举报

相关推荐

0 条评论