LeetCode Weekly Contest 47
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题
665. Non-decreasing Array
- User Accepted:1054
- User Tried:1755
- Total Accepted:1080
- Total Submissions:7519
- Difficulty:Easy
Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.
We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).
Example 1:
Input: [,,] Output: True Explanation: You could modify the first to to get a non-decreasing array.
Example 2:
Input: [,,] Output: False Explanation: You can't get a non-decreasing array by modify at most one element
Note: Thenbelongs to [1, 10,000].
这是一道极其简单的题,给出一个数组,如果至多改一个数字能使它为升序排列,我们就叫它 Non-decreasing Array。
思路极其简单,如果从里面可以找出一个数,把它拿出来,剩下的数是升序的,那么返回true,如果没有这么一个数,返回false
那么怎么写呢??????我搜索了我脑中的办法,怎么从数组中根据索引删掉一个元素。。然后还得放回原来的索引位置里。。。书到用时方恨少。。。我还百度了,没有什么好办法。
我开始尝试 new 一个num.length-1的数组,用system arraryCopy的办法接上去。。但是我要把这个元素放回去又尴尬了。
于是万般险阻,我想到了List的底层就是个数组啊!!!!
我可以get(index),然后remove(index),然后又add(index,obj)进去。。完美!
于是,我开始写我的办法,脑子里大概有了个思路
1、先把数组转化成list
2、for循环 把list的第一个元素去掉,判断剩下的是不是升序,如果是,那么直接返回true,跳出遍历;如果剩下的数组不是升序,那么把第一个元素加回第一个位置(索引值是0),再进行下一个元素类似的操作;
遍历完所有元素都找不到的话,返回false
我当时是大概这么想的,但是中途出了点小错
反正我最后的代码如下
class Solution
{
public boolean checkPossibility(int[] nums)
{
List<Integer> list = new ArrayList<Integer> ();
boolean result = true; //返回的结果,先设置为true;
/**
* 把数组放到List中
*/
for(int i=0;i<nums.length;i++)
{
list.add(nums[i]);
}
/**
* 遍历List
*/
for(int j=0;j<list.size();j++)
{
int num = list.get(j); //先把要去掉的当前元素存起来
list.remove(j); //移除
for(int k=0;k<list.size()-1;k++)//遍历剩下的元素
{
if(list.get(k)>list.get(k+1)) //判断是否有一个位置是严格降序(不含等号的降序)
{
result =false; //如果是,result设为false;跳出循环,肯定不是这个数
break;
}
else result = true; //如果不是,result为true 继续循环判断下一个位置是否严格降序
}
list.add(j,num);
if(result) //执行完上面的循环判断后,如果result为true,说明本次循环去掉的索引位置为j的元素是捣乱的
break; //跳出当前循环
}
return result;
}
}
}
于是我开始看下一题6分题。。。不好意思没看懂
于是我又开始挑战下一个题8分题,我看懂了啊,去你妈的做出来肯定是没时间了,我这里先把题目贴出来
Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement:
Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly k distinct integers.
If there are multiple answers, print any of them.
Example 1:
Input: n = , k = Output: [, , ] Explanation: The [, , ] has three different positive integers ranging to , and the [, ] has exactly distinct integer: .
Example 2:
Input: n = , k = Output: [, , ] Explanation: The [, , ] has three different positive integers ranging to , and the [, ] has exactly distinct integers: and .
Note:
- The
nandkare in the range 1 <= k < n <= 104.
我就是嘴笨,也不知道怎么描述着题的意思就是给出一个 n和一个 k 然后你要把1、2、3、4。。。。。。n重新排列,让他们前面一个数都去减后面一个数,得到结果取绝对值。但是最后的结果有且仅有k个不同的值
这个问题我很快就有了想法,减出来的结果有n个,要有k个不同的值,那么有n-k个是重复的。但是不好意思,我太菜了不会写。。。
我最后看了一下,本次比赛有1054个人是有分数的。。。而我是第1052名。。。不过能做出一题就很开心了
LeetCode Weekly Contest 47的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
- 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
随机推荐
- Spring初学
一.spring体系结构spring核心组件 1.Beans(包装应用程序自定义对象Object,Object中存有数据) 2.Core (资源加载,资源抽象,建立维护与bean之间的一些关系所需的一 ...
- Carthage的安装和使用
为什么要使用Carthage CocoaPods是已存在很长时间的Cocoa依赖管理器, 那么为什么要创建Carthage呢? CoaoaPods是一套整体解决方案,我们在Podfile中指定好我们需 ...
- 【HTML】DocType
一.docType是什么 <!DOCTYPE> 声明不是 HTML 标签:它是指示 web 浏览器关于页面使用哪个 HTML 版本进行编写的指令. 在 HTML 4.01 中,<!D ...
- 解决 CefSharp WPF控件不能使用输入法输入中文的问题(代码已提交到 github)
首先,本文所有 代码已经提交到github,需要的可以直接从github获取:https://github.com/starts2000/CefSharp,希望可以帮助到有需要的朋友们. CEF 简介 ...
- Jenkins发布MVC应用程序
一个大的项目一般都会进行模块化.层次化分隔,每个模块.每个层次都可能是一个或多个工程文件组成,而且各个模块都有依赖关系,有先后顺序,先build哪个然后再build哪个都是有顺序的,如果想build一 ...
- SpringBoot初识(一)
一.什么是SpringBoot 最近几年,微服务的概念越来越火.而相信大家在搜索微服务时,映入眼帘的首先就是SpringBoot以及SpringCloud.SpringCloud提供的一套完整的微服务 ...
- cve-2017-8464 复现 快捷方式远程代码执行
cve-2017-8464 2017年6月13日,微软官方发布编号为CVE-2017-8464的漏洞公告,官方介绍Windows系统在解析快捷方式时存在远程执行任意代码的高危漏洞,黑客可以通过U盘.网 ...
- Loadrunner结果分析中连接图没有数据的设置
场景进行中,或者之后进行结果分析中,连接图表没有数据,取消选择标记选项.
- AOP通过反射获取自定义注解
自定义注解: @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component publ ...
- Jsp分页的简单制作
Jsp分页的简单制作 运行环境:jsp+tomcat+eclipse 技术:servlet+jsp+mysql 分页技术还区分两个:假分页和真分页 假分页:一次性从数据库读出表的所有数据一次性的返回给 ...