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之“动态规划”:Interleaving String

    题目链接 题目要求: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example ...

  2. Android绘图机制(四)——使用HelloCharts开源框架搭建一系列炫酷图表,柱形图,折线图,饼状图和动画特效,抽丝剥茧带你认识图表之美

    Android绘图机制(四)--使用HelloCharts开源框架搭建一系列炫酷图表,柱形图,折线图,饼状图和动画特效,抽丝剥茧带你认识图表之美 这里为什么不继续把自定义View写下去呢,因为最近项目 ...

  3. 使用IO映射的方式获取tiny4412板子上的ID号

    在以前的文章中,有一篇 基于ARM-contexA9-Linux驱动开发:如何获取板子上独有的ID号 在那篇文章中,具体可以参考.那时候我使用了简单的字符设备驱动框架,最终的ID号通过read方法可将 ...

  4. Log4j运用于代码中

    在JAVA代码中,我们要打印输出语句的时候,我们经常会使用System.out.print(),但是在项目开发完后,这些代码就会影响项目的运行效率,所以Log4j就派上用场了.话不多说,直接上代码. ...

  5. FCL源码中数组类型的学习及排序函数Sort函数的分析

    Array 是所有数组的基类ArrayList 解决了所有Array 类的缺点    能动态扩容, 但是类型不安全的,而是会有装箱与拆箱的性能开销List<T> 则是解决了ArrayLis ...

  6. vs工具

    首页 精选版块 论坛帮助 论坛牛人 论坛地图 专家问答 CSDN > CSDN论坛 > .NET技术 > 非技术区 返回列表 管理菜单 结帖 发帖 回复 关注 [推荐] Visual ...

  7. WSL与Windows交互实践

    1. WSL是什么 2. WSL新特性 3. WSL管理配置 4. WSL交互 5. 解决方案  * 5.1 使用别名  * 5.2 多复制一份  * 5.3 重定向  * 5.4 symlink 6 ...

  8. 完美解决ScollView内嵌ListView的问题

    1.之前看了别人的一代码,解决办法是自己定制一个ListView,代码如下: public class NoScrollListView extends ListView { public NoScr ...

  9. Github Page 绑定域名

    http://kyle.xlau.org/posts/github-cname.html CNAME 创建一个CNAME文件,内容是你的域名,如: xlau.org 然后把此文件添加到Github仓库 ...

  10. sharesdk for android集成调试的几个问题

    1.一定要下载最新版,这个东西目前版本升级很频繁,证明产品本身还不稳定,最新版bug会少一点 2.下载最新版SDK的时候,跟随下载最新Sample,官网文档的示例代码及时性很差. 3.调试的几个Key ...