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 ...
随机推荐
- 【css】css3属性
1. 无需区分webkit moz等前缀的css3属性 text-shadow background 2. 需要区分前缀的css属性 border-radius box-shadow text-str ...
- 【Mysql】Mysql关键字
ADD ALL ALTER ANALYZE AND AS ASC ASENSITIVE BEFORE BETWEEN BIGINT BINARY BLOB BOTH BY CALL CASCADE C ...
- 如何使用命令行cmd执行java程序
如果你的电脑上没有像idea eclipse这类的IDE,但是因为工作需要必须要执行java代码怎么办呢? 这个时候就需要使用电脑最原始的执行方式 既命令行 1:首先你得安装了jdk与jre (这里就 ...
- 51nod 1008 N的阶乘 mod P
输入N和P(P为质数),求N! Mod P = ? (Mod 就是求模 %) 例如:n = 10, P = 11,10! = 3628800 3628800 % 11 = 10 Input 两 ...
- 解决oracle数据库删除sql语句出现^H字样
1:安装readline包 yum install readline* 2:安装源码包: rlwrap-0.30.tar.gz ./configure && make & ...
- AngularJs学习笔记3-服务及过滤器
距离上次别博客有有一段时间了,因为最近公司和个人事情比较多,也因为学习新的知识所以耽搁了,也有人说Angularjs1.5没有人用了,没必要分享,我个人感觉既然开头了我就坚持把他写完,对一些还在使用或 ...
- 如何通过binlog获取我们想要的MySql语句?
前言 MySql的binlog一般用于我们对数据的恢复,以及从数据库对主数据库的复制和更新. 假设此时我们有一个需要查询和读取Mysql最近操作DDL的信息,我们需要怎么处理? 聪明的你可能已经想到了 ...
- input file样式修改,图片预览删除功能
本篇对input file进行了修改,改成自己需要的样式,类似验证身份上传身份证图片的功能. 效果图如下: 这里主要展示上传预览图片功能,对于删除功能的html及css写的比较粗糙,对于想要精细表现这 ...
- C# Process.Start()
本文转自:http://webcache.googleusercontent.com/search?q=cache:v4Sh6GlfJPYJ:blog.csdn.net/czw2010/article ...
- [算法题] 3Sum Closest
题目内容 Given an array S of n integers, find three integers in S such that the sum is closest to a give ...