Leetcode_128_Longest Consecutive Sequence

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pistolove/article/details/43854597

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43854597


Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

For example,
Given [100, 4, 200, 1, 3, 2],
The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4.

Your algorithm should run in O(n) complexity.


思路:

(1)题意为给定一个未排序的数组,求数组中最长连续元素的个数。

(2)由于数组是未排序的,而需要求得数组中连续序列元素的个数。首先想到的应该是对数组进行排序,本文用的是java类库中自带的排序算法:Arrays.sort()。然后遍历数组,由于数组中可能出现多个连续序列,所以设置当前最长序列个数max和正在遍历的连续序列的个数count。首先,从数组下标为0开始往后遍历,判断当前元素和后续元素是否相差1,如果相差1,则count++,当遍历到倒数第二个元素时,返回count和max的较大值;如果相等,判断max和count大小,将较大值赋给max,继续遍历;其余情况为值相差大于1,这时将max和count较大值赋给max,并将count置为1(因为连续序列在这里断开了,需要重新记录);遍历完整个数组,所得max即为最长连续序列个数。

(3)本文算法效率不是很高,有待后续优化。希望本文对你有所帮助。


算法代码实现如下:

/**
 * @author liqq
 */
public class Longest_Consecutive_Sequence {
	public int longestConsecutive(int[] num) {
		if (num == null)  return -1;
		if (num.length == 1)  return 1;

		Arrays.sort(num);

		int count = 1;
		int max = 1;
		for (int i = 0; i < num.length - 1; i++) {
			if (num[i] + 1 == num[i + 1]) {
				count = count + 1;
				if (i == num.length - 2) {
					return count > max ? count : max;
				}
			} else if (num[i] == num[i + 1]) {
				max = count > max ? count : max;
				continue;
			} else {
				max = max > count ? max : count;
				count = 1;
			}

		}
		return max;
	}
}


Consecutive Digits

08-18

Problem DescriptionnAs a recruiting ploy, Google once posted billboards in Harvard Square and in the Silicon Valley area just stating “first 10-digit prime found in consecutive digits of e.com”. In other words, find that 10-digit sequence and then connect to the web site— and find out that Google is trying to hire people who can solve a particular kind of problem. Not to be outdone, Gaggle (a loosy-goosy fuzzy logic search firm), has devised its own recruiting problem. Consider the base 7 expansion of a rational number. For example, the first few digits of the base 7 expansion of 1/510 = 0.12541...7,33/410 = 11.15151...7, and 6/4910 = 0.06000...7, From this expansion, find the digits in a particular range of positions to the right of the "decimal" point.n nnInputnThe input file begins with a line containing a single integer specifying the number of problem sets in the file. Each problem set is specified by four base 10 numbers on a single line, n d b e, where n and d are the numerator andndenominator of the rational number and 0 ≤ n ≤ 5,000 and 1 ≤ d ≤ 5,000. b and e are the beginning and ending positions for the desired range of digits, with 0 ≤ b,e ≤ 250 and 0 ≤ (e-b) ≤ 20. Note that 0 is the position immediately to the right of the decimal point.n nnOutputnEach problem set will be numbered (beginning at one) and will generate a single line: Problem k: n / d, base 7 digits b through e: result where k is replaced by the problem set number, result is your computed result, and the other values are the corresponding input values.n nnSample Inputn4n1 5 0 0n6 49 1 3n33 4 2 7n511 977 122 126n nnSample OutputnProblem set 1: 1 / 5, base 7 digits 0 through 0: 1nProblem set 2: 6 / 49, base 7 digits 1 through 3: 600nProblem set 3: 33 / 4, base 7 digits 2 through 7: 151515nProblem set 4: 511 / 977, base 7 digits 122 through 126: 12425

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试