[leetcode] 390 Elimination Game
很开心,自己想出来的一道题
There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.
Repeat the previous step again, but this time from right to left, remove the right most number and every other number from the remaining numbers.
We keep repeating the steps again, alternating left to right and right to left, until a single number remains.
Find the last number that remains starting with a list of length n.
Example:
Input:
n = 9,
1 2 3 4 5 6 7 8 9
2 4 6 8
2 6
6 Output:
6
public class Solution {
public int lastRemaining(int n) {
int start = 1;
int end = n;
int duration = 1;
boolean flag = true;
while (start < end) {
int x = (end - start + duration) / duration;
if (flag) {
if (x % 2 == 1) {
end -= duration;
}
start += duration;
flag = false;
} else {
if (x % 2 == 1) {
start += duration;
}
end -= duration;
flag = true;
}
duration *= 2;
}
return start;
}
}
[leetcode] 390 Elimination Game的更多相关文章
- [LeetCode] 390. Elimination Game 淘汰游戏
There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number ...
- 【LeetCode】390. Elimination Game 解题报告(Python)
[LeetCode]390. Elimination Game 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/elimina ...
- 【leetcode】390. Elimination Game
题目如下: 解题思路:对于这种数字类型的题目,数字一般都会有内在的规律.不管怎么操作了多少次,本题的数组一直是一个等差数列.从[1 2 3 4 5 6 7 8 9] -> [2 4 6 8] - ...
- 390. Elimination Game
正规解法直接跳到代码上面一点的部分就可以了.但我想记录下自己的思考和尝试过程,希望二刷能看到问题所在. 找规律的时候写了好多,虽然规律很简单. 只要随便写3以上的例子,就应该发现,相邻的2个最后结果是 ...
- 390 Elimination Game 淘汰游戏
详见:https://leetcode.com/problems/elimination-game/description/ C++: 方法一: class Solution { public: in ...
- leetcode 390. 消除游戏
{20-01-29 19:22} class Solution { public int lastRemaining(int n) { return help(n); } public static ...
- Java实现 LeetCode 390 消除游戏
390. 消除游戏 给定一个从1 到 n 排序的整数列表. 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾. 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始,每隔一个数 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- Oracle 中 decode 函数用法
Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...
- 用VC2010以上版本编译可以在低版本XP和2003的运行程序的方法
2013-09-17 作者:佚名 来源:本站整理 浏览:2001 评论:1 一直以来倍受此事困拢,vc2010以上版本编译出的exe或dll总是会引用kernel32.dll的En ...
- 金字塔Lucas-Kanande光流算法实现
// Lucas-Kanade method Optical Flow in OpenCV // BJTShang, 2016-12-13 #include <cv.h> #include ...
- 【Android自学日记】【转】Android Fragment 真正的完全解析(上)
自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fragment谈上关系,做什么都要问下Fragment能实现不~~~哈哈,是不是有点过~~~ 本篇博客力求为大家说明Fragment如何产 ...
- highcharts
preparation Highcharts Highcharts是一个制作图表的纯Javascript类库,主要特性如下: 兼容性:兼容当今所有的浏览器,包括iPhone.IE和火狐等等: 对个人用 ...
- AJAX获取数据成功后的返回数据如何声明成全局变量
var result=""; $.ajax({ type: "post", url: "../reportRule/main.do?method=se ...
- Unhandled Exception:System.DllNotFoundException: Unable to load DLL"**":找不到指定的模块
在项目中使用C#代码调用C++ DLL时.常常会出现这个问题:在开发者自己的电脑上运行没有问题,但是部署到客户电脑上时会出现下面问题: Unhandled Exception:System.DllNo ...
- cat命令
[cat] 合并文件和打印到标准输出 命令格式: cat [OPTION]... [FILE]... 命令功能: 拼接文件或者做标准输入输出 命令格式: cat [OPTION].. ...
- Java字节流和字符流区别
1.字节流:直接操作文件本身. 2.字符流:通过缓冲区来操作文件. 所有的文件在硬盘或在传输时都是以字节的方式进行的,包括图片等都是按字节的方式存储的,而字符是只有在内存中才会形成,所以在开发中,字节 ...
- 《A Convolutional Neural Network Cascade for Face Detection》
文章链接: http://pan.baidu.com/s/1bQBJMQ 密码:4772 作者在这里提出了基于神经网络的Cascade方法,Cascade最早可追溯到Haar Feature提取 ...