【CC150】最简单 串内无重复字符
题目说明:
给定一个字符串,实现一个算法,确定一个字符串的所有字符是否都不同。
(ASCII编码)
如:abcdefga 返回false(a重复)
思路:新建辅助数组,其下标有意义,表示字符的ASCII码,其内容表示该字符出现的次数;
代码如下:
1 public class case1_isUiqueStr { 2 3 public static void main(String[] args) { 4 String str="abcdefg"; 5 str="sabcdfaefthuiaabuh"; 6 boolean res=isDiffer(str); 7 System.out.println(res); 8 } 9 10 private static boolean isDiffer(String str) { 11 if(str.isEmpty()) 12 return true; 13 14 int []help=new int[128]; 15 for(int i=0;i<str.length();i++){ 16 char c=str.charAt(i);//数组下标有意义; 17 if(help[c]>0){ 18 return false; 19 } 20 help[c]++; 21 } 22 23 return true; 24 } 25 26 }
浙公网安备 33010602011771号