Arrays分类算法-Largest Range

题目要求

在这里插入图片描述

解法一:
可以通过sort的方式,简单找出最长的连续的序列。但是本题不可sort。因此为了查找高效,采用hashMap来存已知数字,别对数字是否被用过进行标记,分别向前和向后找出存在的连续序列。
Time: O(n) Space: O(n)
代码1:

import java.util.*;

class Program {
  public static int[] largestRange(int[] array) {
    // Write your code here.
		if (array == null || array.length == 0) return new int[]{};
		HashMap<Integer, Boolean> map = new HashMap<>();
		int[] res = new int[2];
		int[] temp = new int[2];
		for (int num : array) {
			map.put(num, true);
		}
		for (int i = 0; i < array.length; i++) {
			if (map.get(array[i])) {
				map.put(array[i], false);
				int num = array[i] - 1;
				temp[0] = array[i];
				temp[1] = array[i];
				while (map.containsKey(num)) {
					map.put(num, false);
					temp[0] = num;
					num--;
				} 
				num = array[i] + 1;
				while (map.containsKey(num)) {
					map.put(num, false);
					temp[1] = num;
					num++;
				}
				if ((temp[1] - temp[0]) >= (res[1] - res[0])) {
					System.arraycopy(temp, 0, res, 0, 2);
				}
			} 
		}
    return res;
  }
}

代码2(写法简化):

import java.util.*;

class Program {
   public static int[] largestRange(int[] array) {
    // Write your code here.
		if (array == null || array.length == 0) return new int[]{};
		HashMap<Integer, Boolean> map = new HashMap<>();
		int[] res = new int[2];
		int longest = 0;
		for (int num : array) {
			map.put(num, true);
		}
		for (int num : array) {
			if (map.get(num)) {
				map.put(num, false);
				int left = num - 1;
				int right = num + 1;
				int tempLongth = 1;
				while (map.containsKey(left)) {
					map.put(left, false);
					tempLongth++;
					left--;
				} 
				while (map.containsKey(right)) {
					map.put(right, false);
					tempLongth++;
					right++;
				}
				if (tempLongth > longest) {
					longest = tempLongth;
					res = new int[]{left + 1, right - 1};
				}
			} 
		}
    return res;
  }
}
已标记关键词 清除标记
相关推荐
<p> <b><span style="background-color:#FFE500;">【超实用课程内容】</span></b> </p> <p> <br /> </p> <p> <br /> </p> <p> 本课程内容包含讲解<span>解读Nginx的基础知识,</span><span>解读Nginx的核心知识、带领学员进行</span>高并发环境下的Nginx性能优化实战,让学生能够快速将所学融合到企业应用中。 </p> <p> <br /> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b><br /> </b> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b><span style="background-color:#FFE500;">【课程如何观看?】</span></b> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> PC端:<a href="https://edu.csdn.net/course/detail/26277"><span id="__kindeditor_bookmark_start_21__"></span></a><a href="https://edu.csdn.net/course/detail/27216">https://edu.csdn.net/course/detail/27216</a> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 移动端:CSDN 学院APP(注意不是CSDN APP哦) </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 本课程为录播课,课程永久有效观看时长,大家可以抓紧时间学习后一起讨论哦~ </p> <p style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <br /> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <strong><span style="background-color:#FFE500;">【学员专享增值服务】</span></strong> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b>源码开放</b> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化 </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 下载方式:电脑登录<a href="https://edu.csdn.net/course/detail/26277"></a><a href="https://edu.csdn.net/course/detail/27216">https://edu.csdn.net/course/detail/27216</a>,播放页面右侧点击课件进行资料打包下载 </p> <p> <br /> </p> <p> <br /> </p> <p> <br /> </p>
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页