今天完成了三道题目,总结一下:

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的更多相关文章

  1. Yii2 AR find用法 (2016-05-18 12:06:01)

    Yii2 AR find用法 (2016-05-18 12:06:01) 转载▼     User::find()->all();    返回所有数据   User::findOne($id); ...

  2. BlackArch Linux 2019.06.01 宣布发布

    导读 BlackArch Linux是一个基于Arch Linux的发行版,专为渗透测试人员和安全研究人员设计,并包含大量渗透测试和安全实用程序,已宣布发布2019.06.01版本. BlackArc ...

  3. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  4. [Leetcode Week10]01 Matrix

    01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...

  5. Leetcode 542.01矩阵

    01矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 ...

  6. LeetCode 06 ZigZag Conversion

    https://leetcode.com/problems/zigzag-conversion/ 水题纯考细心 题目:依照Z字形来把一个字符串写成矩阵,然后逐行输出矩阵. O(n)能够处理掉 记i为行 ...

  7. [leetcode] 542. 01 Matrix (Medium)

    给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...

  8. 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的值. 最 ...

  9. Java实现 LeetCode 542 01 矩阵(暴力大法,正反便利)

    542. 01 矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 ...

随机推荐

  1. Extjs4.1MVC详细解释

    :http://xiebaochun.github.io/ app.js [javascript] view plaincopyprint? Ext.onReady(function(){ Ext.Q ...

  2. 取一种类型里面的产品销售前3甲的数据Sql

    需求:取出每种分类里面的销售前3甲的产品信息 表设计如下图: 数据如下: 两种方法可以实现: 1. SELECT * FROM (SELECT ROW_NUMBER() OVER(PARTITION ...

  3. nodejs开发aspnet5项目

    结合nodejs开发aspnet5项目 1.安装kvm   官方教程地址:https://github.com/ligershark/Kulture 打开 powershell命令窗口,找不到可以在开 ...

  4. ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪婪)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 FatMouse prepared M pounds of cat ...

  5. UUShutdown关机工具 - 给 Windows8.1Metro 开始屏幕添加 关机重启按钮

    UUShutdown,给开始屏幕(开始菜单)添加重启关机等按钮 如图: 安装完成之后找到开始菜单程序文件夹中的快捷方式,附加到开始屏幕即可. 2.0加入主程序界面,支持换肤和定时: 看见的需要那就拿去 ...

  6. lua及luci学习

    由于项目需要对Luci进行修改,所以这里开始地luci进行较深入的研究. 探索其中的运行路径. Openwrt默认的HTTP服务器为uhttpd,该WEB服务器是由Luci的开发者自行开发的,非常小巧 ...

  7. openwrt驱动与应用程序的联系

    应用程序与驱动之间需要进行命令的传递,因而它们之间需要共同定义一套双方都可以识别的数据结构,实际使用时它们include的是名字和内容相同但位置不同的头文件. 比如spi_gpio_ad7193.h这 ...

  8. GridView中的编辑和删除按钮,执行更新和删除代码之前的更新提示或删除提示

    在GridView中,可以通过设计界面GridViewr任务->编辑列->CommandField,很简单的添加的编辑和删除按钮 在前台源码中,可以看到GridView自动生成了两个列. ...

  9. SD卡FAT32获得高速的文件格式(图文介绍)

    说明: MBR :Master Boot Record ( 主引导记录) DBR :DOS Boot Record ( 引导扇区) FAT :File Allocation Table ( 文件分配表 ...

  10. WITH (NOLOCK)浅析

    SQL Server 中WITH (NOLOCK)浅析 2014-08-30 11:58 by 潇湘隐者, 503 阅读, 2 评论, 收藏, 编辑 概念介绍 开发人员喜欢在SQL脚本中使用WITH( ...