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 ...
随机推荐
- c语言:函数的递归调用
c语言可以将代码模块化,这是其很重要的一个特性. 说道代码模块化,我们很自然的就会联想到函数.而函数中,比较难的一个知识点就是函数的递归调用. 值得注意的是,函数的递归调用在现实工作并不是很常用,但是 ...
- 将QT窗口嵌入到WinForm窗口
要想 windows下抓取Qt进程主界面,并嵌入到自己的程序中显示,需要首先设置qt窗口的windowTitle属性,然后就可以通过 windows api 中的 FindWindow 函数查找到窗口 ...
- jquery 第二节 Dom和jQuery的互相转换
1.Dom转jQuery Dom对象: var v_dom = document.getElementById("name"); 转换: jQuery对象: var v_jq ...
- Nginx模块-ngx_http_mirror_module-流量复制【转】
Nginx流量复制# 需求# 将生产环境的流量拷贝到预上线环境或测试环境,这样做有很多好处,比如: 可以验证功能是否正常,以及服务的性能: 用真实有效的流量请求去验证,又不用造数据,不影响线上正常访问 ...
- [原]C++新标准之std::ratio
原 总结 ratio 概览 类定义 预定义ratio 应用 示例代码 参考资料 概览 std::ratio定义在<ratio>文件中,提供了编译期的比例计算功能.为std::chrono ...
- aop 实现原理
aop 底层采用代理机制实现 接口 + 实现类 :spring 采用 jdk 的 动态代理 只有实现类:spring 采用 cglib 字节码增强 aop专业术语 1.target(目标) 需要被代理 ...
- python学习笔记(31)——日志格式
- git的基础使用
GIT """ 什么是git:版本控制器 - 控制的对象是开发的项目代码 代码开发时间轴:需求1 > 版本库1 > 需求2 > 版本库2 > 版本 ...
- dubbo的重试原则
验证思路.使用超时来验证重试次数 XML 注解
- tensorflow中使用指定的GPU及GPU显存
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 本文目录 1 终端执行程序时设置使用的GPU 2 python代码中设置使用的GPU 3 设置tensorflow使用的显 ...