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 ri…
详见:https://leetcode.com/problems/elimination-game/description/ C++: 方法一: class Solution { public: int lastRemaining(int n) { return help(n, true); } int help(int n, bool left2right) { if (n == 1) { return 1; } if (left2right) { return 2 * help(n / 2,…
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 ri…
很开心,自己想出来的一道题 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,…
[LeetCode]390. Elimination Game 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/elimination-game/description/ 题目描述: There is a list of sorted integers from 1 to n. Starting from left to right, remove the first number and every other numb…
390. 消除游戏 给定一个从1 到 n 排序的整数列表. 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾. 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始,每隔一个数字进行删除,直到列表开头. 我们不断重复这两步,从左到右和从右到左交替进行,直到只剩下一个数字. 返回长度为 n 的列表中,最后剩下的数字. 示例: 输入: n = 9, 1 2 3 4 5 6 7 8 9 2 4 6 8 2 6 6 输出: 6 PS: 最下面那一行是真正大佬的思路 class S…
{20-01-29 19:22} class Solution { public int lastRemaining(int n) { return help(n); } public static int help(int n){ if(n==2)return 2; if(n==1)return 1; if(n%2==1){ return help(n-1); }else{ return 2*(n/2+1-help(n/2)); } } } 作者:daiyang 链接:https://leet…
题目如下: 解题思路:对于这种数字类型的题目,数字一般都会有内在的规律.不管怎么操作了多少次,本题的数组一直是一个等差数列.从[1 2 3 4 5 6 7 8 9] -> [2 4 6 8] -> [2 6] -> [6]这个序列中,我们可以得到公差分别是1,2,4.如果我们把n扩大一点,打印出其中每一步剩余的数组序列,我们很容易发现公差是pow(2,n)次方,发现了这个规律后,一切就水到渠成了.接下来,我们只要记录每一次操作后剩下序列的low,high以及序列的长度,直到最后序列只有一…
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and r…
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." Given a board with m by n cells, each cell has an initial state live (1…