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[]. ...
随机推荐
- web报表工具FineReport的公式编辑框的语法简介
FINEREPORT用到公式的地方非常多,单元格(以=开头的便被解析为公式),条件显示,数据字典,报表填报属性值定义,图表标题,轴定义,页眉页脚,甚至单元格的其他属性中的鼠标悬浮提示内容都可以写公式, ...
- C# PDF Page操作——设置页面切换按钮
概述 在以下示例中,将介绍在PDF文档页面设置页面切换按钮的方法.示例中将页面切换按钮的添加分为了两种情况,一种是设置按钮跳转到首页.下页.上页或者最后一页,另一种是设置按钮跳转到指定页面.两种方法适 ...
- 实现Android Native端爆破源码
尝试在移动端so侧做一些内存修改,使之走向不通的逻辑,一下为将要爆破的APP源码 JAVA侧: package com.example.grady.sectestone; import android ...
- 在java中读取配置文件信息
public class PropertyUtil { public static final Properties PROP = new Properties(); /** * 读取配置文件的内容( ...
- JeeSite中Excel导入导出
在各种管理系统中,数据的导入导出是经常用到的功能,通常导入导出以Excel.CSV格式居多.如果是学习的过程中,最好是自己实现数据导入与导出的功能,然而在项目中,还是调用现成的功能比较好.近期一直使用 ...
- async/await 的基本实现和 .NET Core 2.1 中相关性能提升
前言 这篇文章的开头,笔者想多说两句,不过也是为了以后再也不多嘴这样的话. 在日常工作中,笔者接触得最多的开发工作仍然是在 .NET Core 平台上,当然因为团队领导的开放性和团队风格的多样性(这和 ...
- ffmpeg 的 tbr tbc 和 tbn的意义
tbn = the time base in AVStream that has come from the container tbc = the time base in AVCodecConte ...
- svn path already exists的解决办法
这种问题的一般原因是这个path所指的目录在服务器端是一个空目录,对客户端不可见,客户端如果新建了这个目录,而且向服务器端commit的时候就会报错,服务器端此目录已存在,这个时候就会存在一个问题:就 ...
- C#更改操作系统时间
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServi ...
- 原生Feign使用详解
一,简介 Feign使得 Java HTTP 客户端编写更方便.Feign 灵感来源于Retrofit.JAXRS-2.0和WebSocket.Feign最初是为了降低统一绑定Denominator到 ...