LeetCode0003

  • 给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。
  • 示例 1:
  • 输入: "abcabcbb"
  • 输出: 3
  • 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {
let maxLength = 0;
let tmp = [];
let count = 0;
for (let index = 0, lens = s.length; index < lens; index++) {
let dup = tmp.indexOf(s[index]);
if (dup > -1) {
if (count > maxLength) {
maxLength = count;
}
tmp.splice(0, dup + 1);
tmp.push(s[index]);
count = tmp.length;
}
else {
tmp.push(s[index]);
count++;
}
}
tmp = null;
return Math.max(maxLength, count);
};

LeetCode0009

  • 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。(进阶:不将整数转为字符串来解决这个问题。)
  • 输入: 121
  • 输出: true

思路一

/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function(x) {
//最简单的就将x.toString().reverse() === x.toString()即可
//let str = x.toString();
//return str.split('').reverse().join('') === str;
//不用考虑溢出问题,因为如果是回文数肯定最后不会溢出,溢出就返回false即可
if (x < 0) return false;
if (x === 0) return true;
let y = x, remainder = 0, result = 0;
while (y > 0) {
remainder = y % 10;
result = result * 10 + remainder;
y = Math.floor(y / 10);
}
return result === x;
};

思路二(官方题解:只反转一半数字

  • 上面我们考虑到了反转全部数字,对于int要求严格的语言来说,很容易在计算上超过int.Max,那么你还要去处理溢出问题。
  • 那么能不能只反转一半数字呢?
  • 例如,输入1221,我们可以将数字 “1221” 的后半部分从 “21” 反转为 “12”,并将其与前半部分 “12” 进行比较,因为二者相同,我们得知数字 1221 是回文。
  • 对于数字 1221,如果执行 1221 % 10,我们将得到最后一位数字 1,要得到倒数第二位数字,我们可以先通过除以 10 把最后一位数字从 1221 中移除,1221 / 10 = 122,再求出上一步结果除以 10 的余数,122 % 10 = 2,就可以得到倒数第二位数字。如果我们把最后一位数字乘以 10,再加上倒数第二位数字,1 * 10 + 2 = 12,就得到了我们想要的反转后的数字。如果继续这个过程,我们将得到更多位数的反转数字。
  • 现在的问题是,我们如何知道反转数字的位数已经达到原始数字位数的一半?
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function (x) {
if (x < 0) return false;
if (x === 0) return true;
if (x % 10 === 0) return false;
let result = 0;
//注意这个判断条件,我们上面判断的是大于0,也就是完全反转
//为什么这里x只要大于result就可以停下来了呢
//以x = 1221为例,第一步执行,result=1, x = 122
//第二步,result = 12, x=12
//可以看到这个时候result就已经等于x了,后面再执行也就是把x=0,result=1221而已
//考虑到上面是偶数的情况,我们以x=12321为例,第一步执行,result=1, x = 1232
//第二步,result = 12, x=123
//第三步,result = 123, x=12,此时跳出循环
//只要result/10 = 12等于x即可,中间的奇数位不需要跟其他数字做任何比较
while (x > result) {
result = result * 10 + x % 10;;
x = Math.floor(x / 10);
}
return result === x || Number.parseInt(result / 10) === x;
};

LeetCode Day 3的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. 洛谷 P1341 无序字母对(欧拉回路)

    题目传送门 解题思路: 一道欧拉回路的模板题,详细定理见大佬博客,任意门 AC代码: #include<cstdio> #include<iostream> using nam ...

  2. 使用flask_sqlalchemy操作mysql的一个测试

    示例代码 from flask_sqlalchemy import SQLAlchemy from flask import Flask app=Flask(__name__) app.config[ ...

  3. 【转】我们为什么要使用 Markdown

    目录 从前码字时我们面临着什么困境 标记语言显神威 到底什么是 Markdown 所以为什么我们要使用 Markdown Markdown 简明语法 段落和换行 标题 区块引用 列表 强调 代码标识和 ...

  4. 封装FTP类

    using System; using System.Net; using System.Net.Sockets; using System.Text; using System.IO; using ...

  5. jmlr论文下载

    下载脚本 #!/bin/bash # down_jmlr.sh ver=$1 wget http://www.jmlr.org/papers/$ver/ -O index.htm cat index. ...

  6. debian8修改kde桌面语言

    apt-get install kde-l10n-zhcn, language里面改中文 亲测可用 来源:http://tieba.baidu.com/p/2489771177

  7. PAT甲级——1001 A+B Format (20分)

    Calculate a+b and output the sum in standard format – that is, the digits must be separated into gro ...

  8. IIS设置禁止某个IP或IP段访问网站的方法

    网站被刷,对话接不过来 打开IIS,选中禁IP的站点,找到“ip地址和域限制”这个功能,如果没有安装,打开服务器管理器,点击角色,窗口右边找到添加角色服务,找到“IP和域限制”并勾选安装. 打开ip地 ...

  9. Monkey安装与配置教程

    一.JAVA环境的搭建 安装jdk1.8.0_221,完成环境变量的配置 然后再在系统变量中找到Path,添加%JAVA_HOME%\bin;,确定后,按win+r打开运行,输入cmd 在cmd窗口中 ...

  10. 手机APP例如抖音,让 people‘s 注意力集中到了 社会进化的 优胜部分 (优胜劣汰,什么是优) + 真善美,的 “美” , 促进了2极分化, 会产生强者俞强,弱者越弱,确实促进了信息的流通,传播了有用的东东 产生了独特的价值 而 如何 能计算出这些价值呢, 需要 数学 金融 财务 货币 量化吗

    手机APP例如抖音,让      people‘s  注意力集中到了  社会进化的 优胜部分  (优胜劣汰,什么是优)   +     真善美,的  “美”        , 促进了2极分化, 会产生 ...