390 Elimination Game 淘汰游戏

详见: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, false);
        }
        else
        {
            return 2 * help(n / 2, true) - 1 + n % 2;
        }
    }
};

方法二:

class Solution {
public:
    int lastRemaining(int n) {
        return n==1?1:2*(1+n/2-lastRemaining(n/2));
    }
};

方法三:

class Solution {
public:
    int lastRemaining(int n) {
        int base = 1, res = 1;
        while (base * 2 <= n)
        {
            res += base;
            base *= 2;
            if (base * 2 > n)
            {
                break;
            }
            if ((n / base) % 2 == 1)
            {
                res += base;
            }
            base *= 2;
        }
        return res;
    }
};

详见:https://www.cnblogs.com/grandyang/p/5860706.html

原文地址:https://www.cnblogs.com/xidian2014/p/8849486.html

时间: 2024-08-29 09:43:39

390 Elimination Game 淘汰游戏的相关文章

[LeetCode] 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 ri

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 ri

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 ri

【leetcode】390. Elimination Game

题目如下: 解题思路:对于这种数字类型的题目,数字一般都会有内在的规律.不管怎么操作了多少次,本题的数组一直是一个等差数列.从[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以及序列的长度,直到最后序列只有一

游戏的定价

现在的游戏真的便宜得令人惊诧了.最近花了一元在APP Store上购入了Horizon Chase这个赛车游戏,又在Steam上花了17元买了蝙蝠侠阿卡姆起源.而且这两个游戏都是诚意满满的大作,这价钱,和以前读书时买的盗版光碟也别无二致了. 以前国产单机消亡,很多人把原因归咎于盗版.因为国外的3A大作都可以通过极低的价钱买到,那不如3A大作的国产单机游戏,动辄几十上百,又怎能吸引人去买呢?实际上道理确实是有的,但是现在,就算人人都培养起了正版意识,但是新的问题放在了国产单机游戏的面前,那就是现在

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

hdu5643, 递归求解约瑟夫环问题

King's Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 832    Accepted Submission(s): 460 Problem Description In order to remember history, King plans to play losephus problem in the parade

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea