public static void main(String[] args) { // question1(); // question2(); System.out.println(~1 + 1); } private static void question1() { /* 思路,规律: 利用字符串的包含和替换。 */ char[] arr = {'a','b','c','c'}; String s = new String(arr); while(s.length() > 0){ char b = getRandomChar(); System.out.println(s + "------" +b); String bStr = String.valueOf(b); if(s.contains(bStr)){ s = s.replaceFirst(bStr,""); } } System.out.println(s); } private static char getRandomChar() { String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random random = new Random(); int number = random.nextInt(str.length()); return str.charAt(number); } private static void question2() { /* 思路,规律:(也是利用字符串的包含和替换。) 如果连续且包含,则继续并将字符串该字符替换为空。否则重置。 直到找到结束,或遍历一遍没找到结束。 */ char[] arr = {'a','b','c','c'}; String s = new String(arr); String temp = s; String target = "1cacb23abcc456"; StringBuilder sb = new StringBuilder(); for(int i=0;i<target.length();i++){ String c = String.valueOf(target.charAt(i)); if(s.contains(c)){ sb.append(c); s = s.replaceFirst(c,""); if(s.length() == 0){ break; } }else{ sb = new StringBuilder(); s = temp; } } System.out.println(sb.toString()); }
标签:arr,0001,笔试,System,char,static,数组,new,String From: https://www.cnblogs.com/super-chao/p/17721593.html