leetcode-12-stack
409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
This is case sensitive, for example "Aa" is not considered a palindrome here
解题思路:
这道题我一直WA是因为理解错了题意。。。要构建最长的回文串,那么出现奇数次数的字母也可以用啊,去掉一个就好了。之前为什么会理解成,奇数次数的字母只能
挑一个来用呢=。=
不贴代码了,好蠢=。=
290. Word Pattern

解题思路:
这道题需要注意的是,要求字母和字符串一一匹配。所以在判断的时候,需要再检查一遍字典的value部分。另外,在前面切割
字符串的时候,最后一个单词,因为没有空格跟着,所以最后要将temp再压栈一次。
bool wordPattern(string pattern, string str) {
vector<string> v;
string temp = "";
for (int i = 0; i < str.length(); i++) {
if (str[i] != ' ')
temp += str[i];
else {
v.push_back(temp);
temp = "";
}
}
v.push_back(temp);
if (pattern.length() != v.size())
return false;
map<string, char> dict;
map<string, char>::iterator it;
int i;
for (i = 0; i < pattern.length(); i++) {
if (dict.find(v[i]) == dict.end()) {
for (it = dict.begin(); it != dict.end(); it++) {
if (it->second == pattern[i] && it->first != v[i])
return false;
if (it->second == pattern[i] && it->first == v[i])
break;
}
if (it == dict.end())
dict.insert(make_pair(v[i], pattern[i]));
else
continue;
} else {
if (dict.find(v[i])->second != pattern[i])
return false;
}
}
return true;
}
20. Valid Parentheses

解题思路:
这道题比较简单,只需要用通过进栈出栈来匹配就好了。需要注意的是,例子中有类似这种情况"}",所以在判断时要关注栈是否为空。
bool isValid(string s) {
stack<char> st;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
st.push(s[i]);
continue;
}
else {
// important here
if (st.empty() == true)
return false;
if (s[i] == ')' && st.top() == '(') {
st.pop();
continue;
}
if (s[i] == ']' && st.top() == '[') {
st.pop();
continue;
}
if (s[i] == '}' && st.top() == '{') {
st.pop();
continue;
}
if (s[i] == ')' && st.top() != '(' || s[i] == ']' && st.top() != '[' || s[i] == '}' && st.top() != '{') {
return false;
}
}
}
return st.empty();
}
leetcode-12-stack的更多相关文章
- leetcode 12题 数字转罗马数字
leetcode 12题 数字转罗马数字 答案一:我的代码 代码本地运行完全正确,在线运行出错 class Solution { public: string intToRoman(int num) ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- [LeetCode] Max Stack 最大栈
Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...
- LeetCode Monotone Stack Summary 单调栈小结
话说博主在写Max Chunks To Make Sorted II这篇帖子的解法四时,写到使用单调栈Monotone Stack的解法时,突然脑中触电一般,想起了之前曾经在此贴LeetCode Al ...
- LeetCode Max Stack
原题链接在这里:https://leetcode.com/problems/max-stack/description/ 题目: Design a max stack that supports pu ...
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- Leetcode 12,452,455-贪心算法
Leetcode第12题,整数转罗马数字,难度中等 整个题目比较好理解,难度也不大,就算不过脑子,用一串if也基本上可以解决问题,比如 /** 执行用时:6ms,在所有 Java 提交中击败了52.6 ...
- Java实现 LeetCode 12 整数转罗马数字
12. 整数转罗马数字 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 ...
随机推荐
- byte取高4位,低4位,byte转int
byte abyte =-1; System.out.println(abyte); System.out.println(Integer.toBinaryString(abyte)); //取高四位 ...
- Jmeter4.0----录制脚本(4)
1.前言 Jmeter录制脚本有两种方式.1.通过第三方工具录制比如:Badboy,然后转化为jmeter可用的脚本:2.使用jmeter本身自带的录制脚本功能. 对于测试小白来说可用先使用jmete ...
- log4j.properties错误及配置详解
当在Eclipse上运行MapReduce程序遇到以上问题时,请检查项目中是否有log4j.properties配置文件,或者配置文件是否正确. 刚接触Hadoop的时候不太了解log4j.prope ...
- 项目打包发布到tomcat中,中文出现乱码
先吐槽一下,花了我3个小时,心累 本地运行正常,发布时maven插件里要加utf-8编码 https://blog.csdn.net/testcs_dn/article/details/4558379 ...
- 从零开始利用vue-cli搭建简单音乐网站(一)
最近在学习vue框架,练习了一些例子之后,想着搭建一个vue项目,了解到官方有提供一个vue-cli工具来搭建项目脚手架,尝试了一下,写下博客来记录一下. 一.工具环境 1.node.js 6.10. ...
- django之基于cookie和装饰器实现用户认证
示例1 # Create your views here. user = "a" pwd = "a" def login(request): if reques ...
- js插件设置innerHTML时,在IE8下报错“未知运行时错误”
问题描述: 网站中使用了一个js插件,设置innerHTML时,在IE8下报错“未知运行时错误”: <div id=”divContainer”> <a name=”link”> ...
- Python + selenium之unitest(2)
unittest单元测试框架中重要的概念: 1.Test Case 一个Test Case实例就是一个测试用例.在一个完整的测试流程中,包括测试前准备环境的搭建(setUp),实现测试过程的代码(ru ...
- jsp四大作用域之request
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- .net代码获取节点以及读取属性
获取配置文件的节点,可以使用System.Configuration.ConfigurationManager.GetSection方法获取指定的节点,以sessionstate节点为例,如果需要获取 ...