面试题[简单]:判断字符是否唯一解析(python,Java)

来源于leetcode

题目要求:

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

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

python 解法:

	class Solution(object):
		def isUnique(self, astr):
			c = ""
        for i in astr:
            if i not in c:
                c += i
            else:
                return False
        return True

Java解法:

class Solution {
    
    
    public boolean isUnique(String astr) {
    
    
        Set set = new HashSet();
        for (int i = 0; i <astr.length() ; i++) {
    
    
            set.add(astr.charAt(i));
        }
        return set.size() == astr.length();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40463117/article/details/105514928