下面是今天写的几道题:

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. LeetCode 21 -- Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  2. PAT 5-8 File Transfer (25分)

    We have a network of computers and a list of bi-directional connections. Each of these connections a ...

  3. nexus7 二代 升级 android L

    折腾了半天 ,最后发现其实很简单... 1.装好windows下gdb和bootloader的驱动,注意打开usb debug,另外进入bootloader是开机按电源键和音量减小键,至于要解锁这个想 ...

  4. angular ng-if scope权限问题

    今天在一个ng-if处理的div中处理一个scope,我一开始想要打印这个选中的值,但是一直打印的是为undefined,找了一会,原来是ng-if这个指令单独开了一个作用域,它只可以继承,不可以进行 ...

  5. Myeclipse中导入新项目报叹号

    Myeclipse中导入新项目报红色叹号 原因是导入项目中,有的jar路径不对, 在上图中,先把报错的jar移除,之后将JRE开头的那个library移除,最后点击add Library,选择jre. ...

  6. anjularjs简介

    1 什么时候该用AngularJS AngularJs(后面就简称ng了)是一个用于设计动态web应用的结构框架.首先,它是一个框架,不是类库,是像EXT一样提供一整套方案用于设计web应用.它不仅仅 ...

  7. 解决getElementsByClassName的兼容性问题

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. kafka性能基准测试

    转载请注明出处:http://www.cnblogs.com/xiaodf/ 1.测试环境 该benchmark用到了六台机器,机器配置如下 l  IntelXeon 2.5 GHz processo ...

  9. Android App Build System

  10. word2vec

    makegcc word2vec.c -o word2vec -lm -pthread -O3 -march=native -Wall -funroll-loops -Wno-unused-resul ...