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();
}  

最新文章

  1. sql事务(Transaction)用法介绍及回滚实例
  2. 2014优秀的好用的20款免费jQuery插件推荐
  3. 泛型中? super T和? extends T的区别
  4. 常用的sql脚本 游标遍历操作
  5. 【poj2724】 Purifying Machine
  6. sqlmap文件在tomcat7中运行报错原因及&lt;![CDATA[ ]]&gt;
  7. POJ 1113 Wall 求凸包的两种方法
  8. 前端开发者使用JS框架的三个等级
  9. 本科非cs菜鸟计算机面试实录
  10. Web Api 图片上传,在使用 Task.ContinueWith 变量无法赋值问题
  11. Python基础学习7---异常处理
  12. Beta版本冲刺计划及安排(附七天冲刺的博客链接)
  13. CSS3 3D立方体效果
  14. 一口一口吃掉Hibernate(五)——一对多单向关联映射
  15. Raiden Charge
  16. git远端密码修改
  17. 1.4 SQL函数
  18. tomcat 配置 使用综合
  19. 自学Aruba4.1-Aruba开机初始化
  20. android 之 Hnadler 、Message 、Looper

热门文章

  1. 【手撸一个ORM】第三步、SQL语句构造器和SqlParameter封装
  2. SpringBoot---核心---日志配置
  3. Linux下无法挂载U盘
  4. 在MVC中使用dotless后台动态解析LESSCSS的学习笔记
  5. 执行ng build --prod --aot命令报错
  6. Xamarin.Form的坑
  7. 数据结构-List接口-LinkedList类-Set接口-HashSet类-Collection总结
  8. Android Studio 编译错误 Error:Execution failed for task &#39;:app:buildInfoDebugLoader&#39;.
  9. javascript简单计算器实践
  10. MySQL查询示例