zoukankan      html  css  js  c++  java
  • 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();
        }  

  • 相关阅读:
    操作系统 chapter3 进程线程模型
    操作系统 chapter1 操作系统概述
    操作系统 chapter2 操作系统运行环境
    计算机网络 chapter 9 无线网络
    计算机网络 chapter 10 下一代因特网
    计算机网络 chapter 8 因特网上的音频/视频服务
    汇总常用的jQuery操作Table tr td方法
    jquery判断checkbox是否选中及改变checkbox状态
    $.ajax()方法详解
    wamp设置mysql默认编码
  • 原文地址:https://www.cnblogs.com/pxy7896/p/6547477.html
Copyright © 2011-2022 走看看