|
1 | 1 | # `Today Update`
|
2 | 2 | (Notes: "♥" Welcome to visit or fork or star my LeetCode Manager @
|
3 | 3 | https://github.com/jackzhenguo/LeetCodeManager
|
4 |
| -## Sort |
5 |
| -### 324 Wiggle Sort II |
6 |
| -* [Github:#324 Wiggle Sort II](/Sort/WiggleSortSln.cs) |
7 |
| -* [CSDN:#324 Wiggle Sort II](http://blog.csdn.net/daigualu/article/details/72820281) |
8 |
| - * This is a great question for it involves a great idea: virtual index |
9 |
| - ```C# |
10 |
| - /// <summary> |
11 |
| - /// WiggleSort |
12 |
| - /// </summary> |
13 |
| - public class WiggleSortSln |
14 |
| - { |
15 |
| - private static int[] _array; |
16 |
| - |
17 |
| - public int[] WiggleSort(int[] array) |
18 |
| - { |
19 |
| - _array = array; |
20 |
| - int median = findKThLargest(_array.Length / 2); |
21 |
| - int left = 0, i = 0, right = _array.Length - 1; |
22 |
| - |
23 |
| - while (i <= right) |
24 |
| - { |
25 |
| - if (_array[newIndex(i)] > median) |
26 |
| - { |
27 |
| - //put newIndex(i) at odd index(from 1, 3, to 5, ...) |
28 |
| - swap(newIndex(left++), newIndex(i)); |
29 |
| - i++; |
30 |
| - } |
31 |
| - else if (_array[newIndex(i)] < median) |
32 |
| - { |
33 |
| - //put newIndex(i) at even index(max even index to little .... ) |
34 |
| - swap(newIndex(right--), newIndex(i)); //right--, so i relatively toward right 1 step |
35 |
| - } |
36 |
| - else |
37 |
| - { |
38 |
| - i++; |
39 |
| - } |
40 |
| - } |
41 |
| - return _array; |
42 |
| - } |
43 |
| - |
44 |
| - private int newIndex(int index) |
45 |
| - { |
46 |
| - return (1 + 2 * index) % (_array.Length | 1); |
47 |
| - } |
48 | 4 |
|
49 |
| - private void swap(int i, int j) |
50 |
| - { |
51 |
| - int tmp = _array[i]; |
52 |
| - _array[i] = _array[j]; |
53 |
| - _array[j] = tmp; |
54 |
| - } |
55 |
| - |
56 |
| - //based on quick sort to find the Kth largest in _array |
57 |
| - private int findKThLargest(int k) |
58 |
| - { |
59 |
| - int left = 0; |
60 |
| - int right = _array.Length - 1; |
61 |
| - while (true) |
62 |
| - { |
63 |
| - int pivotIndex = quickSort(left, right); |
64 |
| - if (k == pivotIndex) |
65 |
| - return _array[pivotIndex]; |
66 |
| - else if (k < pivotIndex) |
67 |
| - right = pivotIndex - 1; |
68 |
| - else |
69 |
| - left = pivotIndex + 1; |
70 |
| - } |
71 |
| - } |
72 |
| - |
73 |
| - private int quickSort(int lo, int hi) |
| 5 | +## Bit Mainpulation |
| 6 | +### 461 Hamming Distance |
| 7 | +* [CSDN:#461 Hamming Distance](http://blog.csdn.net/daigualu/article/details/72830624) |
| 8 | + > x&(x-1) application; xor application</br> |
| 9 | + ```C# |
| 10 | + public int HammingDistance(int x, int y) |
74 | 11 | {
|
75 |
| - int key = _array[lo]; |
76 |
| - while (lo < hi) |
| 12 | + ///a and y are different bits, so we think the XOR |
| 13 | + ///think:0001(1D) |
| 14 | + /// 0100(4D) |
| 15 | + ///xor = 0101(1D^4D) |
| 16 | + int dist = 0, xor = x ^ y; |
| 17 | + while (xor > 0) |
77 | 18 | {
|
78 |
| - while (lo < hi && _array[hi] >= key) |
79 |
| - hi--; |
80 |
| - //hi is less than key, hi element moves to lo index |
81 |
| - _array[lo] = _array[hi]; |
82 |
| - while (lo < hi && _array[lo] < key) |
83 |
| - lo++; |
84 |
| - //lo is bigger than key, lo element moves to hi index |
85 |
| - _array[hi] = _array[lo]; |
| 19 | + ///xor & (xor-1): it sets the rightest 1 bit to 0 bit of xor. |
| 20 | + ++dist; |
| 21 | + xor = xor & (xor - 1); |
86 | 22 | }
|
87 |
| - _array[lo] = key; |
88 |
| - return lo; |
| 23 | + return dist; |
89 | 24 | }
|
90 |
| - } |
91 |
| - ``` |
92 |
| - ## Bit Mainpulation |
93 |
| -### 342 Power of Four |
94 |
| -* [Github:#342 Power of Four](/BitManipulation/PowOfFourSln.cs) |
95 |
| -* [CSDN:#342 Power of Four](http://blog.csdn.net/daigualu/article/details/72821233) |
96 |
| ---- |
| 25 | + ```C# |
0 commit comments