problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::elimination_game::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static int lastRemaining (int)
 

详细描述

在文件 leetcode.h178 行定义.

成员函数说明

◆ lastRemaining()

int leetcode::elimination_game::Solution::lastRemaining ( int  n)
static

在文件 leetcode.cpp302 行定义.

302 {
303 int num_amount = n;
304 int loop_cnt = 0;
305 int a0 = 1;//初项
306 int d = 1;//差
307 while(num_amount != 1) {
308 //剩下的数目不为1时
309 if(num_amount % 2 == 1) {
310 // 奇数个数字
311 a0 = a0 + d;
312 } else if(num_amount % 2 == 0) {
313 // 偶数个数字
314 const bool left_to_right = loop_cnt % 2 == 0;
315 if(left_to_right) {
316 a0 = a0 + d;
317 } else {
318 a0 = a0;
319 }
320 }
321 loop_cnt++;
322 d *= 2;
323 num_amount /= 2;
324 }
325 return a0;
326 }

被这些函数引用 leetcode::elimination_game::TEST().


该类的文档由以下文件生成: