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 ...
随机推荐
- 配置HTTPS服务
环境为CentOS 7.3.httpd2.4.6 一 搭建证书 说明: CA 主机为192.168.29.3 client主机为 192.168.29.100 1 生成私钥 [root@centos7 ...
- 【jquery】ajax请求
1. defferred对象 实现链式回调函数编程. http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_d ...
- Asp.Net MVC-4-过滤器1:认证与授权
基础 过滤器体现了MVC框架中的Aop思想,虽然这种实现并不完美但在实际的开发过程中一般也足以满足需求了. 过滤器分类 依据上篇分析的执行时机的不同可以把过滤器按照实现不同的接口分为下面五类: IAu ...
- Flunetd 用于统一日志记录层的开源数据收集器
传统的日志查看方式 使用fluentd之后 一.介绍 Fluentd是一个开源的数据收集器,可以统一对数据收集和消费,以便更好地使用和理解数据. 几大特色: 使用JSON统一记录 简单灵活可插拔架构 ...
- ExtJs的expand和collapse
最近在研究ExtJs的窗口组件(Ext.window),关于扩展显示expand和折叠显示collapse的一点心得记录一下,以便后查. var win2 = new Ext.window({ id ...
- RSA简介(一)——数论原理
RSA是最常用的非对称加密算法. 所谓非对称加密,就是说有两个密钥,一个密钥加密只可以用另外一个密钥解密,一般一个作为公钥,公开给所有人用来加密用,而另一个用来解密其他拥有公钥的加密结果,叫做私钥.另 ...
- OpenCV1.0在VC ++6.0下的配置
1.本人使用win7操作系统,首先要预装VC++6.0,安装方法不再赘述. 2.在OpenCV官方网站下载OpenCV的安装文件"OpenCV_1.0.EXE"(参考链接:http ...
- CentOS编译PHP过程中常见错误信息的解决方法
原文链接:http://www.linuxidc.com/Linux/2014-05/102327.htm ********************************************** ...
- Windows下搭建Git 服务器: BONOBO GIT SERVER + TortoiseGit
本文将介绍如何在Windows操作系统下搭建Git服务器和客户端.服务器端采用的是Bonobo Git Server,一款用ASP.NET MVC开发的Git源代码管理工具,界面简洁,基于Web方式配 ...
- .net操作IIS,新建网站,新建应用程序池,设置应用程序池版本,设置网站和应用程序池的关联
ServerManager类用来操作IIS,提供了很多操作IIS的API.使用ServerManager必须引用Microsoft.Web.Administration.dll,具体路径为:%wind ...