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

  1. leetcode 12题 数字转罗马数字

    leetcode 12题 数字转罗马数字 答案一:我的代码 代码本地运行完全正确,在线运行出错 class Solution { public: string intToRoman(int num) ...

  2. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  3. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  4. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

  5. [LeetCode] Max Stack 最大栈

    Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x onto ...

  6. LeetCode Monotone Stack Summary 单调栈小结

    话说博主在写Max Chunks To Make Sorted II这篇帖子的解法四时,写到使用单调栈Monotone Stack的解法时,突然脑中触电一般,想起了之前曾经在此贴LeetCode Al ...

  7. LeetCode Max Stack

    原题链接在这里:https://leetcode.com/problems/max-stack/description/ 题目: Design a max stack that supports pu ...

  8. [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 ...

  9. Leetcode 12,452,455-贪心算法

    Leetcode第12题,整数转罗马数字,难度中等 整个题目比较好理解,难度也不大,就算不过脑子,用一串if也基本上可以解决问题,比如 /** 执行用时:6ms,在所有 Java 提交中击败了52.6 ...

  10. 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 ...

随机推荐

  1. HDU 1176 免费馅饼 矩阵取数, dp + 滚动数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1176 首先可以处理出整张地图的状态. book[T][POS]表示第T秒,在第pos个地方有多少个馅饼. dp[ ...

  2. (转)mysqldump: Got error: 1556: You can't use locks with log tables.

    mysqldump: Got error: 1556: You can't use locks with log tables. 原文:http://blog.51cto.com/oldboy/112 ...

  3. ruby Iconv.iconv编码方法

    #定义一个UTF-8=>GBK的方法def encoding inStr    Iconv.iconv("GBK","UTF-8",inStr)end#定 ...

  4. c# ExpandoObject动态扩展对象

    js中的Object 对象. php中的stdClass. c# 也有动态可扩展对象 ExpandoObject,需要添加System.Dynamic引用 用法: dynamic model = ne ...

  5. JAVA 框架之面向对象设计原则

     面向对象设计原则:  单一职责原则 SRP :   一个类或者行为只做一件事 .  降低代码冗余,提高可重用性,可维护性,可扩展性,可读性 使用组合形式   里氏替换原则 LSP :  所有引用基类 ...

  6. JSTORM中IRichBolt与IBasicBolt的区别

  7. ios获取数据之encodeURI 和 decodeURI

    在APP开发过程中,免不了要进行ios的数据处理,在ios传递数据的过程中,会出现JSON数据获取不到的情况,这时候就轮到encodeURI 和 decodeURI出马了. 1.encodeURI,d ...

  8. IT部门域事件与业务分析

    IT event--->system--->IT dept |--------------->IT dept |--------------->system 域事件分类: 直接 ...

  9. Azure 镜像市场支持一键部署到云

    本视频教程介绍了Azure 镜像市场和一键部署到云. Azure 镜像市场(AMP)由世纪互联运营,是一个联机应用程序和服务市场,它通过独立软件服务商(ISV)能够成为 Azure 客户(Custom ...

  10. python 之网页解析器

    一.什么是网页解析器 1.网页解析器名词解释 首先让我们来了解下,什么是网页解析器,简单的说就是用来解析html网页的工具,准确的说:它是一个HTML网页信息提取工具,就是从html网页中解析提取出“ ...