下面是今天写的几道题:

292. Nim Game
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.
 bool canWinNim(int n) {
if ( <n && n< ) return true;
else if(n==) return false;
else return n% != ;
}
 
344. Reverse String
Write a function that takes a string as input and returns the string reversed.
1.C++ code  Runtime:12ms
 class Solution {
public:
string reverseString(string s) {
reverse(s.begin(),s.end());
return s;
}
};

2.C code  Runtime:4ms

 char* reverseString(char* s) {
int len = strlen(s);
for(int i=;i<len/;i++)
{
char tempc = s[i];
s[i] = s[len--i];
s[len--i] = tempc;
}
return s;
}

371. Sum of Two Integers

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -

这道题自己虽然知道要用位运算但是一开始没想出来,下边的方法借鉴网上,原文地址http://www.cnblogs.com/la0bei/p/5659829.html

 int main()
{
int a,b,c;
cin >> a >> b;
while(b)
{
c = a^b;
b = (a&b)<<;
a = c;
}
cout << a << endl;
system("pause");
}

--------------------------------------------------------------------------------------------------------------------------------------------------------

下边这个方法按道理是不符合题目的,题中要求不使用+-符号,但是这竟然在LeetCode上ac了,我这个代码需要小心边界即负数最小值。

 //整数转换为二进制数组
int intToBit(int *str,long long num)
{
int i=;
do
{
str[i++] = num%;
num = num/;
}while(num>); return i;
}
//负数的话二进制取反+1
void reverdBitAdd1(int *str,int *num)
{
//取反
int temp = *num;
while(temp--) str[temp] = ( (str[temp] == )? : ); //负数需要反转+1;
int sum = ;
for(int i=;i<*num;i++)
{
sum += str[i];
str[i] = sum%;
sum = sum/;
}
str[*num] = ;
(*num)++;
} //两个二进制相加
int bitAdd(int *sumbit,int*a,int sizea,int*b,int sizeb)
{
int sum = ;
int num = (sizea>sizeb?sizea:sizeb);
for (int i=;i<num;i++)
{
sum +=(a[i]+b[i]);
sumbit[i] = sum%;
sum /=;
}
if (sum ==) sumbit[num++] = ;
return num;
} //将二进制转换为十进制
int sumBit(int *bitNum, int n)
{
int sum = ;
int bit2 = ;
for(int i=;i<n;i++)
{
sum += bitNum[i]*bit2;
bit2 *=;
}
return sum;
} int getSum(int a, int b) {
//定义存储结果的sum,标识a,b的符号的falg
int sum = ,flaga = ,flagb = ;
//定义存储ab以及相加后的二进制数组
int bita[] = {};
int bitb[] = {};
int sumbit[] = {};
//考虑a,b可能为负数最小值
long long la = a;
long long lb = b;
//先按无符号位处理
if(la<)
{
la = -la;
flaga = ;
} if(lb<)
{
lb = -lb;
flagb = ;
} int sizea = intToBit(bita,la);
int sizeb = intToBit(bitb,lb); //如果符号相同直接相加
if(flaga == flagb)
{
int num = bitAdd(sumbit,bita,sizea,bitb,sizeb);
sum = sumBit(sumbit,num);
if(flaga == ) sum = -sum;
}
//如果a是负数
else if(flaga == )
{
//先将a反转+1,此时a多了个符号位
reverdBitAdd1(bita,&sizea);
while(sizea<sizeb)//补齐负数位
{
bita[sizea++] = ;
}
int num = bitAdd(sumbit,bita,sizea,bitb,sizeb);
if(sizeb>sizea-) sum = sumBit(sumbit,num-);//正数二进制位多
else if(sizea->sizeb)//负数符号位多
{
num--;
reverdBitAdd1(sumbit,&num);
sum = -sumBit(sumbit,num-);
}
else if(sizea- == sizeb)//不包括符号位两个位数一样多
{
if(sumbit[num] == ) sum = -sumBit(sumbit,num-);
else sum = sumBit(sumbit,num-);
}
}
else if(flagb == )
{
reverdBitAdd1(bitb,&sizeb);
while(sizeb<sizea)
{
bitb[sizeb++] = ;
}
int num = bitAdd(sumbit,bita,sizea,bitb,sizeb);
if(sizea>sizeb-) sum = sumBit(sumbit,num-);
else if(sizeb->sizea)
{
num--;
reverdBitAdd1(sumbit,&num);
sum = -sumBit(sumbit,num-);
}
else if(sizeb- == sizea)
{
if(sumbit[num] == ) sum = -sumBit(sumbit,num-);
else sum = sumBit(sumbit,num-);
}
}
return sum;
}

Nim Game,Reverse String,Sum of Two Integers的更多相关文章

  1. Redis 命令,键(key),字符串(String),哈希(Hash),列表(List),集合(Set)(二)

      Redis 命令 Redis 命令用于在 redis 服务上执行操作. 要在 redis 服务上执行命令需要一个 redis 客户端.Redis 客户端在我们之前下载的的 redis 的安装包中. ...

  2. Object c的NSString的使用,创建,拼接和分隔,子string,substring

    main: // //  main.m //  StringDemo // //  Created by 千 on 16/9/22. //  Copyright © 2016年 kodulf. All ...

  3. 把List<string>集合,编程string,并以“,”号分割

    List<int> roleIdList = GetRoleIdList(user.ID); string roleIdsStr = ""; if (roleIdLis ...

  4. Delphi在创建和使用DLL的时候如果使用到string,请引入ShareMem单元

    当使用了长字符串类型的参数.变量时,如string,要引用ShareMem. 虽然Delphi中的string功能很强大,但若是您编写的Dll文件要供其它编程语言调用时,最好使用PChar类型.如果您 ...

  5. python3.x Day4 内置方法,装饰器,生成器,迭代器

    内置方法,就是python3提供的各种函数,可以认为是关键字,帮助进行一些列的牛x运算. abs()#取绝对值 all([])#可迭代对象中的所有元素都为True 则为True,只要至少一个为Fals ...

  6. 面向对象编程(四)继承,概念及super关键字,final关键字,Object类常见方法

    继承 概念: ①   继承背后的思想就是基于已存在的类来构建新类; ②   当从已存在类继承时,就重用了它的方法和属性,还可以添加新的方法和属性来定制新类以应对需求; ③   当从其它类导出的类叫作子 ...

  7. gvim -- 跳转命令,查找格式,正则

    1.跳转命令 ‘w'单词前进,'b'单词后退,'e'单词前进,‘ge’单词后退,存在单词词首词尾区别,'W''B''E''gE'将不以单词区分,以空格区分 ‘$’行尾,'^'非空白行首,'0'行首 ‘ ...

  8. LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers

    344. Reverse String /** * @param {string} s * @return {string} */ var reverseString = function(s) { ...

  9. 【JavaScript框架封装】使用Prototype给Array,String,Function对象的方法扩充

    /* * @Author: 我爱科技论坛* @Time: 20180705 * @Desc: 实现一个类似于JQuery功能的框架* V 1.0: 实现了基础框架.事件框架.CSS框架.属性框架.内容 ...

随机推荐

  1. Servlet向客户端发送中文数据的编码情况

    (更多内容请关注本人微信订阅号:it_pupil) 本文讲述服务端servlet向客户端浏览器发送中文数据的编码情况,需要抓住下面几点: 输出流发送数据,必须是以字节形式传输的.也就是说,如果你在服务 ...

  2. Why jsp?

    Before the JSP come into the world . The CGI and servlet took the responsibility of generating dynam ...

  3. NOIP 考前DP 复习

    POJ 2533 最长不降子序列 #include <cstdio> ; int a[Maxn],Pos[Maxn],F[Maxn],n,Ans; inline int Max(int x ...

  4. Cisco ASA 配置案例---anyconnect拨通后所有流量从服务器端出去

    一.目的: 1.Cisco ASA之Lan端能正常上网. 2.anyconnect端所有流量从Cisco ASA的Outside出去. 3.anyconnect端能访问Cisco ASA的Inside ...

  5. 无法将文件" "复制到“bin\*.*”。对路径“bin\*.*”的访问被拒绝。 解决方法

    如果没有特别什么代码的错误,而是更新了某个自动获取的Webserive 那么什么都不用管,直接VS关闭,从新打开就好了.

  6. Caffe 源碼閱讀(五) Solver.cpp

    1.Solver类两个构造函数 Solver(const SolverParameter& param) Solver(const string& param_file) 初始化两个类 ...

  7. HDU 1839

    http://acm.hdu.edu.cn/showproblem.php?pid=1839 题意:从1到n,要求时间小于等于T到达.每条边有一个容量,问最多能运多少货物. 分析:最多能运的货物取决于 ...

  8. Azure Media Service

    该视频来源于Build 2015, 视频比较老, 从演讲的角度看, 是个非常不错的演讲, 内容也很全面. Apr 27, 2015

  9. 。net新人报道

    入行一年多  关注博客园也有半年的时间了 今天开始写第一篇东西  以后有什么笔记也会慢慢写上来的

  10. 用Android Studio 出现的问题

    解决的办法是cmd--恢复网络设置---netsh winsock reset----重启电脑解决.