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. Leetcode_238_Product of Array Except Self

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/48598939 Given an array of n in ...

  2. 在GitHub上创建代码仓库

    目前在GitHub上管理托管带代码的人越来越多了,今天也尝试了一次,顺便记下来,备用. 首先是在GitHub上创建一个代码仓库,创建完之后,GitHub上会有提示,这时进入项目目录执行下面的命令,顺便 ...

  3. BASE64 的加密与解密

    package com.suning.security; import java.io.IOException; import sun.misc.BASE64Decoder; import sun.m ...

  4. MOOS学习笔记——多线程

    /* * A simple example showing how to use a comms client */ #include "MOOS/libMOOS/Comms/MOOSAsy ...

  5. Fragment生命周期与Fragment执行hide、show后的生命周期探讨

    一.Fragment 生命周期中的每个方法的意义与作用: 1.setUserVisibleHint()(此方法不属于生命周期方法):设置Fragment 用户可见或不可见时调用此方法,此方法在Frag ...

  6. jQuery监听事件经典例子

    关键字:jQuery监听事件经典例子  js代码:  ============================================================  $(function( ...

  7. Spring AOP四种实现方式Demo详解与相关知识探究

    一.前言 在网络上看到一篇博客Spring实现AOP的4种方式,博主写的很通俗易懂,但排版实在抓狂,对于我这么一个对排版.代码格式有强迫症的人来说,实在是不能忍受~~~~(>_<)~~~~ ...

  8. .net捕捉全局未处理异常的3种方式

    前言: 我们在实际项目开发中,经常会遇到一些不可预见的异常产生,有的异常在程序运行时就对其进行处理(try)但是,有的程序不需要每一个地方都用try进行处理,那么针对这种情况,可以参照下面的方式,实现 ...

  9. PHP采用Cookie实现 购物车

    先来看一下逻辑分析图:

  10. php面向对象中的魔术方法

    原创,转载请注明出处 在 PHP 中以两个下划线开头的方法,__construct(), __destruct (), __call(), __callStatic(),__get(), __set( ...