zoukankan      html  css  js  c++  java
  • leetcode-2-basic

    解题思路:

    题目本身挺简单的,考虑用set,判断每个单词的字母是不是属于同一个集合。需要注意的是:1)set的构造方法;2)单词可能是大小写混合的,不一定只是首字母大写;

    3)break是跳出循环=。=switch写的时候要注意,不好用的话还是用if-else好了。

        vector<string> findWords(vector<string>& words) {
            char f[20] = {'q','w','e','r','t','y','u','i','o','p','Q','W','E','R','T','Y','U','I','O','P'};
            set<char>first(f,f+20);
            char s[18] = {'a','s','d','f','g','h','j','k','l','A','S','D','F','G','H','J','K','L'};
            set<char>second(s, s+18);
            char t[14] = {'z','x','c','v','b','n','m','Z','X','C','V','B','N','M'};
            set<char>third(t, t+14);
            vector<string>::iterator it;
            string temp = "";
            vector<string> result;
            bool flag = true;
            int i;
            for (it = words.begin(); it != words.end(); it++) {
    
                temp = *it;
                flag = true;
    
                if (temp.length() == 1) {
                    result.insert(result.end(), temp);
                    continue;
                }
               if (first.find(temp[0]) != first.end()) {
                        for (i = 1; i < temp.length(); i++) {
                            if (first.find(temp[i]) == first.end()) {
                                break;
                            }
                        }
                        if (i != temp.length()) {
                            flag = false;
                            continue;
                        }
                        }
                else if (second.find(temp[0]) != second.end()) {
                        for (i = 1; i < temp.length(); i++) {
                            if (second.find(temp[i]) == second.end()) {
                                break;
                            }
                        }
                        if (i != temp.length()) {
                            flag = false;
                            continue;
                        }
                        }
                 else {
                        for (i = 1; i < temp.length(); i++) {
                            if (third.find(temp[i]) == third.end()) {
                                break;
                            }
                        }
                        if (i != temp.length()) {
                            flag = false;
                            continue;
                        }
                        }
                    
                if (flag == true) {
                    result.insert(result.end(), temp);
                }
            }
            return result;
        }

     

  • 相关阅读:
    学习 Apache FileMatchs 规则
    yii2 vendor/bower/jquery/dist not exist
    Ionic POST提交使用普通表单提交数据
    Yii2 在php 7.2环境下运行,提示 Cannot use ‘Object’ as class name
    Yii2 使用 npm 安装的包
    phpStorm 激活
    Chrome DNS_PROBE_FINISHED_NXDOMAIN
    Yii2 中使用ts
    Js 对 浏览器 的 URL的操作
    js 编码、解码与asp.net 编码、解码
  • 原文地址:https://www.cnblogs.com/pxy7896/p/6478599.html
Copyright © 2011-2022 走看看