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. mysql 驱动问题

    转 : https://blog.csdn.net/bloodycuckoo/article/details/51175339 转 : https://blog.csdn.net/u010746431 ...

  2. EOJ Problem #3261 分词 trie + dp + 小剪枝

    http://acm.ecnu.edu.cn/problem/3261/ 分词 Time limit per test: 1.0 seconds Time limit all tests: 1.0 s ...

  3. SSAS 部署之创建部署脚本

    1.获取多维数据库的结构脚本: 当你的SSAS项目完成后,在Bin目录下会有一个SSAS.asdatabase文件. 2.打开“开始” ->Microsoft SQL Server 2008 R ...

  4. nginx超时问题

    一. 戏说不管你是做运维还是做开发,哪怕你是游客,时不时会遇到502 Bad Gateway或504 Gateway Time-out.出现这页面,把服务重启下,再实在不行重启下服务器,问题就解决了, ...

  5. SyntaxError: Use of const in strict mode.

    具体报错console c:\Users\Administrator\WebstormProjects\blogtest\node_modules\connect-mongo\src\index.js ...

  6. Ionic开发-搭建开发环境

    1安装node.js 2安装ionic & cordova: 命令行输入:npm install –g cordova ionic 注:-g表示全局安装,也可以进入指定的目录安装,但这里推荐全 ...

  7. eclipse查看jar包源文件

    话不多说上链接 https://www.cnblogs.com/1995hxt/p/5252098.html这里介绍了完整的流程,亲自试过,可以的! 以防以后要用的时候找不到文件的下载地址,所以就先在 ...

  8. CallContext的LogicalCallContext在多线程环境下面公用变量

    压根名听说过这个类的看这里:如何实现对上下文(Context)数据的统一管理 原来以为CallContext就可以直接在多线程环境下面共享使用的,今天突然想到:Asp.Net环境下面,设置来设置去的, ...

  9. 开源项目android-uitableview介绍

    在iOS应用中,UITableView应该是使用率最高的视图之一了.iPod.时钟.日历.备忘录.Mail.天气.照片.电话.短信. Safari.App Store.iTunes.Game Cent ...

  10. easyUI filebox限定文件大小

    转载自:https://www.2cto.com/kf/201701/574667.html 侵删  easyui1.5filebox控件中增加文件大小的验证规则 2017-01-07 09:22:0 ...