leetcode简单题目两道(1)
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)的更多相关文章
- leetcode简单题目两道(2)
Problem Given an integer, write a function to determine if it is a power of three. Follow up: Could ...
- leetcode简单题目两道(4)
心情还是有问题,保持每日更新,只能如此了. Problem Given a binary tree, return the level order traversal of its nodes' va ...
- leetcode简单题目两道(3)
本来打算写redis的,时间上有点没顾过来,只能是又拿出点自己的存货了. Problem Given an array nums, write a function to move all 's to ...
- leetcode简单题目两道(5)
Problem Given an integer (signed bits), write a function to check whether it . Example: Given num = ...
- CTF中关于XXE(XML外部实体注入)题目两道
题目:UNCTF-Do you like xml? 链接:http://112.74.37.15:8008/ hint:weak password (弱密码) 1.观察后下载图片拖进WINHEX发现提 ...
- 两道面试题,带你解析Java类加载机制
文章首发于[博客园-陈树义],点击跳转到原文<两道面试题,带你解析Java类加载机制> 在许多Java面试中,我们经常会看到关于Java类加载机制的考察,例如下面这道题: class Gr ...
- 【转】两道面试题,带你解析Java类加载机制(类初始化方法 和 对象初始化方法)
本文转自 https://www.cnblogs.com/chanshuyi/p/the_java_class_load_mechamism.html 关键语句 我们只知道有一个构造方法,但实际上Ja ...
- 『ACM C++』Virtual Judge | 两道基础题 - The Architect Omar && Malek and Summer Semester
这几天一直在宿舍跑PY模型,学校的ACM寒假集训我也没去成,来学校的时候已经18号了,突然加进去也就上一天然后排位赛了,没学什么就去打怕是要被虐成渣,今天开学前一天,看到最后有一场大的排位赛,就上去试 ...
- 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制
你所不知道的库存超限做法 在互联网企业中,限购的做法,多种多样,有的别出心裁,有的因循守旧,但是种种做法皆想达到的目的,无外乎几种,商品卖的完,系统抗的住,库存不超限.虽然短短数语,却有着说不完,道不 ...
随机推荐
- 7z文件格式及其源码的分析(四)
这是7z文件格式及其源码的分析系列的第四篇. 上一篇讲到了7z文件静态结构的尾header部分.这一篇开始,将从7z实际压缩流程开始详细介绍7z文件尾header的详细结构. 一, 第一个概念: co ...
- 关于SVN浏览服务器的错误
这种错误是因为URL错误,需要把https://iZ1gyqtig7Z/svn/BoLeBang/ 换成自己的公网ip地址 https://xx.xx.xx.xxsvn/BoLeBang/ 就可以 ...
- 网站运维之 使用IIS日志分析器1.03.exe进行IIS服务器日志分析
引言 对于网站运维是一个比较要细心有耐心的工作,当一个网站从开发到上线后,后期的维护也很关键,特别是对于引流的网站来说更是至关重要. 对于网站运维的内容大致可以分为: SEO流量监控方面:风险防控:访 ...
- 201621123023《Java程序设计》第3周学习总结
一. 本周学习总结 写出你认为本周学习中比较重要的知识点关键词,如类.对象.封装等 关键字:面向对象,类,对象,构造函数,封装,继承 用思维导图或者Onenote或其他工具将这些关键词组织起来 二.书 ...
- 【English】20190430
Network security网络安全[ˈnetwɜːrk] [sɪˈkjʊrəti] Teradata Generic Security Service 通用安全服务[dʒəˈnerɪk] [s ...
- RDLC报表的相关技巧一(不足N行用空行补齐)
为了广泛支持客户端,系统框架运行在.Net Framework 4.0之上,Report viewer的版本也限制在11.0.3366.16. 使用NUGET安装Microsoft.ReportVie ...
- objectARX 添加线型下拉组合框空间 CAcUiLineTypeComboBox
不知道是有意还是无意,objectARX的所有文档中,居然没有CAcUiLineTypeComboBox, 而实际上这个是存在的.位于\inc\acuiComboBox.h 而在添加变量的向导中也没有 ...
- 搭建sftp并设置不同权限的多个用户
一, 设置相关用户,用户组,ssh配置文件 mkdir -pv /opt/ftpsite/{admin,user1,user2} groupadd sftpadmins groupadd sftpus ...
- rsync文件同步详解
一. 环境和测试说明 rsync(remote sync)是unix及类unix平台下的数据镜像备份软件,它不像FTP那样需要全备份,rsync可以根据数据的变化进行差异备份,从而减少数据流量,提高 ...
- React-Native 工程添加推送功能 (iOS 篇)
推送已经是是手机应用的基本功能,如果自己实现一套推送系统费时费力,所有一般我们会使用第三方的推送服务,这里我使用「极光推送」作为集成推送的例子,因为有现成的 react native 插件 jpush ...