Skip to content

Commit 0573fca

Browse files
committed
20181030
1 parent 08ebd97 commit 0573fca

20 files changed

+600
-305
lines changed

src/main/java/leetcode/array/LongestConsecutiveSequence.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/main/java/leetcode/array/MergeIntervals.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/main/java/leetcode/array/RotateImage.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/main/java/leetcode/array/SpiralMatrix.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/main/java/leetcode/array/SpiralMatrix2.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package leetcode.array;
2+
3+
/**
4+
* 给定一个未排序的整数数组,找出最长连续序列的长度。
5+
6+
要求算法的时间复杂度为 O(n)。
7+
8+
示例:
9+
10+
输入: [100, 4, 200, 1, 3, 2]
11+
输出: 4
12+
解释: 最长连续序列是 [1, 2, 3, 4]。它的长度为 4。
13+
*/
14+
15+
import java.util.HashSet;
16+
17+
public class _128_LongestConsecutiveSequence {
18+
19+
public int longestConsecutive(int[] num) {
20+
HashSet<Integer> set = new HashSet<>();
21+
22+
int max = Integer.MIN_VALUE;
23+
for (int i : num) {
24+
set.add(i);
25+
}
26+
for (int i = 0; i < num.length; i++) {
27+
int temp = num[i];
28+
int len = 1;
29+
while (set.contains(--temp)) {
30+
len++;
31+
set.remove(temp);
32+
}
33+
temp = num[i];
34+
while (set.contains(++temp)) {
35+
len++;
36+
set.remove(temp);
37+
}
38+
if (len > max) {
39+
max = len;
40+
}
41+
42+
}
43+
return max;
44+
}
45+
46+
public static void main(String[] args) {
47+
int[] num = {100, 4, 200, 1, 3, 2, 5, 6, 7, 101, 102, 103, 104, 105, 106, 107, 108, 109,
48+
110, 8, 9, 10,
49+
11, 12, 13};
50+
_128_LongestConsecutiveSequence test = new _128_LongestConsecutiveSequence();
51+
System.out.println(test.longestConsecutive(num));
52+
}
53+
}

src/main/java/leetcode/array/_26_RemoveDuplicatesFromSortedArray.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,41 @@
11
package leetcode.array;
2-
/* Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
32

4-
Do not allocate extra space for another array, you must do this in place with constant memory.
3+
/**
4+
* 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
55
6-
For example,
7-
Given input array A =[1,1,2],
6+
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
87
9-
Your function should return length =2, and A is now[1,2]. */
8+
示例 1:
9+
10+
给定数组 nums = [1,1,2],
11+
12+
函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。
13+
14+
你不需要考虑数组中超出新长度后面的元素。
15+
示例 2:
16+
17+
给定 nums = [0,0,1,1,1,2,2,3,3,4],
18+
19+
函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。
20+
21+
你不需要考虑数组中超出新长度后面的元素。
22+
说明:
23+
24+
为什么返回数值是整数,但输出的答案是数组呢?
25+
26+
请注意,输入数组是以“引用”方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
27+
28+
你可以想象内部操作如下:
29+
30+
// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
31+
int len = removeDuplicates(nums);
32+
33+
// 在函数里修改输入数组对于调用者是可见的。
34+
// 根据你的函数返回的长度, 它会打印出数组中该长度范围内的所有元素。
35+
for (int i = 0; i < len; i++) {
36+
print(nums[i]);
37+
}
38+
*/
1039
public class _26_RemoveDuplicatesFromSortedArray {
1140

1241
public int removeDuplicates(int[] nums) {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package leetcode.array;
2+
3+
/**
4+
* 给定一个 n × n 的二维矩阵表示一个图像。
5+
6+
将图像顺时针旋转 90 度。
7+
8+
说明:
9+
10+
你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
11+
12+
示例 1:
13+
14+
给定 matrix =
15+
[
16+
[1,2,3],
17+
[4,5,6],
18+
[7,8,9]
19+
],
20+
21+
原地旋转输入矩阵,使其变为:
22+
[
23+
[7,4,1],
24+
[8,5,2],
25+
[9,6,3]
26+
]
27+
示例 2:
28+
29+
给定 matrix =
30+
[
31+
[ 5, 1, 9,11],
32+
[ 2, 4, 8,10],
33+
[13, 3, 6, 7],
34+
[15,14,12,16]
35+
],
36+
37+
原地旋转输入矩阵,使其变为:
38+
[
39+
[15,13, 2, 5],
40+
[14, 3, 4, 1],
41+
[12, 6, 8, 9],
42+
[16, 7,10,11]
43+
]
44+
*/
45+
46+
public class _48_RotateImage {
47+
48+
public void rotate(int[][] matrix) {
49+
int n=matrix[0].length,temp;
50+
// 沿着副对角线反转
51+
for (int i = 0; i < n; ++i) {
52+
for (int j = 0; j < n - i; ++j) {
53+
temp = matrix[i][j];
54+
matrix[i][j] = matrix[n - 1 - j][n - 1 - i];
55+
matrix[n - 1 - j][n - 1 - i] = temp;
56+
}
57+
}
58+
// 沿着水平中线反转
59+
for (int i = 0; i < n / 2; ++i){
60+
for (int j = 0; j < n; ++j) {
61+
temp = matrix[i][j];
62+
matrix[i][j] = matrix[n - 1 - i][j];
63+
matrix[n - 1 - i][j] = temp;
64+
}
65+
}
66+
}
67+
68+
69+
public void rotate1(int[][] matrix) {
70+
int x = matrix[0].length;
71+
int y = matrix.length;
72+
//上下反转
73+
for(int i = 0 ; i < y/2 ; i++){
74+
for(int j = 0 ; j < x ; j ++){
75+
swap(matrix, i, j, y-i-1, j);
76+
}
77+
}
78+
//对角线反转
79+
for(int i = 0 ; i < x ; i++){
80+
for(int j = i+1 ; j < y ; j++){
81+
swap(matrix, i, j, j, i);
82+
}
83+
}
84+
85+
86+
}
87+
88+
private void swap (int[][] m, int ax, int ay, int bx, int by) {
89+
int temp = m[ax][ay];
90+
m[ax][ay] = m[bx][by];
91+
m[bx][by] = temp;
92+
}
93+
}

0 commit comments

Comments
 (0)