Algorithm-judge that all characters (ASCII) are different (Java)

Source: Internet
Author: User

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


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.