Leetcode 题目整理-5 Valid Parentheses & Merge Two Sorted Lists
20. 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.
注:给出一些有括号组成的字符串,判断这些字符串是否合法。不知道这里是否考虑空格的因素还是所有的字符都是括号,还有就是应该考虑嵌套的格式,如(){[]}个人理解应该是合法的。
代码如下:
bool Solution::isValid(string s)
{
bool flag{ };
if (s.size() == )
{
return flag = ;
}
map<char, char> side;
side['('] = 'l';side['{'] = 'l';side['['] = 'l';side[')'] = 'r';side['}'] = 'r';side[']'] = 'r';
map<char, char> group;
group[')'] = '('; group['}'] = '{'; group[']'] = '[';//通过右括号索引左括号
for (string::iterator s_i = s.begin(); s_i != s.end(); )
{
if (side[*s_i] == 'r')//找第一次出现右括号的点
{
if (s_i == s.begin())
{
return flag = ;//如果开始就出现了右括号,那是非法的,返回即可
}
if (group[*s_i] == *(s_i - ))
{
s.erase(s_i - );
s.erase(s_i - );//相等说明是对的,那就把正确的这一对儿删除掉,指针要向前移动一位
if (s.size() == )
{
return flag=;//如果删完没有了,就返回认为是正确的
}
else{
s_i--;
}
}
else
{
return flag = ;//如果不相等说明排列有问题,返回
}
}
else
{
s_i++;//如果出现的不是右括号就移动到下一个
}
} return flag;//如果所有的都是左括号,那么最后将在这里返回 0
}
21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
注:合并两个有序链表,并且不能丢弃其中任何一个node。
解:代码过程中出现一个问题就是对空链表如何处理,第二个问题就是竟然要排序,又重新添加了一遍。
ListNode* Solution::mergeTwoLists(ListNode* l1, ListNode* l2)
{
ListNode *head = new ListNode();//这是要返回的那个值
ListNode *temp;
temp = head;
while (l1 != NULL || l2 != NULL)
{
//只要有一个不为零就执行下列程序
if (l1 == NULL)
{
temp->next = l2;
l2 = l2->next;
}
else
{
if (l2 == NULL)
{
temp->next = l1;
l1 = l1->next;
}
else
{//如果都不是空的
if (l1->val > l2->val)
{//取小的放在前边
temp->next = l2;
l2 = l2->next;
}
else{
temp->next = l1;
l1 = l1->next;
}
}
}
temp = temp->next;//每次都要有两个指针同时前进
}
//把刚才建立的头删掉 return head->next;
}
Leetcode 题目整理-5 Valid Parentheses & Merge Two Sorted Lists的更多相关文章
- 【LeetCode】【数组归并】Merge k Sorted Lists
描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- Leetcode练习题21. Merge Two Sorted Lists
题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...
- 乘风破浪:LeetCode真题_032_Longest Valid Parentheses
乘风破浪:LeetCode真题_032_Longest Valid Parentheses 一.前言 这也是非常有意思的一个题目,我们之前已经遇到过两个这种括号的题目了,基本上都要用到堆栈来解决,这次 ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [LeetCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode 23 Merge k Sorted Lists(合并k个有序链表)
题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...
- Merge Two Sorted Lists - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Merge Two Sorted Lists - LeetCode 注意点 两个链表长度可能不一致 解法 解法一:先比较两个链表长度一致的部分,多余的部分 ...
- [Leetcode Week4]Merge Two Sorted Lists
Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...
随机推荐
- PSR-1之PHP代码文件必须以不带BOM的UTF-8编码
BOM——Byte Order Mark,就是字节序标记 在UCS 编码中有一个叫做”ZERO WIDTH NO-BREAK SPACE“的字符,它的编码是FEFF.而FFFE在UCS中是不存在的字符 ...
- python3.6中使用selenium + chromedriver访问Chrome浏览器时报错
from selenium import webdriver from selenium.webdriver.chrome.options import Options if __name__ == ...
- 使用SonarQube+Eclipse来分析python代码
背景 最近在项目中推广集成测试的理念以及相关工具,在jenkins中集成sonar去分析项目的java代码的时候,意外的发现,sonarqube上还有对应python的插件,而自己写的测试工具大部分都 ...
- 电脑端TIM登录时记住密码
为什么每次登录TIM时点了记住密码,下次再登录时还是记不住呢? 不是扫码就是还得输出密码,为这事愁了好多次, 最近终于发现如何记住密码了... 进入登录界面以后,点击左下角这个小图标>> ...
- Windows To Go 企业版2019 LTSC 开发环境部署
Windows To Go 是一项非常实用的功能,与传统方式安装Windows 10相比更具有灵活性,会根据每次接入的硬件型号保留不同版本驱动. 由于博主是一名全栈程序员(截至发稿处于菜鸟级别),对灵 ...
- 「Luogu P3395」路障 解题报告
点开有惊喜 其实是题面 这D1T1给的很有面子! 我居然做的来! 从左上角走到右上角 然后n<=1000 所以果断放弃DFS,选择BFS 思路还是一样的BFS 证明: 走到一个点的时间越早越好( ...
- 使用SqlDependency实时监听SQL server数据库变化并执行事件
sql server设置:ALTER DATABASE <DatabaseName> SET ENABLE_BROKER;语句让相应的数据库启用监听服务,以便支持SqlDependency ...
- Spring Boot 集成 Seata 解决分布式事务问题
seata 简介 Seata 是 阿里巴巴2019年开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务.在 Seata 开源之前,Seata 对应的内部版本在阿里内部一 ...
- 我怎么感觉 ConcurrentDictionary<,> 不是线程安全的喃?
直接上代码 class Program { static readonly ConcurrentDictionary<string, Person> Dic = new Concurren ...
- 单调队列优化 dp
The only difference between easy and hard versions is the constraints. Vova likes pictures with kitt ...