Skip to content

Commit 60b687e

Browse files
committed
[Function add]
1. Add conclusion about index count and bucket count. 2. Add leetcode solutions.
1 parent 0501b74 commit 60b687e

File tree

5 files changed

+241
-33
lines changed

5 files changed

+241
-33
lines changed

DataStructrue/Sort/BucketSort.md

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,59 @@
1-
## 桶排序
2-
#### 桶排序方法
3-
1. 先找出数组的最大值max,我们创建一个最大值max + 1长度的数组用于记录所有的数组出现的次数。
4-
2. 因为生成的bucket是按序递增的,我们根据bucket重新填满我们的数组。
1+
# 桶排序
2+
### 介绍
3+
桶排序是一种分治技术的体现,我们通过横向对比的方法研究桶排序的优势。快速排序的时间复杂度是O(NlgN), N的影响是很大的。所以我们希望找到一种方法,将(原本基数很大的)数组分别处理(分治技术)。
54

6-
#### 代码
5+
谈到分治,重要的是三点:
6+
1. 如何分
7+
2. 怎么治
8+
3. 怎么合
79

8-
```Java
9-
package ca.mcmaster.chapter.two.Sort;
10+
此次介绍桶排序,就是一种在排序中的分的技术。
11+
12+
### 原理分析
13+
1. 我们有一组小数,在0-1之间:
14+
| 0.21 | 0.33 | 0.27 | 0.98 | 0.45 | 0.41 | 0.66 | 0.71 | 0.69 | 0.11 |
15+
|---|---|---|---|---|---|---|---|---|---|
16+
2. 我们创建一个哈希桶,键:小数乘以10以后的整数部分, 值:链(或者数组)用于存储符合当前键的所有小数。我们按顺序遍历小数并将他们放入桶中。
17+
|||
18+
|---|---|
19+
| 0 ||
20+
| 1 |0.11|
21+
| 2 |0.21, 0.27|
22+
| 3 |0.33|
23+
| 4 |0.45, 0.41|
24+
| 5 ||
25+
| 6 |0.66, 0.69|
26+
| 7 |0.71|
27+
| 8 ||
28+
| 9 |0.98|
29+
3. 此时已经完成了分的过程我们只需要对每一个数组进行排序。
30+
4. 再根据index将所有排序好的进行合并。
1031

32+
### 代码示例
33+
```Java
1134
public class BucketSort {
12-
private static void sort(int[] nums){
13-
int max = Integer.MIN_VALUE;
14-
for(int num : nums)
15-
max = Math.max(max, num);
16-
int[] bucket = new int[max+1];
17-
for(int i = 0; i < nums.length; i++)
18-
bucket[nums[i]]++;
19-
for(int i = 0, j = 0; j < bucket.length; j++){
20-
while(bucket[j]-- > 0)
21-
nums[i++] = j;
22-
}
23-
}
24-
public static void main(String[] args) {
25-
int[] arr = new int[]{1,4,5,3,2,5,7};
26-
sort(arr);
27-
for(int i : arr)
28-
System.out.print(i + " ");
29-
System.out.println();
30-
}
35+
public static Object[] bucketSort(float[] arr){
36+
ArrayList[] lists = new ArrayList[10];
37+
for(int i = 0; i < 10; i++)
38+
lists[i] = new ArrayList<Float>();
39+
for(float f : arr){
40+
lists[(int)(f * 10)].add(f);
41+
}
42+
for(ArrayList list : lists){
43+
list.sort((f1, f2) -> {return ((Float)f1 > (Float)f2) ? -1: 1;} );
44+
}
45+
ArrayList<Float> res = new ArrayList<>();
46+
for(ArrayList list : lists)
47+
res.addAll(list);
48+
return res.toArray();
49+
}
50+
51+
public static void main(String[] args) {
52+
Object[] result = bucketSort(new float[]{0.21F, 0.33F, 0.27F, 0.98F, 0.45F, 0.41F, 0.66F, 0.71F, 0.69F, 0.11F});
53+
for(Object f : result)
54+
System.out.println(f + " ");
55+
System.out.println();
56+
}
3157
}
3258
```
33-
34-
### 复杂度分析
35-
* 时间复杂度:O(N)
36-
* 额外空间复杂度:O(N)
59+

DataStructrue/Sort/CountSort.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 计数排序
2+
#### 计数排序方法
3+
1. 先找出数组的最大值max,我们创建一个最大值max + 1长度的数组用于记录所有的数组出现的次数。
4+
2. 因为生成的bucket是按序递增的,我们根据bucket重新填满我们的数组。
5+
6+
#### 技术排序分析
7+
技术排序可以使用额外的内存空间,所以思考上就会简单很多。总体的效率是Ο(n+k),k是整数的范围。
8+
1. 举例: 7,6,3,1,3,其中最大值为7,我们建造一个大小为8的数组(即为max + 1)。
9+
2. 我们遍历需要排序的数组,可以得到:
10+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
11+
|---|---|---|---|---|---|---|---|
12+
| 0 | 1 | 0 | 2 | 0 | 0 | 1 | 1 |
13+
3. 其中上排是数字,下排是数字对应出现的次数。
14+
4. 至此,我们可以遍历这个数组的每一个位置,然后将index填充到新的数组中。实现排序
15+
16+
#### 代码
17+
```Java
18+
package ca.mcmaster.chapter.two.Sort;
19+
20+
public class CountSort {
21+
private static void sort(int[] nums){
22+
int max = Integer.MIN_VALUE;
23+
for(int num : nums)
24+
max = Math.max(max, num);
25+
int[] bucket = new int[max+1];
26+
for(int i = 0; i < nums.length; i++)
27+
bucket[nums[i]]++;
28+
for(int i = 0, j = 0; j < bucket.length; j++){
29+
while(bucket[j]-- > 0)
30+
nums[i++] = j;
31+
}
32+
}
33+
public static void main(String[] args) {
34+
int[] arr = new int[]{1,4,5,3,2,5,7};
35+
sort(arr);
36+
for(int i : arr)
37+
System.out.print(i + " ");
38+
System.out.println();
39+
}
40+
}
41+
```

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,18 +291,21 @@
291291

292292
[160. Intersection of Two Linked Lists](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/160.%20Intersection%20of%20Two%20Linked%20Lists.md)
293293

294-
[162. Find Peak Element]((https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/162.%20Find%20Peak%20Element.md))
294+
[162. Find Peak Element](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/162.%20Find%20Peak%20Element.md)
295+
296+
[164. Maximum Gap](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/164.%20Maximum%20Gap.md)
297+
298+
[165. Compare Version Numbers](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/165.%20Compare%20Version%20Numbers.md)
295299

296300
## Algorithm(4th_Edition)
297301
Reading notes of book Algorithm(4th Algorithm),ISBN: 9787115293800.
298302
All java realization codes are placed in different packages.
299303
### [Sorting](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Sort/Sort.md)
300304
* [Selection](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Sort/SelectionSort.md)
301305
* [Insertion](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Sort/InsertionSort.md)
302-
* [Shell]()
303306
* [Merge](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Sort/MergeSort.md)
304307
* [QuickSort](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Sort/QuickSort.md)
305-
* [Priority Queue]()
308+
* [CountSort](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Sort/CountSort.md)
306309
* [BucketSort](https://github.com/Seanforfun/Algorithm/blob/master/DataStructrue/Sort/BucketSort.md)
307310

308311
### Search

leetcode/164. Maximum Gap.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
## 164. Maximum Gap
2+
3+
### Questions:
4+
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.
5+
6+
Return 0 if the array contains less than 2 elements.
7+
8+
```
9+
Example 1:
10+
11+
Input: [3,6,9,1]
12+
Output: 3
13+
Explanation: The sorted form of the array is [1,3,6,9], either (3,6) or (6,9) has the maximum difference 3.
14+
15+
Example 2:
16+
Input: [10]
17+
Output: 0
18+
Explanation: The array contains less than 2 elements, therefore return 0.
19+
```
20+
21+
Note:
22+
* You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range.
23+
* Try to solve it in linear time/space.
24+
25+
### Thinking:
26+
* Method 1:
27+
* 先排序,再从头开始遍历找出max gap。
28+
* 并不满足题目要求的O(N),这种方法的复杂度是O(N + NlgN).
29+
*
30+
```Java
31+
class Solution {
32+
public int maximumGap(int[] nums) {
33+
Arrays.sort(nums);
34+
int diff = 0;
35+
for(int i = 1; i < nums.length; i++)
36+
diff = Math.max(diff, nums[i] - nums[i - 1]);
37+
return diff;
38+
}
39+
}
40+
```
41+
42+
### 二刷
43+
1. 研究了[桶排序](https://seanforfun.github.io/algorithm/2018/12/18/BucketSort.html)的原理。
44+
45+
![Imgur](https://i.imgur.com/vZ1OYIA.jpg)
46+
```Java
47+
class Solution {
48+
public int maximumGap(int[] nums) {
49+
if(nums.length <= 1) return 0;
50+
int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
51+
for(int num : nums){
52+
max = Math.max(max, num);
53+
min = Math.min(min, num);
54+
}
55+
if(max == min) return 0;
56+
if(nums.length == 2) return max - min;
57+
int len = (int)Math.ceil((double)(max - min) / (nums.length - 1));
58+
int n = (max - min) / len;
59+
int[] minValues = new int[n + 1];
60+
int[] maxValues = new int[n + 1];
61+
for(int i = 0; i < n + 1; i++){
62+
minValues[i] = Integer.MAX_VALUE;
63+
maxValues[i] = Integer.MIN_VALUE;
64+
}
65+
for(int num : nums){
66+
int index = (num - min) / len;
67+
minValues[index] = Math.min(minValues[index], num);
68+
maxValues[index] = Math.max(maxValues[index], num);
69+
}
70+
int result = 0;
71+
int pre = -1;
72+
for(int i = 0; i < n + 1; i++){
73+
if(minValues[i] != Integer.MAX_VALUE){
74+
if(pre != -1)
75+
result = Math.max(result, minValues[i] - pre);
76+
pre = maxValues[i];
77+
}
78+
}
79+
return result;
80+
}
81+
}
82+
```

leetcode/165. Compare Version Numbers.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11
## 165. Compare Version Numbers
2+
3+
### Questions:
4+
Compare two version numbers version1 and version2.
5+
If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0.
6+
7+
You may assume that the version strings are non-empty and contain only digits and the . character.
8+
The . character does not represent a decimal point and is used to separate number sequences.
9+
For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
10+
11+
```
12+
Example 1:
13+
14+
Input: version1 = "0.1", version2 = "1.1"
15+
Output: -1
16+
17+
Example 2:
18+
19+
Input: version1 = "1.0.1", version2 = "1"
20+
Output: 1
21+
22+
Example 3:
23+
24+
Input: version1 = "7.5.2.4", version2 = "7.5.3"
25+
Output: -1
26+
```
27+
28+
229
### Thinking:
330
* Method 1:
431
* 要注意在使用split的时候"."需要使用转义字符"\\."
@@ -34,4 +61,36 @@ class Solution {
3461
return 0;
3562
}
3663
}
64+
```
65+
66+
### 二刷
67+
1. 还是要注意使用转义字符,因为.在正则中是单字符匹配。
68+
2. 从字符串转成整形有两种方法,一种是Integer.valueOf(),第二种是Integer.parseInt(),似乎valueOf方法更快一些。
69+
```Java
70+
class Solution {
71+
public int compareVersion(String version1, String version2) {
72+
String[] arr1 = version1.split("\\.");
73+
String[] arr2 = version2.split("\\.");
74+
int len1 = arr1.length, len2 = arr2.length;
75+
int index1 = 0, index2 = 0;
76+
while(index1 < len1 && index2 < len2){
77+
Integer token1 = Integer.valueOf(arr1[index1++]);
78+
Integer token2 = Integer.valueOf(arr2[index2++]);
79+
if(token1 < token2) return -1;
80+
else if(token1 > token2) return 1;
81+
}
82+
if(index1 == len1){
83+
while(index2 < len2){
84+
if(Integer.valueOf(arr2[index2++]) != 0)
85+
return -1;
86+
}
87+
}else{
88+
while(index1 < len1){
89+
if(Integer.valueOf(arr1[index1++]) != 0)
90+
return 1;
91+
}
92+
}
93+
return 0;
94+
}
95+
}
3796
```

0 commit comments

Comments
 (0)