面试10大算法题汇总-字符串和数组8

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 + " ");
	}
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值