当前位置:网站首页>LeetCode581+621+207
LeetCode581+621+207
2022-08-04 09:13:00 【想进阿里的小菜鸡】
用了一个很巧妙的方法做出来的。
以nums = [2,6,4,8,10,9,15]为例,叙述思路。
思路
假如我们找到左边第一个开始下降的点和右边第一个开始上升的点,那么这两个点中点的所有点都需要排序。但是会有一种情况时[1,3,3,2]。所以我们需要找到的是左边第一个比右边最小的值大的点作为start,在右边找到第一个比左边最大值大的点是end,这样,这两个点中间的所有值就是要排序的点。其实本质还是找左边第一个开始下降的点和右边第一个开始上升的点。
左边最大值又没有说范围,所以不能直接求最大值,这里可以用一边遍历,一遍求的方式。我们可以用for循环来边遍历,边比较。max先设置为nums[0],然后从下标为1的位置开始遍历,每次遍历就先找两个数中最大的数,然后用当前i位置的数和max比较,如果当前i位置的数比max小,
end就是i。找start同理的。
总结就是end是正序遍历,start是倒序遍历。
代码
class Solution {
public int findUnsortedSubarray(int[] nums) {
int start = 0;
int length = nums.length;
int end = 0;
int min = nums[length-1];
int max = nums[0];
for(int i= 1;i<length;i++){
max = Math.max(nums[i],max);
min = Math.min(nums[length-i-1],min);
if(nums[i]<max){
end = i;
}
if(nums[length-i-1]>min){
start =length-i-1;
}
}
if(end == start){
return 0;
}
return end-start+1;
}
}
思路
这道题也是看的题解解决的。大家可以看这位大佬的题解,画的图很不错。用的是贪心算法。
代码
class Solution {
public int leastInterval(char[] tasks, int n) {
int[] table = new int[26];
for(char s:tasks){
table[s-'A']++;
}
int maxCount = 0;
for(int i = 0;i<table.length;i++){
maxCount = Math.max(table[i],maxCount);
}
int res = (maxCount-1)*(n+1);
int temp = 0;
for(int i = 0;i<table.length;i++){
if(table[i]!=0){
temp++;
}
}
return Math.max((res+temp),tasks.length);
}
}
思路
先构造图,然后用dfs遍历图,在遍历的时候判断图是否有圈,有圈就说明是不可能上完的。
代码
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int[] flages = new int[numCourses];//记录当前节点是否被访问过。正在访问是1,访问后是2.
List<ArrayList<Integer>> graph = new ArrayList(numCourses);
for(int i = 0;i<numCourses;i++){
graph.add(new ArrayList<Integer>());
}
//以上是初始化图的存储结构.
for(int i = 0;i<prerequisites.length;i++){
int course = prerequisites[i][0];
int pre = prerequisites[i][1];
// 下标为course的数组里面的内容是course的所有前置课程
graph.get(course).add(pre);
}
//以上是将图中赋值。
//下面用dfs遍历图,并且判断图中是否有圈
for (int i = 0; i < numCourses; i++) {
if (dfs(i, flages, graph) == true) {
return false;
}
}
return true;
}
public boolean dfs(int cur, int[] flages, List<ArrayList<Integer>> graph){
if(flages[cur] == 1){
return true;
}
if(flages[cur] == 2){
return false;
}
flages[cur] = 1;
for(Integer preNode:graph.get(cur)){
if(dfs(preNode,flages,graph) == true){
return true;
}
}
flages[cur] = 2;
return false;
}
}
边栏推荐
- 一道[CSCCTF 2019 Qual]FlaskLight的详解再遇SSTI
- leetcode单调栈经典例题——最大矩形
- 低代码应用开发的五大好处
- .NET深入解析LINQ框架(五:IQueryable、IQueryProvider接口详解)
- OAK-FFC-4P全网首次测试
- async - await
- Detailed Explanation of Addresses Delivered by DHCP on Routing/Layer 3 Switches [Huawei eNSP]
- 用OpenGL绘制winXP版扫雷的笑脸表情
- MindSpore:model.train中的dataset_sink_mode该如何理解?
- 王爽汇编语言第四章:第一个程序
猜你喜欢
【论文笔记】Delving into the Estimation Shift of Batch Normalization in a Network
加降息与BTC流动性事件策略研究
字符串相关题目
注意力机制
请你谈谈网站是如何进行访问的?【web领域面试题】
recursive thinking
[Punctuality Atom STM32 Serial] Chapter 4 STM32 First Experience Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
DOM简述
DNS 查询原理详解—— 阮一峰的网络日志
leetcode二叉树系列(一)
随机推荐
oracle sql 多表查询
TCP的四次挥手
2022年化工自动化控制仪表考试模拟100题及模拟考试
The separation configuration Libpq is supported, speaking, reading and writing
有了这篇 Kubernetes 的介绍,它的原理秒懂!
我和 TiDB 的故事 | TiDB 对我不离不弃,我亦如此
It is found that several WRH tables are locked, what should I do?
About Oracle RAC 11g rebuilding the disk group
Interview at 14:00 in the afternoon, I came out at 14:08 with my head down, asking too much...
How Oracle for current library or certain library data on the same server number?
Cloud function to achieve automatic website check-in configuration details [Web function/Nodejs/cookie]
[Punctuality Atom STM32 Serial] Chapter 3 Development Environment Construction Excerpted from [Punctual Atom] MiniPro STM32H750 Development Guide_V1.1
spark算子讲解
发现WRH几个表被锁了,怎么办?
如何设计一个注册中心
TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
获取cpu的核数
Inheritance and the static keyword
用OpenGL绘制winXP版扫雷的笑脸表情
双指针方法