Leetcode: 06/01
今天完成了三道题目,总结一下:
1: Length of last word(细节实现题)
此题有一些细节需要注意(比如 “a_ _” 最后一个单词是a, 而不是遇到空格就直接算成没有),别的基本就是模拟了。
class Solution {
public:
int lengthOfLastWord(const char *s) {
string str = s;
if(str.empty()) return ;
int index;
// 从后向前扫描到第一个不是空格的字符
for(index = str.size()-;index>=;index--)
{
if(str[index]!=' ')
break;
}
if(index<) return ;
int i;
//从此字符开始,往前遇到空格停止计算或者把字符串扫描完
for(i=index;i>=;i--)
{
if(str[i] == ' ')
break;
}
return (index-i);
}
};
2. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1]
, return 6
.
这道题目是直方图类题目,贴出的解法是book上的,两轮扫描,第一轮记录左前缀最大值和右前缀最大值; 第二轮扫描,对每个位置,可以积的水是
max(min(max_left,max_right)-height,0) height 是当前位置的格子高度。 这道题目应该算是一道技巧题了。
class Solution {
public:
int trap(int A[], int n) {
if(n<) return ;
int* left_max = new int[n];
int* right_max = new int[n]; // Two pass algorithm
left_max[] = A[];
right_max[n-] = A[n-];
for(int i=;i<n;i++)
{
left_max[i] = max(left_max[i-],A[i]);
right_max[n--i] = max(right_max[n-i],A[n--i]);
}
int water = ;
for(int i=;i<n;i++)
{
water += max(min(left_max[i],right_max[i])-A[i],);
}
return water; }
};
3.Valid Parentheses
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are all valid but "(]"
and "([)]"
are not.
简单的括号匹配题,一般都是用栈来实现。实现细节要注意,错误都犯在控制变量的初始化上了(test 变量每次set成true之后,没有重新初始化)
class Solution {
public:
bool isValid(string s) {
stack<int> st;
bool test=false;int num;
for(int i=;i<s.size();i++)
{
switch (s[i])
{
case '(': num = ;st.push(num);break;
case '[': num = ;st.push(num);break;
case '{': num = ;st.push(num);break;
case ')': num = ;test = true;break;
case ']': num = ;test =true;break;
case '}': num = ;test = true; break;
}
if(test)
{
if(st.empty() || st.top()!=num) return false;
else st.pop();
test =false;
}
}
if(st.empty()) return true;
else return false;
}
};
Leetcode: 06/01的更多相关文章
- Yii2 AR find用法 (2016-05-18 12:06:01)
Yii2 AR find用法 (2016-05-18 12:06:01) 转载▼ User::find()->all(); 返回所有数据 User::findOne($id); ...
- BlackArch Linux 2019.06.01 宣布发布
导读 BlackArch Linux是一个基于Arch Linux的发行版,专为渗透测试人员和安全研究人员设计,并包含大量渗透测试和安全实用程序,已宣布发布2019.06.01版本. BlackArc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- [Leetcode Week10]01 Matrix
01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...
- Leetcode 542.01矩阵
01矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 ...
- LeetCode 06 ZigZag Conversion
https://leetcode.com/problems/zigzag-conversion/ 水题纯考细心 题目:依照Z字形来把一个字符串写成矩阵,然后逐行输出矩阵. O(n)能够处理掉 记i为行 ...
- [leetcode] 542. 01 Matrix (Medium)
给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...
- leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings
542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...
- Java实现 LeetCode 542 01 矩阵(暴力大法,正反便利)
542. 01 矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 ...
随机推荐
- 详解JMeter函数和变量
JMeter函数可以被认为是某种特殊的变量,它们可以被采样器或者其他测试元件所引用.函数调用的语法如下: ${__functionName(var1,var2,var3)} 其中,__function ...
- C--运算符,表达式和语句实例
//第五章 运算符,表达式和语句 #include<stdio.h> //引入头文件 #include<math.h> #define ADJUST 7.64 //定义常量 # ...
- jquery_mobile.js+html5+css3打造手机平板等各种效果
http://www.w3school.com.cn/jquerymobile/jquerymobile_events_orientation.asp
- Java用ZIP格式压缩和解压缩文件
转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...
- 在asp.net webservice中如何使用session
原文:在asp.net webservice中如何使用session 原文:刘武|在asp.net webservice中如何使用session 在使用asp.net编写webservice时,默认情 ...
- Installshield关于.NET安装时需要重启动的处理办法,以及延伸出的重启后继续安装的安装包的一点想法
原文:Installshield关于.NET安装时需要重启动的处理办法,以及延伸出的重启后继续安装的安装包的一点想法 很多朋友做安装包的时候,所打包的软件需要.NET Framework之类的环境,他 ...
- userAgent,JS这么屌的用户代理,你造吗?——判断浏览器内核、浏览器、浏览器平台、windows操作系统版本、移动设备、游戏系统
1.识别浏览器呈现引擎 为了不在全局作用域中添加多余变量,这里使用单例模式(什么是单例模式?)来封装检测脚本.检测脚本的基本代码如下所示: var client = function() { var ...
- 用一条SQL语句取出第 m 条到第 n 条记录的方法
原文:用一条SQL语句取出第 m 条到第 n 条记录的方法 --从Table 表中取出第 m 条到第 n 条的记录:(Not In 版本) * FROM Table id ...
- JS中通过call方法实现继承
原文:JS中通过call方法实现继承 讲解都写在注释里面了,有不对的地方请拍砖,谢谢! <html xmlns="http://www.w3.org/1999/xhtml"& ...
- [译]Java 设计模式 之模板方法
(文章翻译自Java Design Pattern: Template Method) 模板方法设计模式定义了归档特定操作的工作流.它允许子类去修改特定的步奏而不用改变工作流的结构. 下面的例子表示模 ...