17.最长连续序列
给定一个无序数组,求最长连续序列的长度。要求负责度为O(n)
例:数组num:[100, 4, 200,1, 3, 2]的最长连续序列为[1,2,3,4],长度为4
解法一:建立一个bool数组,用于表示该数字是否存在,之后求数组中连续为true的最长值即可。在[100, 4, 200, 1, 3, 2]中,其最小值为1,最大值为200,因此建立长度为iMax-iMin+1(200-1+1) = 200的数组bExist,之后对数组赋值,其中bExist[100 - 1] ,bExist[4 – 1],bExist[200 – 1],bExist[1 – 1],bExist[3 – 1],bExist[2 – 1]设为true,最后发现数组中最长一段连续为true的值是bExit[0]~bExist[3],长度4。
Code:
public class test {
public static int longestConsecutive(int[] num) {
int maxLength = 0;
int iMin = Integer.MAX_VALUE, iMax = Integer.MIN_VALUE;
for (int i : num) {
if (iMin > i)
iMin = i;
if (iMax < i)
iMax = i;
}
int iRange = iMax - iMin + 1;
boolean[] bExist = new boolean[iRange];
for (int i : num) {
bExist[i - iMin] = true;
}
int i = 0, j = 0;
while (i < iRange) {
if (bExist[i] == true)
++j;
else {
maxLength = Math.max(maxLength, j);
j = 0;
}
++i;
}
maxLength = Math.max(maxLength, j);
return maxLength;
}
public static void main(String[] args) {
int[] a = { 100, 4, 200, 1, 3, 2 };
System.out.println(longestConsecutive(a));
}
}
解法一适用于num[]中数字区间较小的情况。若num中iMin = -999999,iMax = +999999,则会对浪费大量空间。
解法二:
用哈希表。将数组中所有数字存入表中,对每个数字,查看其连续值(-1/+1)是否也在表中。
Code:
import java.util.HashSet;
import java.util.Set;
public class test {
public static int longestConsecutive(int[] num) {
if (num.length == 0) {
return 0;
}
Set<Integer> set = new HashSet<Integer>();
int max = 1;
for (int e : num)
set.add(e);
for (int e : num) {
int left = e - 1;
int right = e + 1;
int count = 1;
while (set.contains(left)) {
count++;
set.remove(left);
left--;
}
while (set.contains(right)) {
count++;
set.remove(right);
right++;
}
max = Math.max(count, max);
}
return max;
}
public static void main(String[] args) {
int[] a = { 2147483646, -2147483647, 0, 2, 2147483644, -2147483645,
2147483645 };
System.out.println(longestConsecutive(a));
}
}
18.螺旋矩阵
给定一个m*n的矩阵,从外围按顺时针螺旋打印。
例:
[
[ 1,2, 3 ],
[ 4,5, 6 ],
[ 7,8, 9 ]
]
输出:
[1,2,3,6,9,8,7,4,5].
Code:
import java.util.ArrayList;
public class test {
public static ArrayList<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> result = new ArrayList<Integer>();
if (matrix == null || matrix.length == 0)
return result;
int m = matrix.length;
int n = matrix[0].length;
int x = 0;
int y = 0;
while (m > 0 && n > 0) {
// if one row/column left, no circle can be formed
if (m == 1) {
for (int i = 0; i < n; i++) {
result.add(matrix[x][y++]);
}
break;
} else if (n == 1) {
for (int i = 0; i < m; i++) {
result.add(matrix[x++][y]);
}
break;
}
// below, process a circle
// top - move right
for (int i = 0; i < n - 1; i++) {
result.add(matrix[x][y++]);
}
// right - move down
for (int i = 0; i < m - 1; i++) {
result.add(matrix[x++][y]);
}
// bottom - move left
for (int i = 0; i < n - 1; i++) {
result.add(matrix[x][y--]);
}
// left - move up
for (int i = 0; i < m - 1; i++) {
result.add(matrix[x--][y]);
}
x++;
y++;
m = m - 2;
n = n - 2;
}
return result;
}
public static void main(String[] args) {
ArrayList<Integer> lResult = new ArrayList<Integer>();
int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
lResult = spiralOrder(matrix);
for (Integer i : lResult)
System.out.print(i + " ");
}
}