LeetCode0011

  • 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。n 的值至少为 2。

  • 图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
  • 示例:
  • 输入: [1,8,6,2,5,4,8,3,7]
  • 输出: 49

思路:

  • 我们最喜闻乐见的暴力法,对每个元素都计算一下他跟其他元素能形成的最大面积即可,时间复杂度O(n2),当然,这么玩就没意思了。
  • 我们假定有两块挡板,最早的时候左挡板在最左边,右挡板在最右边,这样子距离是最大的,最大面积就由那块短板决定。
  • 接下来我们不管移动左边的挡板或者右边的挡板,两块板之间的距离都会变小,那么唯有下一块挡板高度更高时,才有可能装更多的水。因此哪块挡板更小,我们就先移动哪块,以此为标准来移动左挡板或者右挡板,以便试探容器的最大值。
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function (height) {
let left = 0, right = height.length - 1;
let maxArea = 0;
while (left < right) {
maxArea = Math.max(maxArea, Math.min(height[left], height[right]) * (right - left));
if (height[left] < height[right]) {
left++;
}
else {
right--;
}
}
return maxArea;
};

LeetCode0008

  • 请你来实现一个 atoi 函数,使其能将字符串转换成整数。
  • 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。
  • 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。
  • 该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。
  • 注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。
  • 在任何情况下,若函数不能进行有效的转换时,请返回 0。
  • 假定我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231, 231 − 1]。如果数值超过这个范围,请返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。
/**
* @param {string} str
* @return {number}
*/
var myAtoi = function (str) {
let min = Math.pow(-2, 31);
let max = Math.pow(2, 31) - 1
let maxPre = Number.parseInt(max / 10);
let negative = false;//结果为正数还是负数
let regx = /[0-9]/;
let result = 0;
let startWithNumOrSign = false;//是否已经遇到过数字或者符号,如果true再遇到符号或者字母就直接输出result
for (let i = 0, lens = str.length; i < lens; i++) {
if (str[i] === '-') {
if (startWithNumOrSign) return result * (negative ? -1 : 1);
negative = true;
startWithNumOrSign = true;
} else if (str[i] === '+') {
if (startWithNumOrSign) return result * (negative ? -1 : 1);
negative === false;
startWithNumOrSign = true;
}
else if (regx.test(str[i])) {
let num = Number.parseInt(str[i]);
startWithNumOrSign = true;
if (result > maxPre) {
return negative ? min : max;
}
else if (result === maxPre) {
if (negative) {
if (num > 8) return min;
} else {
if (num > 7) return max;
}
}
result = result * 10 + num;
} else if (str[i] === ' ') {
if (startWithNumOrSign) return result * (negative ? -1 : 1);
} else {
if (!startWithNumOrSign) {
return 0;
} else {
return result * (negative ? -1 : 1);
}
}
}
return result * (negative ? -1 : 1);
};

LeetCode Day 4的更多相关文章

  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. CSS3 media媒体查询器的使用方法

    最近几年随着响应式布局的发展,一次开发多次使用,自适应屏幕的响应式网站的需求越来越多.但是怎样使得网站能自适应屏幕呢?这里就需要提到一个css3里面新增的技术了-media媒体查询器. 那么什么是me ...

  2. PHP实现快速排序算法相关案例

    <?php /** * 快速排序 --主要运用递归, 先把一个数找准位置,然后再递归把左右两边的数都找准位置 */ function QSort($a= []){ $nCount = count ...

  3. cin cout

    编写一个程序,要求用户输入一串整数和任意数目的空格,这些整数必须位于同一行中,但允许出现在该行的任何位置.当用户按下enter键,数据输入停止.程序自动对所有的整数进行求和并打印出结果. 需要解决两个 ...

  4. pytorch(ch5

    读取图片数据集::# -*- coding: utf-8 -*-import torch as tfrom torch.utils import dataimport osfrom PIL impor ...

  5. [LC] 82. Remove Adjacent Repeated Characters IV

    Repeatedly remove all adjacent, repeated characters in a given string from left to right. No adjacen ...

  6. 学习LCA( 最近公共祖先·二)

    http://poj.org/problem?id=1986 离线找u,v之间的最小距离(理解推荐) #include<iostream> #include<cstring> ...

  7. debian8修改kde桌面语言

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

  8. Java字符串替换函数replace、replaceFirst、replaceAll

    一.replace(String old,String new) 功能:将字符串中的所有old子字符串替换成new字符串 示例 String s="Hollow world!"; ...

  9. [Sdoi2013]森林(启发式合并+主席树)

    对于操作1,显然可以使用主席树维护,然后对于一条链(x,y),假设lca为f,根为rt,则(rt,x)+(rt,y)-(rt,f)-(rt,fa[f])即为所求的链,在主席树上直接查询即可,查询方式类 ...

  10. 项目中关于RPC 和rocketMQ使用场景的感受

    在花生待的这半年,切身体会了系统之间交互场景的接口技术实现方式,个人总结.仅供参考: 1.关于rpc接口,一般情况下 都是同步的.A系统的流程调用B系统.等着B返回,根据返回结果继续进行A接下来的流程 ...