17. Longest continuous sequence
Given an unordered array, the length of the longest sequential sequence is obtained. Required degree of responsibility O (N)
Example: the longest continuous sequence of array num:[100, 4, 200, 1, 3, 2] is [1,2,3,4], and the length is 4
Solution One: Create a bool array that indicates whether the number exists, and then the longest value in the array that is consecutive true. In [100, 4, 200, 1, 3, 2], its minimum value is 1, the maximum value is 200, so establish an array bexist with a length of imax-imin+1 (200-1+1) = 200, and then assign values to the arrays, where bexist[100-1], bexist[4–1 ],BEXIST[200–1],BEXIST[1–1],BEXIST[3–1],BEXIST[2–1] is set to true, the last found in the array is the longest consecutive true value is bexit[0]~bexist[3], length 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));}}
Solution one applies to the case where the number range is smaller in num[]. If num is imin = -999999,imax = +999999, it will waste a lot of space.
Solution Two:
With a hash table. Put all the numbers in the array into the table, and for each number, see if their contiguous values ( -1/+1) are also in the table.
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. Spiral Matrix
Given a m*n matrix, the clockwise spiral is printed from the periphery.
Cases:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Output:
[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;in T m = matrix.length;int n = matrix[0].length;int x = 0;int y = 0;while (M > 0 && n > 0) {//if one Row/colum n left, no circle can is formedif (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 rightfor (int i = 0; i < n-1; i++) {Result.add (matrix[x][y++]);} Right-move downfor (int i = 0; i < m-1; i++) {Result.add (matrix[x++][y]);} Bottom-move leftfor (int i = 0; i < n-1; i++) {Result.add (matrix[x][y--]);} Left-move upfor (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 + "");}}
Interview 10 Big algorithm Rollup-strings and Arrays 8