LeetCode Day 3
- 给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。
- 示例 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);
};
- 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。(进阶:不将整数转为字符串来解决这个问题。)
- 输入: 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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- Python说文解字_看起来有点儿像字典的元组(命名元祖)
1. 需要一个库 namedtuple: 所谓命名元组就是对于元组的每一个元素进行起名,看起来很像访问字典一样. 实例 from collections import namedtuple Stock ...
- 在项目中ES6语法转ES5语法 babel转码器
es6 babel 安装以及使用 1,安装好node(需要使用npm包管理工具) 2,在本地项目路径下npm init,格式化成功后会在项目下生成一个配置文件package.json 3,本地安装 ...
- python学习笔记-函数与可变长参数
一.函数 1. def test(x): y=x+1 return yprint(test) #这是打印函数在内存中的地址 过程:就是没有返回值的函数 在python中过程也是函数,就算没哟返回值,也 ...
- blocking(非阻塞)回调函数
回调函数不会造成阻塞 function loop() { setTimeout(loop, 0) } loop 死循环 while(true)
- [Scoi2016]背单词(trie+贪心)
题意:重新解释一下题意吧(题意晦涩难懂) 给定n个单词,你可以按照顺序学习,当学习这一单词时,这个单词是第x个要学习的单词,需要的代价分三类: 1.若存在其他单词是其后缀没被学习,则代价为n2 2.若 ...
- opencv+tkinter制作HsvMaster(一)
这两天看opencv-python的HSV色彩空间,在写程序时发现用HSV来提取图像区域是件令人恶心的麻烦事.拿阈值分割做个对比,阈值最多也就一两个参数需要调整:但是HSV需要对三个通道调整上下限,也 ...
- Python——课程数据统计分析
介绍 在该章节中我们将利用提供的课程数据来进行一次实战性质的时间序列和聚类分析. 知识点 数据处理 数据可视化 中文分词 文本聚类 数据概览 本次课程的数据来源于运行过程中产生的真实数据,我们对部分数 ...
- linux epoll ET边沿触发
/***EPOLL ET 触发必须使用非阻塞,LT触发可以阻塞/非阻塞.*read 函数 非阻塞读需 忙轮寻 soket关闭返回0,循环读完数据*如果已经读完再读read返回 -1,errno=11( ...
- VS IDE调试下将打印日志输出到输出窗口
int aBeginTime = GetTickCount(); TRACE("Current time begin:%d \n", aBeginTime); 查看输出窗口:
- 9.数据分组 group by
--数据分组 group by --作用:用于 对查询的数据进行分组,并处理 select deptno,job from emp group by deptno,job --1.分组之后,不能将除分 ...