zoukankan      html  css  js  c++  java
  • Java实现 LeetCode 390 消除游戏

    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 Solution {
        public int lastRemaining(int n) {
     boolean isNormal = true;
        int len = n;
        int start = 1;
        int step = 1;
        while (len > 1) {
            if (isNormal) {
                start = start + step;
            } else {
                start = len % 2 == 0 ? start : start + step;
            }
            step = step * 2;
            len = len / 2;
            isNormal = !isNormal;
        }
        return start;
        //  return n == 1 ? 1 : 2 * (n / 2 + 1 - lastRemaining(n / 2));
        }
    }
    
  • 相关阅读:
    jquery动画效果---animate()--滚屏
    一个前端的自我修养
    开发和测试
    jquery.find()
    c99和c++11的差异之一
    容器经典图
    C/C++中的##用法
    【心学.悟道】千圣皆过影,良知乃吾师
    memcpy, memset代码改写的方式
    三大软件原则
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13075764.html
Copyright © 2011-2022 走看看