"Coding Interview Guide" - whether or not all of the characters appear only once to determine the character array

[ Title ]

  Given a character type array chas [], chas determines whether all the characters appear only once

  For example, chas = [ 'a', 'b', 'c'], returns true; chas = [ '1', '2', '1'], it returns false

 

[ Requirements ]

  Time complexity is O (N)

1  // The method using the data structure HashSet 
2  public  Boolean isUnique ( char [] Chas)
 . 3  {
 . 4      IF (Chas == null )
 . 5      {
 . 6        return  to true ;
 . 7      }
 . 8  
. 9      HashSet <Character> SET = new new HashSet <> ();
 10      for ( int I = 0; I <chas.length; I ++ )
 . 11      {
 12 is        IF (set.contains (Chas [I]))
 13 is        {
 14            return  to false;
 15        }
 16        the else 
. 17        {
 18 is            set.add (Chas [I]);
 . 19        }
 20 is      }
 21 is      return  to true ;
 22 is  }
 23 is  
24  
25  // second method, using a boolean array 
26 is  public  Boolean isUnique ( char [] Chas )
 27  {
 28      IF (Chas == null )
 29      {
 30        return  to true ;
 31 is      }
 32          
33 is      Boolean[] map = new boolean[256];
34     for(int i = 0; i < chas.length; i++)
35     {
36       if(map[chas[i]])
37       {
38           return false;
39       }
40       map[chas[i]] = true;
41     }
42     return true;
43 }

 

[ Requirements ]

  In ensuring the extra space complexity is O (1) is provided, time complexity as low as possible to achieve a method

1  public  boolean isUnique ( char [] chas)
 2  {
 3      if (chas == null || chas.length <2 )
 4      {
 5        return  true ;
6      }
 7  
8      heapSort (chas);
9      for ( int i = 1; i <chas.length; i ++ )
 10      {
 11        if (chas [i] == chas [i - 1 ])
 12        {    
 13            return  false ;
14        }
 15     }
16     return true;
17 }
18   
  // 堆排序的非递归实现 19 public void heapSort(char[] chas) 20 { 21 for(int i = 0; i < chas.length; i++) 22 { 23   heapInsert(chas, i); 24 } 25 for(int i = chas.length - 1; i > 0; i--) 26 { 27   swap(chas, 0, i); 28   heapify(chas, 0, i); 29 } 30 } 31 is 32 // build large root stack 33 is public void heapInsert ( char [] Chas, int I) 34 is { 35 int parent = 0 ; 36 the while (I = 0! ) 37 [ { 38 is   parent = (I -. 1) / 2 ; 39   IF (Chas [parent] <Chas [I]) // big heap root, than the value of any sub-node of the parent node is small, the maximum value of the root node 40   { 41 is   the swap (Chas, parent, I); 42 is   I = parent; 43 is   } 44   else 45   { 46   break; 47   } 48 } 49 } 50 51 // 调整堆 52 public void heapify(char[] chas, int i, int size) 53 { 54 int left = i * 2 + 1; 55 int right = i * 2 + 2; 56 int largest = i; 57 while(left < size) 58 { 59   if(chas[left] > chas[i]) 60   { 61   largest = left; 62   } 63   if(right < size && chas[right] > chas[largest]) 64   { 65   largest = right; 66   } 67   if(largest != i) 68   { 69   swap(chas, largest, i); 70   } 71   else 72   { 73     break; 74   } 75   i = largest; 76   left = i * 2 + 1; 77    right = i * 2 + 2; 78 } 79 } 80 81 public void swap(char[] chas, int i, int j) 82 { 83 char temp = chas[i]; 84 chas[i] = chas[j]; 85 chas[j] = temp; 86 }

 

 

Source: Left Cheng Yun teacher "Programmer Code Interview Guide"

  

Guess you like

Origin www.cnblogs.com/latup/p/10960136.html