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 ...
随机推荐
- 驾驭git merge——git merge的规范化操作
这两天负责将一个开发了较长时间,代码量数万行的C语言项目(A项目)的代码分支合并到主线.由于之前参与过一些其他项目分支收编时采用git merge引入问题的修改,个人从心理上对git merge有所抵 ...
- EasyMock.replay()有什么用
现在很多项目都使用EasyMock来作为单元测试框架. EasyMock一个方法,基本上是三步:EasyMock.expect().EasyMock.replay().EasyMock.verify( ...
- 洛谷$P3620\ [APIO/CTSC 2007]$数据备份 贪心
正解:贪心 解题报告: 传送门$QwQ$ $umm$感觉这种问题还蛮经典的,,,就选了某个就不能选另一个这样儿,就可以用堆模拟反悔操作 举个$eg$,如果提出了$a_i$,那就$a_{i-1}$和$a ...
- Linux Centos7 环境搭建Docker部署Zookeeper分布式集群服务实战
Zookeeper完全分布式集群服务 准备好3台服务器: [x]A-> centos-helios:192.168.19.1 [x]B-> centos-hestia:192.168.19 ...
- Java虚拟机详解(十一)------双亲委派模型
在上一篇博客,我们介绍了类加载过程,包括5个阶段,分别是“加载”,“验证”,“准备”,“解析”,“初始化”,如下图所示: 本篇博客,我们来介绍Java虚拟机的双亲委派模型,在介绍之前,我先抛出一个问题 ...
- RabbitMQ安装(Windows)
一.下载安装 由于RabbitMQ是用Erlang语言编写的,因此需要先安装Erlang. 通过http://www.erlang.org/downloads获取对应安装文件进行安装 增加环境变量ER ...
- Win10该文件没有与之关联的应用来执行该操作...请在"默认应用设置"页面中创建关联
问题发现:一直使用的一款软件--火柴,这两天忽然发现通过ctrl + 回车快捷键无法进入到文件所在的目录中(之前几天印象中还可以使用该功能).后来测试又发现无法打开网易云音乐中下载的音乐而进入到该音乐 ...
- nginx错误: [error] OpenEvent("Global\ngx_reload_10444") failed (2: The system cannot find the file specified)
执行nginx -s reload命令: nginx: [error] OpenEvent("Global\ngx_reload_10444") failed (2: The sy ...
- C++ | C++ 基础知识 | 指针、数组与引用
1.指针 在 C++ 语言中存放及使用内存地址是通过指针和引用完成的. char c = 'a'; // 声明 c 变量,c 变量存储的是 'a' 的值. char* p = &c; // 声 ...
- html 小游戏合集(1.0)
最近做了个小游戏合集,有点沙雕,毕竟是1.0,将就看看. <!DOCTYPE html> <html> <head> <meta charset=" ...