LeetCode Day 4
- 给定 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;
};
- 请你来实现一个 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的更多相关文章
- 我为什么要写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 ...
随机推荐
- leetcode 690.员工的重要性
题目: 给定一个保存员工信息的数据结构,它包含了员工唯一的id,重要度 和 直系下属的id. 比如,员工1是员工2的领导,员工2是员工3的领导.他们相应的重要度为15, 10, 5.那么员工1的数据结 ...
- 1.2 NumPy数组基础
目录 第一章 numpy入门 1.2 numpy数组基础 1.2.1 数组的属性 1.2.2 数组的索引:获取单个元素 1.2.3 数组切片:获取子数组 1.2.4 数组的变形 1.2.5 数组的拼接 ...
- SDWebImage缓存图片和读取图片
NSString *urlStr: NSUrl *url = [NSURL URLWithString:urlStr]; //缓存图片 SDWebImageManager *manager = [SD ...
- 备战秋招——C++知识点
1.字符串的末尾'\0'也算一个字符,一个字节. 2.使用库函数strcpy(a,b)进行拷贝b->a操作,strcpy会从源地址一直往后拷贝,直到遇到'\0'为止.所以拷贝的长度是不定的.如果 ...
- 吴裕雄--天生自然Linux操作系统:Linux 文件与目录管理
Linux的目录结构为树状结构,最顶级的目录为根目录 /. 其他目录通过挂载可以将它们添加到树中,通过解除挂载可以移除它们. 绝对路径: 路径的写法,由根目录 / 写起,例如: /usr/share/ ...
- 吴裕雄--天生自然 PYTHON3开发学习:列表
list1 = ['Google', 'Runoob', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b& ...
- python数据预处理for knn
机器学习实战 一书中第20页数据预处理,从文本中解析数据的程序. import numpy as np def dataPreProcessing(fileName): with open(fileN ...
- python编程:从入门到实践----第五章>if 语句
一.一个简单示例 假设有一个汽车列表,并想将其每辆汽车的名称打印出来.遇到汽车名‘bmw’,以全大写打印:其他汽车名,首字母大写 cars=['audi','bmw','subaru','toyota ...
- 远程SSH服务使用指南
Author Email Yaoyao Liu yaoyaoliu@msn.com 本文所有教程以ubuntu为例,对其他unix内核系统如Debian.CentOS.macOS等也适用. 目录 安装 ...
- java添加后台缓存
public class Cache { private String key;//缓存ID private Object value;//缓存数据 private long timeOut;//更新 ...