Problem:

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.

Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.

Example:

if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.

Hint:

If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?

Code:

class Solution {
public:
bool canWinNim(int n) {
return !(n % 4 == 0);
}
};

说明:

这里用到的是1+3=4,就是当4出现在你面前时,你必失败,这样的话,每多一个4,你的对手都会将他消除,所以,只要是4的倍数,就必输。而只要不是4的倍数,你就可以将它变成4的倍数,从而必胜。

Problem

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.

Example:

Given nums = [-2, 0, 3, -5, 2, -1]

sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3

Code

class NumArray {
public:
NumArray(vector<int> &nums) {
data = nums;
} int sumRange(int i, int j) {
if (i < 0 || j >= data.size() || i > j) {
return 0;
}
int result = 0;
for (int k = i; k <= j; k++) {
result += data[k];
}
return result;
}
private:
vector<int> data;
}; // Your NumArray object will be instantiated and called as such:
// NumArray numArray(nums);
// numArray.sumRange(0, 1);
// numArray.sumRange(1, 2);

说明:

这个应该没啥可说的,就是初始化好了,取相应下标之间的数作和,不过第一次提交失败了,踩了java的坑了,定义成员变量用的是nums,直接this.nums了,,应该是this->nums的。

感觉似乎有点与初衷相悖了,因为总是从自己的私库里取东西发表,应该写新的的。

leetcode简单题目两道(1)的更多相关文章

  1. leetcode简单题目两道(2)

    Problem Given an integer, write a function to determine if it is a power of three. Follow up: Could ...

  2. leetcode简单题目两道(4)

    心情还是有问题,保持每日更新,只能如此了. Problem Given a binary tree, return the level order traversal of its nodes' va ...

  3. leetcode简单题目两道(3)

    本来打算写redis的,时间上有点没顾过来,只能是又拿出点自己的存货了. Problem Given an array nums, write a function to move all 's to ...

  4. leetcode简单题目两道(5)

    Problem Given an integer (signed bits), write a function to check whether it . Example: Given num = ...

  5. CTF中关于XXE(XML外部实体注入)题目两道

    题目:UNCTF-Do you like xml? 链接:http://112.74.37.15:8008/ hint:weak password (弱密码) 1.观察后下载图片拖进WINHEX发现提 ...

  6. 两道面试题,带你解析Java类加载机制

    文章首发于[博客园-陈树义],点击跳转到原文<两道面试题,带你解析Java类加载机制> 在许多Java面试中,我们经常会看到关于Java类加载机制的考察,例如下面这道题: class Gr ...

  7. 【转】两道面试题,带你解析Java类加载机制(类初始化方法 和 对象初始化方法)

    本文转自 https://www.cnblogs.com/chanshuyi/p/the_java_class_load_mechamism.html 关键语句 我们只知道有一个构造方法,但实际上Ja ...

  8. 『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar && Malek and Summer Semester

    这几天一直在宿舍跑PY模型,学校的ACM寒假集训我也没去成,来学校的时候已经18号了,突然加进去也就上一天然后排位赛了,没学什么就去打怕是要被虐成渣,今天开学前一天,看到最后有一场大的排位赛,就上去试 ...

  9. 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制

    你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...

随机推荐

  1. [修正] Firemonkey 中英文混排折行,省略字符,首字避开标点

    问题:FMX 在移动平台的文字显示并非由该平台的原生 API 来显示,而是由 FMX.TextLayout.GPU 来处理,也许是官方没留意到中文字符的问题,造成在中英文混排折行时,有些问题. 修正: ...

  2. Windows上编译LLVM 3.2

    LLVM还是做的很好的,可以很方便的编译. 解压LLVM 3.2到[工作目录]/llvm/llvm-3.2.src 解压Clang到[工作目录]/llvm/llvm-3.2.src/tools/cla ...

  3. mongodb 连接字符串

    mongodb 连接字符串 <!--连接字符串--> <add key="ts.students" value="mongodb://192.168.0 ...

  4. 4.Python的版本

    Python2: 英文支持没问题,中文报错,默认编码:ascii码 显示中午需要添加代码在首行:# -*- encoding:utf -8 -*- 用户交互  raw_input python2 里还 ...

  5. php 多维数据根据某个或多个字段排序

    实现多维数组的指定多个字段排序 上面的实例讲解了如何实现多维数组指定一个字段排序,但如果要实现指定多个字段来对数组进行排序该如何思考? 多个字段是几个?2个,3个或更多,所以这个不确定的因素需要排除. ...

  6. kali linux之Backdoor-factory

    Backdoor-------python编写 适用于windows PE x32/x64 和Linux ELF x32/x64(OSX),支持msf payload 自定义payload 将shel ...

  7. Tomcat企业级应用

    简介 Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun 和其他一些公司及个人共同开发而成.由 ...

  8. BZOJ1688 Disease Manangement 疾病管理

    Disease Manangement 疾病管理   Description Alas! A set of D (1 <= D <= 15) diseases (numbered 1..D ...

  9. 基础篇:6.3)形位公差-标注 Mark

    本章目的:了解形位公差是如何标注的,及其意义: 1.形位公差框格 Feature Control Frames 2.被测要素的标注(两国标准不同) 2.1 中国GB标准 — 形位公差框格通过用带箭头的 ...

  10. 高阶篇:4.1.2.3)产品零件级别的QFDII

    本章目的:介绍产品零件级别的QFDII编写方法. 1.前言 这章接前面部件级别的QFDII. 产品零件级别的QFDII,其实就是将零件QFDII所得到的设计要求,进一步分配零件的特征(Part Cha ...