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 ...
随机推荐
- (转)linux常用查看硬件设备信息命令
场景:在一篇博客中看到这些总结性命令,感觉很实用就转载,以后用到进行相关的整理 系统 # uname -a # 查看内核/操作系统/CPU信息 # lsb_release -a # 查看操作系统版本 ...
- 【HTML】谈谈html的meta标签
一.定义&用法 <meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. <meta> 标签位于文档的头 ...
- js常用内置对象及方法
在js中万物皆对象:字符串,数组,数值,函数...... 内置对象都有自己的属性和方法,访问方法如下: 对象名.属性名称: 对象名.方法名称 1.Array数组对象 unshift( ) 数组开 ...
- Swift学习之元组(Tuple)
定义 元组是由若干个类型的数据组成,组成元组的数据叫做元素,每个元素的类型都可以是任意的. 用法一 let tuples1 = ("Hello", "World" ...
- 如何编写轻量级 CSS 框架
Github 地址: https://github.com/nzbin/snack Demo 演示: https://nzbin.github.io/snack/ 前言 这篇文章我已经酝酿了半年之久, ...
- iOS底层学习-KVC使用实践以及实现原理
简介 KVC(Key-value coding)键值编码,顾名思义.额,简单来说,是可以通过对象属性名称(Key)直接给属性值(value)编码(coding)"编码"可以理解为& ...
- CODE大全告诉你java是否开始没落了
CODE大全告诉你java是否开始没落了! 22 岁,对于一个技术人来说可谓正当壮年.但对于一门编程语言来说,情况可能又有不同.各类编程语言横空出世,纷战不休,然而 TIOBE 的语言排行榜上,Jav ...
- WebApi Ajax 跨域请求解决方法(CORS实现)
概述 ASP.NET Web API 的好用使用过的都知道,没有复杂的配置文件,一个简单的ApiController加上需要的Action就能工作.但是在使用API的时候总会遇到跨域请求的问题, 特别 ...
- 日常API之C#百度人脸识别
最近看到一只我家徒儿发来的链接,原来是一堆百度AI的SDK,于是一时兴起就做了一只人脸识别,喵喵喵(●'◡'●) 一.准备工作 首先,当然是下载SDK啦:http://ai.baidu.com/sdk ...
- SQL Server数据库基础笔记
启动和停止SQL Server服务三种形式 后台启动服务 计算机->右键->管理->服务和应用程序->服务->sql server(MSSQLSERVER) SQL Se ...