LeetCode0003

  • 给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。
  • 示例 1:
  • 输入: "abcabcbb"
  • 输出: 3
  • 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
  1. /**
  2. * @param {string} s
  3. * @return {number}
  4. */
  5. var lengthOfLongestSubstring = function (s) {
  6. let maxLength = 0;
  7. let tmp = [];
  8. let count = 0;
  9. for (let index = 0, lens = s.length; index < lens; index++) {
  10. let dup = tmp.indexOf(s[index]);
  11. if (dup > -1) {
  12. if (count > maxLength) {
  13. maxLength = count;
  14. }
  15. tmp.splice(0, dup + 1);
  16. tmp.push(s[index]);
  17. count = tmp.length;
  18. }
  19. else {
  20. tmp.push(s[index]);
  21. count++;
  22. }
  23. }
  24. tmp = null;
  25. return Math.max(maxLength, count);
  26. };

LeetCode0009

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

思路一

  1. /**
  2. * @param {number} x
  3. * @return {boolean}
  4. */
  5. var isPalindrome = function(x) {
  6. //最简单的就将x.toString().reverse() === x.toString()即可
  7. //let str = x.toString();
  8. //return str.split('').reverse().join('') === str;
  9. //不用考虑溢出问题,因为如果是回文数肯定最后不会溢出,溢出就返回false即可
  10. if (x < 0) return false;
  11. if (x === 0) return true;
  12. let y = x, remainder = 0, result = 0;
  13. while (y > 0) {
  14. remainder = y % 10;
  15. result = result * 10 + remainder;
  16. y = Math.floor(y / 10);
  17. }
  18. return result === x;
  19. };

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

  • 上面我们考虑到了反转全部数字,对于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,就得到了我们想要的反转后的数字。如果继续这个过程,我们将得到更多位数的反转数字。
  • 现在的问题是,我们如何知道反转数字的位数已经达到原始数字位数的一半?
  1. /**
  2. * @param {number} x
  3. * @return {boolean}
  4. */
  5. var isPalindrome = function (x) {
  6. if (x < 0) return false;
  7. if (x === 0) return true;
  8. if (x % 10 === 0) return false;
  9. let result = 0;
  10. //注意这个判断条件,我们上面判断的是大于0,也就是完全反转
  11. //为什么这里x只要大于result就可以停下来了呢
  12. //以x = 1221为例,第一步执行,result=1, x = 122
  13. //第二步,result = 12, x=12
  14. //可以看到这个时候result就已经等于x了,后面再执行也就是把x=0,result=1221而已
  15. //考虑到上面是偶数的情况,我们以x=12321为例,第一步执行,result=1, x = 1232
  16. //第二步,result = 12, x=123
  17. //第三步,result = 123, x=12,此时跳出循环
  18. //只要result/10 = 12等于x即可,中间的奇数位不需要跟其他数字做任何比较
  19. while (x > result) {
  20. result = result * 10 + x % 10;;
  21. x = Math.floor(x / 10);
  22. }
  23. return result === x || Number.parseInt(result / 10) === x;
  24. };

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. C#压缩解压zip 文件

    /// <summary> /// Zip 压缩文件 /// </summary> public class Zip { public Zip() { } #region 加压 ...

  2. 《Docekr入门学习篇》——Docker实战

    基础环境 root@docker~]# cat /etc/redhat-release #查看版本号 CentOS Linux release (Core) [root@docker ~]# unam ...

  3. Half of UK 10-year-olds own a smartphone

    1. preposition n. 介词  pronoun  n. 代词 2. despite /preposition. (1) used to say that something happens ...

  4. 零相关|回归|相关|相关系数|回归解释相关|r判断相关性|相关系数的区间估计|数据类型|非线性回归

    零相关是什么? 零相关亦称“不相关”.相关的一种.两个变量的相关系数r=0时的相关.零相关表示两个变量非线性相关,这时两个变量可能相互独立,也可能曲线相关.对于正态变量,两个变量零相关与两个变量相互独 ...

  5. ios 监控键盘状态

    增加键盘显示和隐藏事件监听 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(noti ...

  6. iPhoneX的后遗症要持续多久?

    iPhone X的推出算得上苹果历史上的大事件,这款梳着刘海头型的手机作为iPhone十周年纪念款手机,承载着苹果和整个产业链巨大的希望,正因如此,包括苹果在内的大量企业,把宝都压到了这款手机上.后来 ...

  7. 主流消息队列rocketMq,rabbitMq比对使用

    首先整理这个文章是因为我正好有机会实战了一下rocketmq,阿里巴巴的一个开源消息中间件.所以就与以往中rabbitmq进行小小的比较一下.这里主线的根据常见面试问题进行整理. 一.消息队列常用的场 ...

  8. 13.docker 网络 docker NameSpace (networkNamespace)

    一. 案例 1.创建一个 container docker run -d --name test1 busybox /bin/sh -c "while true; do sleep 3600 ...

  9. zabbix3.4--配置微信告警

    1.注册企业微信 https://work.weixin.qq.com/ 2.注册好后登陆,点击“我的企业”,记录企业ID. 3.点击“应用管理”--“创建应用”,创建应用时添加接收告警的用户 4.添 ...

  10. python画图例子代码

    matplotlib包,使得python可以使用类似matlab的命令 双坐标,子图例子 fig, axes = plt.subplots( 2,1, figsize=(14, 14) ) ax = ...