Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c443555

Browse files
update
1 parent c99be94 commit c443555

File tree

2 files changed

+111
-88
lines changed

2 files changed

+111
-88
lines changed

‎TodayUpdate.md‎

Lines changed: 17 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,25 @@
11
# `Today Update`
22
(Notes: "♥" Welcome to visit or fork or star my LeetCode Manager @
33
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-
}
484

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)
7411
{
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)
7718
{
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);
8622
}
87-
_array[lo] = key;
88-
return lo;
23+
return dist;
8924
}
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#

‎UpdateHistory.md‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Update History
2+
3+
## Sort
4+
### 324 Wiggle Sort II
5+
* [Github:#324 Wiggle Sort II](/Sort/WiggleSortSln.cs)
6+
* [CSDN:#324 Wiggle Sort II](http://blog.csdn.net/daigualu/article/details/72820281)
7+
> This is a great question for it involves a great idea: virtual index
8+
```C#
9+
/// <summary>
10+
/// WiggleSort
11+
/// </summary>
12+
public class WiggleSortSln
13+
{
14+
private static int[] _array;
15+
16+
public int[] WiggleSort(int[] array)
17+
{
18+
_array = array;
19+
int median = findKThLargest(_array.Length / 2);
20+
int left = 0, i = 0, right = _array.Length - 1;
21+
22+
while (i <= right)
23+
{
24+
if (_array[newIndex(i)] > median)
25+
{
26+
//put newIndex(i) at odd index(from 1, 3, to 5, ...)
27+
swap(newIndex(left++), newIndex(i));
28+
i++;
29+
}
30+
else if (_array[newIndex(i)] < median)
31+
{
32+
//put newIndex(i) at even index(max even index to little .... )
33+
swap(newIndex(right--), newIndex(i)); //right--, so i relatively toward right 1 step
34+
}
35+
else
36+
{
37+
i++;
38+
}
39+
}
40+
return _array;
41+
}
42+
43+
private int newIndex(int index)
44+
{
45+
return (1 + 2 * index) % (_array.Length | 1);
46+
}
47+
48+
private void swap(int i, int j)
49+
{
50+
int tmp = _array[i];
51+
_array[i] = _array[j];
52+
_array[j] = tmp;
53+
}
54+
55+
//based on quick sort to find the Kth largest in _array
56+
private int findKThLargest(int k)
57+
{
58+
int left = 0;
59+
int right = _array.Length - 1;
60+
while (true)
61+
{
62+
int pivotIndex = quickSort(left, right);
63+
if (k == pivotIndex)
64+
return _array[pivotIndex];
65+
else if (k < pivotIndex)
66+
right = pivotIndex - 1;
67+
else
68+
left = pivotIndex + 1;
69+
}
70+
}
71+
72+
private int quickSort(int lo, int hi)
73+
{
74+
int key = _array[lo];
75+
while (lo < hi)
76+
{
77+
while (lo < hi && _array[hi] >= key)
78+
hi--;
79+
//hi is less than key, hi element moves to lo index
80+
_array[lo] = _array[hi];
81+
while (lo < hi && _array[lo] < key)
82+
lo++;
83+
//lo is bigger than key, lo element moves to hi index
84+
_array[hi] = _array[lo];
85+
}
86+
_array[lo] = key;
87+
return lo;
88+
}
89+
}
90+
```
91+
## Bit Mainpulation
92+
### 342 Power of Four
93+
* [Github:#342 Power of Four](/BitManipulation/PowOfFourSln.cs)
94+
* [CSDN:#342 Power of Four](http://blog.csdn.net/daigualu/article/details/72821233)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /