下面是今天写的几道题:

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. javascript运动应用

    多物体运动框架: 1.多个盒子同时运动:move(obj,target)多一个参数 <!DOCTYPE html><html><head> <title> ...

  2. mov和ldr/str的区别

    ARM是RISC结构,数据从内存到CPU之间的移动只能通过L/S指令来完成,也就是ldr/str指令.比如想把数据从内存中某处读取到寄存器中,只能使用ldr比如:ldr r0, 0x12345678就 ...

  3. React Native工作小技巧及填坑记录

    以下是本人在React Native开发工作中使用的一些小技巧,记录一下. 1.从网络上拉取下来的React Native缺少React和React Native库. 终端 1. cd 项目根目录 2 ...

  4. DOM和IE中的 事件对象

    DOM中的事件对象:(符合W3C标准)    preventDefault()        取消事件默认行为    stopImmediatePropagation() 取消事件冒泡同时阻止当前节点 ...

  5. 关于JAVA学习计划和感想

    学习计划第一阶段:    JAVA语言基础知识.包括异常.IO流.多线程.集合类.    要求:异常------掌握try-catch-finally的使用          IO流------掌握字 ...

  6. LinQ递归查询

    --由父项递归下级 with cte(refid,pid,zname,code) as (--父项 union all --递归结果集中的下级 select t.refid,t.pid,t.zname ...

  7. 实现TabelView的多个cell布局

    - (void)viewDidLoad { [super viewDidLoad]; self.LabelArray=[[NSMutableArray alloc]initWithObjects:@& ...

  8. ASP.NET MVC中多种ActionResult用法总结

    最近一段时间做了个ASP.NET MVC4.0的项目,项目马上就要结束了,今天忙里偷闲简单总结一下心得: 1. 如果Action需要有返回值的话,必须是ActionResult的话,可以返回一个Emp ...

  9. haslayout

    什么是 haslayout ? haslayout 是Windows Internet Explorer渲染引擎的一个内部组成部分.在Internet Explorer中,一个元素要么自己对自身的内容 ...

  10. php读取json时无数据(为空)的解决方法

    在使用PHP调用一些json接口文件时 如果使用 file_get_contents 获取页面json数据后 再使用json_decode()解析后 数据无法正常输出 这是的返回值为null 这是由于 ...