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 ...
随机推荐
- 国外android开源站点
http://android-arsenal.com/
- Linq入门演练---(1)基本用法-分组,排序,内连接
这一节大家共同学习下LINQ的基本用法,主要包括LINQ的分组,排序,和内外连接. 1.分组 基本语法: group element by key element 表示查询结果返回的元素,key表示 ...
- 【C++基础】类的组合
所谓类的组合是指:类中的成员数据是还有一个类的对象或者是还有一个类的指针或引用.通过类的组合能够在已有的抽象的基础上实现更复杂的抽象. 比如: 1.按值组合 #include<iostream. ...
- perconaXTRADB Cluster在Redhat Linux上的安装
installing-perconaXTRADB Cluster 5.6 For Redhat 6.4 一.server版本号查看 Root# cat /etc/redhat-release Red ...
- 【软测试】(两)计算机组成原理-cpu
cpu,中文名称中央处理单元,central processing unit.系统的核心,用于数据的处理,算术以及逻辑运算和控制程序的运行. 组成 运算器 从字面上就能够理解到.运算器主要用来对于逻辑 ...
- 微软不也是从Altair Basic这丑小鸭长成白天鹅吗?
微软不也是从Altair Basic这丑小鸭长成白天鹅吗? February 2015 如果你想要弄清楚初创企业是怎么一回事的话,其中一个非常有价值的尝试是去研究下那些获得巨大成功的公司,去分析下为什 ...
- 发现新大陆:一个最简单的破解SSL加密网络数据包的方法
1. 简介 相信能访问到这篇文章的同行基本上都会用过流行的网络抓包工具WireShark,用它来抓取相应的网络数据包来进行问题分析或者其他你懂的之类的事情. 一般来说,我们用WireShark来抓取包 ...
- SQL Server 2008 R2中,变表的右键弹出菜单中的“选择前1000行”为“选择所有行”
原文:SQL Server 2008 R2中,变表的右键弹出菜单中的"选择前1000行"为"选择所有行" 从SQL Server 2008开始,微软为了提高查询 ...
- IOS被遗忘的知识
IOS ARC项目使用非ARC文件 1.自己的旧项目没有使用ARC,可是引入的第三方库却是使用了ARC的. 对于第一个情况,给採用了ARC的源文件,加入-fobjc-arc选项 2.自己的新项目使用了 ...
- sql点滴38—SQL Server 2008和SQL Server 2008 R2导出数据的选项略有不同
原文:sql点滴38—SQL Server 2008和SQL Server 2008 R2导出数据的选项略有不同 说明: 以前要将一个表中的数据导出为脚本,只有用存储过程.现在在SQL Server ...