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. 35. docker swarm dockerStack 部署 投票应用

    1. 编写 docker-compose.yml # docker-compose.yml version: "3" services: redis: image: redis:a ...

  2. vue整合外部js

    vue引入外部jsimport { TrackLine } from "../../../../../static/js/trajectory.js";import { initM ...

  3. 杂记 -- 关于less在vue项目中的使用

    1.安装less,less-loader npm install less less-loader --save 2.配置wepack.js(vue3+版本中不用自己设置) //添加less路径模块 ...

  4. VMware下的Ubuntu16设置连接主机网络,设置主机下可以通过xshell访问 VMware下的Ubuntu

    NAT模式连接 1. 2. 3. 4. 5. 6.

  5. LeetCode No.163,164,165

    No.163 FindMissingRanges 缺失的区间 题目 给定一个排序的整数数组 nums ,其中元素的范围在闭区间 [lower, upper] 当中,返回不包含在数组中的缺失区间. 示例 ...

  6. ajax使用json数组------前端往后台发送json数组及后台往前端发送json数组

    1.引子 Json是跨语言数据交流的中间语言,它以键/值对的方式表示数据,这种简单明了的数据类型能被大部分编程语言理解.它也因此是前后端数据交流的主要方式和基础. 2.前端往后台传输json数据 第一 ...

  7. Tensorflow学习教程------lenet多标签分类

    本文在上篇的基础上利用lenet进行多标签分类.五个分类标准,每个标准分两类.实际来说,本文所介绍的多标签分类属于多任务学习中的联合训练,具体代码如下. #coding:utf-8 import te ...

  8. zookeeper注册中心和客户端

    1.zookeeper和eureka区别 zookeeper向client进行ping操作,如果不通,就删除client节点 eureka自我保护机制是client向注册中心发送心跳包,如果一定时间内 ...

  9. Matlab高级教程_第四篇:Matlab高级函数_关键词:arrayfun

    % 定义一个句柄并用这个句柄求值 h = @sin; h(3) % % 命令窗口返回内容 % h(3) % ans = % 0.1411 %定义一个完整句柄,@参数+表达式的形式,并给句柄传参 h1 ...

  10. java常用工具类(三)

    一.连接数据库的综合类 package com.itjh.javaUtil; import java.sql.Connection; import java.sql.DriverManager; im ...