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) {
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的更多相关文章
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- Leetcode#344. Reverse String(反转字符串)
题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
- 【leetcode】344. Reverse String
problem 344. Reverse String solution: class Solution { public: void reverseString(vector<char> ...
- 344. Reverse String(C++)
344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...
- 344. Reverse String【easy】
344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...
- 通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)
昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, b ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- [LeetCode] 344. Reverse String 翻转字符串
Write a function that reverses a string. The input string is given as an array of characters char[]. ...
随机推荐
- sql记录去重(SQL查询或者删除表中重复记录)
.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 select*from people where peopleIdin (select peopleIdfrom peopl ...
- android 实现淘宝收益图的折线
实现的效果我一会贴上,我先说下原理,我们知道要实现在canvas上画线,不就是要搞一个paint嘛,然后首先肯定要设置下paint的属性,那么画文字呢,不就是Textpaint吗, 对,就是这么简单, ...
- maximun-depth-of-binary-tree
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- IIR滤波器软件实现(Matlab+C++)
使用C++来写一个IIR滤波器 我们首先要在MATLAB中设计一个IIR滤波器,并生成一个头文件,这个头文件中反映了IIR滤波器的频率响应特性 理论支持 IIR滤波叫做递归滤波器,它是一种具有反馈的滤 ...
- Day13 CSS的与应用
老师博客:http://www.cnblogs.com/yuanchenqi/articles/6856399.html 1,CSS选择器的应用: CSS规则有两个主要部分构成:选择器,以及一条或多条 ...
- python中的类机制
一.python中的对象 1.python中对象种类及关系 <type 'type'>:该对象可以成为其他类的类型,python中几乎所有对象都是直接或间接由<type 'type' ...
- 【转】地球坐标系 (WGS-84) 到火星坐标系 (GCJ-02) 的转换算法
// // Copyright (C) 1000 - 9999 Somebody Anonymous // NO WARRANTY OR GUARANTEE // using System; name ...
- 导出excel 的方法及示例
一.基本知识 1.Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 2. HSSF 是Horribl ...
- 当配置 DispatcherServlet拦截“/”,SpringMVC访问静态资源的三种方式
如何你的DispatcherServlet拦截 *.do这样的URL,就不存在访问不到静态资源的问题.如果你的DispatcherServlet拦截“/”,拦截了所有的请求,同时对*.js,*.jpg ...
- 解密TTY
本文内容来自The TTY demystified ,讲述了*NIX系统中TTY的历史与工作原理,看完后解决了我很多疑惑,于是做此翻译,与大家分享. 译者:李秋豪 江家伟 审校: V1.0 Sun M ...