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. java-02 JDK安装与环境变量配置&安装编程IDE

    1.JDK下载安装与环境变量的配置 1.1 官方JDK 下载地址 大家可以到Oracle中国官方网站下载JDK,也可已 通过这个链接下载 (推荐大家下载1.8版本,这个版本是当前比较流行的版本) 也可 ...

  2. Replication--使用备份初始化订阅--请求订阅

    1. 修改发布属性"许从备份文件初始化"置为TRUE 脚本修改:USE [DB01]GODECLARE @publication AS sysnameSET @publicatio ...

  3. RobotFramework做接口自动化(post请求)

    接口成功时返回: { "reCorde": "SUCCESS", "data": { ", "verify": ...

  4. python版本selenium定位方式(不止八种哦)

    除了大家熟知的8种定位方式之外 1.id定位:find_element_by_id(self, id_)2.name定位:find_element_by_name(self, name)3.class ...

  5. c#中关于协变性和逆变性(又叫抗变)帮助理解

    今天回忆了之前看的<深入理解C#>这本书中的泛型章节,其中对泛型的可变性的理解.泛型可变性分两种:协变和逆变.逆变也又称为抗变. 怎么理解这两个名词的意思: ①:协变即为在泛型接口类型中使 ...

  6. List<object>进行Distinct()去重

    有时我们会对一个list<T>集合里的数据进行去重,C#提供了一个Distinct()方法直接可以点得出来.如果list<T>中的T是个自定义对象时直接对集合Distinct是 ...

  7. .net 抽象类(abstract)和接口(interface)区别

    1.抽象类    (1) 抽象方法只作声明,而不包含实现,可以看成是没有实现体的虚方法    (2) 抽象类不能被实例化    (3) 抽象类可以但不是必须有抽象属性和抽象方法,但是一旦有了抽象方法, ...

  8. chipmunk几何算法

    /* Copyright (c) 2007 Scott Lembcke * * Permission is hereby granted, free of charge, to any person ...

  9. 蛋疼的Action.c (141): undeclared identifier `LAST'异常

    之前这个脚本运行了很久都没有问题,今天突然在场景运行不了: Action.c (141): undeclared identifier `LAST' 害的老子一直在纠结,这个关联函数没有问题啊,怎么一 ...

  10. 升级Linux中的JAVA版本

    首先查看本机中的JAVA版本 如何需要卸载掉现有的JAVA版本的话,可以使用rpm -qa | grep java 和 rpm -e xxx --nodeps进行卸载 登录到JAVA官方下载界面,提供 ...