344. Reverse String

/**
* @param {string} s
* @return {string}
*/
var reverseString = function(s) {
return s.split("").reverse().join("");
};

292. Nim Game

尼姆游戏还是很有意思的,这题有很多地方可以深入理解

/**
* @param {number} n
* @return {boolean}
*/
var canWinNim = function(n) {
if(0 === n%4){
return false;
}
return true; }; //这题解题的过程中,LeetCode提示说和0进行比较的时候使用===而不是==

371. Sum of Two Integers

这题主要考验的是位运算

①异或xor的逆运算是它本身,两次异或同一个数时结果不变。在学习位运算的时候,我们知道XOR的一个重要特性是不进位加法,那么只要再找到进位,将其和XOR的结果加起来,就是最后的答案。/**

 * @param {number} a
* @param {number} b
* @return {number}
*/
var getSum = function(a, b) {
var c1 = a^b;
var d = a&b;
while(d!==0){
d = d<<1;
c2 = c1^d;
d = c1&d;
c1 = c2;
} return c1;
}; //总之就是
0 1 1 — a ==3
1 0 1 — b ==5
——————————
1 1 0 — c1
0 0 1 — d
0 1 0 — d=d<<1
1 0 0 — c2=c1^d
0 1 0 — d=c1&d
1 0 0 — c1=c2
——————————
1 0 0 — d=d<<1
0 0 0 — c2=c1^d
1 0 0 — d=c1&d
0 0 0 — c1=c2
——————————
1 0 0 0 — d=d<<1
1 0 0 0 — c2=c1^d
0 0 0 0— d=c1&d
1 0 0 0 — c1=c2
//关键就是异或—不进位加法,与运算—进位处为1,与运算再<<1则相当于进位数, a,b两个数字将axorb后再加上a&b的值,一直重复到没有进位(a&b==0)就计算完毕。

LeetCode Javascript实现 344. Reverse String 292. Nim Game 371. Sum of Two Integers的更多相关文章

  1. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  2. Leetcode#344. Reverse String(反转字符串)

    题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...

  3. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  4. 【leetcode】344. Reverse String

    problem 344. Reverse String solution: class Solution { public: void reverseString(vector<char> ...

  5. 344. Reverse String(C++)

    344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...

  6. 344. Reverse String【easy】

    344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...

  7. 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

    昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...

  8. 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)

    剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...

  9. [LeetCode] 344. Reverse String 翻转字符串

    Write a function that reverses a string. The input string is given as an array of characters char[]. ...

随机推荐

  1. JQuery UI - resizable

     ·概述 resizable插件可以让选中的元素具有改变尺寸的功能. 官方示例地址:http://jqueryui.com/demos/resizable/ 所有的事件回调函数都有两个参数:eve ...

  2. XMPP系列(三)---获取好友列表、添加好友

    1.心跳检测.掉线重连功能 客户端和服务器端都可以设置多久发送一次心跳包,如果对方没有返回正确的pong信息,则会断开连接,而添加掉线重连功能,则会自动进行连接. 如果自己写聊天功能还得自己做心跳检测 ...

  3. 关于SharePoint2007简单随感

    首先,还是要感谢我毕业以后的这第一份正式工作,当然现在也依然在做,带我走进了SharePoint的世界,很奇妙也许是有缘吧,自己不是个努力的人,从面试的时候对Moss这个东西闻所未闻,到现在一知半解, ...

  4. asp.net mvc控制器激活全分析

    控制器的激活默认情况下使用反射来实现的,这其中采用了DI,单例等设计模式.对于控制器的主要涉及到如下的类:ControllerBuilder.DefaultControllerFactory.Defa ...

  5. python JoinableQueue在生产者消费者项目中的简单应用

    class multiprocessing.JoinableQueue([maxsize]) JoinableQueue, a Queue subclass, is a queue which add ...

  6. spring的优缺点

    它是一个开源的项目,而且目前非常活跃:它基于IoC(Inversion of Control,反向控制)和AOP的构架多层j2ee系统的框架,但它不强迫 你必须在每一层 中必须使用Spring,因为它 ...

  7. Visual Studio 2013创建自定义多项目模版

    首先附上效果图: 可以看到输入解决方案名称后,自动创建了我事先写好的架构,并且项目名及Server层名称都变了,并且依然保持了引用关系. 下面讲具体步骤: 第一步:建立解决方案,并将需要的代码全部写好 ...

  8. 升级Centos 7/6内核版本到4.12.4的方法

    一.查看那系统内核版本 二.升级内核 三.修改grub中默认的内核版本 四.重启系统并查看系统内核 公司打算上Docker服务,目前需要安装运行环境,Docker新的功能除了需要Centos 7系统之 ...

  9. The 4 Essentials of Video Content Marketing Success

    https://www.entrepreneur.com/article/243208 As videos become increasingly popular, they provide the ...

  10. tomcat 绑定ipv4端口

    在<tomcat>/bin目录打开catalina.sh,然后添加如下内容: JAVA_OPTS="$JAVA_OPTS -Djava.net.preferIPv4Stack=t ...