第一次做leetcode中Difficulty为easy的题-292
2015-11-20 22:06
190 查看
今天第一次做leetcode中的题,刚开始入门,便选择了难度为easy的292号题。一开始并没有想出来,java很久没有用也快荒废了。最后在大卓哥的提示和帮助下完成了。
题目:
Nim Game My Submissions Question
Total Accepted: 20613 Total Submissions: 41521 Difficulty: Easy
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.
For 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?
看到题目,首先想到了人工智能中的博弈论问题,但是题目中已经明确了,每个参与者都很clever,那么肯定会做出最明智的决策,所以不需要我给出决策。
分析这个题目,如果在我选择的前,剩下的是4个,那我肯定就会输。如果我想赢,必须让对方在最后一次选择前剩下4个,那么我选择前剩下的是5、6、7个时,我都可以让对方输;但是如果剩下8个,无论我怎么选,对方都可以给我剩下4个。以此类推,如果到我选择时剩下的是4的倍数,无论我选几个,对方都可以让我每次选择前剩下的都是4的倍数,那么我就必输无疑了。
下面是代码:
public class Solution {
public boolean canWinNim(int n) {
if (n<=0||n%4==0){
return false;
}
else{
return true;
}
}
}
总结:
1.代码非常简单,但是由于长时间没有使用Java(平时写算法都用对语法不严格的MATLAB),最基本的输入输出以及方法的定义都不记得了。应加强训练。
2.题目本身并不难,在解决问题的时候注意不要将问题复杂化。
题目:
Nim Game My Submissions Question
Total Accepted: 20613 Total Submissions: 41521 Difficulty: Easy
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.
For 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?
看到题目,首先想到了人工智能中的博弈论问题,但是题目中已经明确了,每个参与者都很clever,那么肯定会做出最明智的决策,所以不需要我给出决策。
分析这个题目,如果在我选择的前,剩下的是4个,那我肯定就会输。如果我想赢,必须让对方在最后一次选择前剩下4个,那么我选择前剩下的是5、6、7个时,我都可以让对方输;但是如果剩下8个,无论我怎么选,对方都可以给我剩下4个。以此类推,如果到我选择时剩下的是4的倍数,无论我选几个,对方都可以让我每次选择前剩下的都是4的倍数,那么我就必输无疑了。
下面是代码:
public class Solution {
public boolean canWinNim(int n) {
if (n<=0||n%4==0){
return false;
}
else{
return true;
}
}
}
总结:
1.代码非常简单,但是由于长时间没有使用Java(平时写算法都用对语法不严格的MATLAB),最基本的输入输出以及方法的定义都不记得了。应加强训练。
2.题目本身并不难,在解决问题的时候注意不要将问题复杂化。
相关文章推荐
- Objective-c单例模式的正确写法
- 杭电2094
- Two Sum
- 一起学mini2440裸机开发(五)--定时器0的基础实验
- Spring mvc 笔记 @Controller @ModelAttribute @SessionAttributes @ControllerAdvice
- iOS技术沙龙之 - 核心动画(1)
- 杭电1285
- 一起学mini2440裸机开发(四)--S3C2440定时器学习
- 一起学mini2440裸机开发(三)--S3C2440时钟学习
- String str=new String("abc")创建了几个对象
- NYOJ 22 素数求和问题
- Android操作HTTP实现与服务器通信
- andorid 获取短信并提取验证码
- Android应用程序权限
- 【FreeMaker】FreeMaker学习-基础
- 一起学mini2440裸机开发(二)--MDK自带的S3C2440.s分析
- 维度模型数据仓库(十九) —— 维度合并
- 通信制式知识
- opengl mfc 载入ply2文件
- 求一列数的第k大的数