首页 > 编程语言 >#yyds干货盘点# LeetCode程序员面试金典:判定字符是否唯一

#yyds干货盘点# LeetCode程序员面试金典:判定字符是否唯一

时间:2022-11-27 18:00:35浏览次数:34  
标签:yyds false 示例 金典 chars return astr true LeetCode

题目:

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

示例 1:

输入: s = "leetcode"

输出: false

示例 2:

输入: s = "abc"

输出: true

代码实现:

class Solution {
public boolean isUnique(String astr) {
char[] chars = astr.toCharArray();
Arrays.sort(chars);
int i = 0;
while (i<chars.length-1){
if (chars[i] == chars[i+1]){
return false;
}else {
i++;
}
}
return true;
}
}

标签:yyds,false,示例,金典,chars,return,astr,true,LeetCode
From: https://blog.51cto.com/u_13321676/5890250

相关文章

  • leetcode 56. 合并区间 js实现
    以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i]=[starti,endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入......
  • leetcode_D4_58最后一个单词的长度
    1.题目  2.解一2.1  2.2  2.3  主要思路:3种方法相差不大,思路基本一致,主要是细节处理上有所不同,都是首先去掉字符串最前面和最后面的空元素,然后从最后......
  • 动态规划算法图文详解(Kotlin语言):二维矩阵中找到只包含 1 的最大正方形(LeetCode-22
    题目描述在一个由0和1组成的二维矩阵内,找到只包含1的最大正方形,并返回其面积。示例:输入:1010010 ​​​11​​​ 111 ​​​11​​​ 110010输出:4......
  • 力扣 leetcode 1752. 检查数组是否经排序和轮转得到
    问题描述给你一个数组nums。nums的源数组中,所有元素与nums相同,但按非递减顺序排列。如果nums能够由源数组轮转若干位置(包括0个位置)得到,则返回true;否则,返回fa......
  • 移除元素-LeetCode27 双指针
    力扣链接:https://leetcode.cn/problems/remove-element/题目  给你一个数组nums 和一个值val,你需要原地移除所有数值等于 val 的元素,并返回移除后数组的新长......
  • LeetCode刷题记录.Day26
    删除字符串中所有相邻重复项1047.删除字符串中的所有相邻重复项-力扣(LeetCode)classSolution{public:stringremoveDuplicates(strings){stack<ch......
  • 力扣 leetcode 882. 细分图中的可到达节点
    问题描述给你一个无向图(原始图),图中有n个节点,编号从0到n-1。你决定将图中的每条边细分为一条节点链,每条边之间的新节点数各不相同。图用由边组成的二维数组edg......
  • leetcode-1175-easy
    leetcode-1175-easyReturnthenumberofpermutationsof1tonsothatprimenumbersareatprimeindices(1-indexed.)(Recallthatanintegerisprimeifand......
  • leetcode-929-easy
    UniqueEmailAddressesEveryvalidemailconsistsofalocalnameandadomainname,separatedbythe'@'sign.Besideslowercaseletters,theemailmaycontai......
  • leetcode-696-easy
    CountBinarySubstringsGivenabinarystrings,returnthenumberofnon-emptysubstringsthathavethesamenumberof0'sand1's,andallthe0'sandallth......