Algorithm-judge that all characters (ASCII) are different (Java)
Judge that all characters are different (Java)
ASCII codeA total of 256 bits,Character Similarities and Differences, UseBit operationYou can solve the problem (11 rows). Bit operations are also supported.Sort non-repeated numeric arrays(11 rows ).
BITs are two-value storage units. They are operated directly to determine whether duplicates and non-Duplicates are sorted, saving time and space. The bool array can be used instead of its function.
Code:
/*** Created by C. l. wang */public class Main {public static void main (String [] args) {String str = god + byte; System. out. println (unique or not: + isUniqueChars (str); int [] arr = {4, 5, 1, 6, 3, 2}; sort (arr); System. out. print (sort array:); for (int I: arr) {System. out. print (I +) ;}}/*** determines whether the character in the String is unique ** @ param str String * @ return is unique */public static boolean isUniqueChars (String str) {if (str. length ()> 256) return false; boolean [] bs = new boolean [256]; for (int I = 0; I <str. length (); ++ I) {int val = str. charAt (I); if (bs [val]) return false; bs [val] = true;} return true;}/*** bit sorting (no repeated array) ** @ param arr array to be sorted */public static void sort (int [] arr) {final int MAX = 256; boolean [] bs = new boolean [MAX]; for (int I: arr) bs [I] = true; int n = 0; for (int I = 0; I <MAX; ++ I) {if (bs [I]) arr [n ++] = I ;}}}
Output:
Unique or not: true sort array: 1 2 3 4 5 6