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;
}
leetcode-2-basic的更多相关文章
- [LeetCode] 224. Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] 227. Basic Calculator II 基本计算器 II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [LeetCode] 772. Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] 227. Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- LeetCode#227.Basic Calculator II
题目 Implement a basic calculator to evaluate a simple expression string. The expression string contai ...
- Java for LeetCode 227 Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- Java for LeetCode 224 Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- (medium)LeetCode 224.Basic Calculator
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- (medium)LeetCode 227.Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- leetcode 224. Basic Calculator 、227. Basic Calculator II
这种题都要设置一个符号位的变量 224. Basic Calculator 设置数值和符号两个变量,遇到左括号将数值和符号加进栈中 class Solution { public: int calcu ...
随机推荐
- UWP 播放媒体控件
最近我的uwp需要有一个有声朗读的功能,like this 点击声音按钮就可以有声朗读了.这里主要是用了媒体播放的控件. 一般我们把需求分为两种: 一种是不需要呈现播放器的样子,只需要用户点击一下别的 ...
- 540 Single Element in a Sorted Array 有序数组中的单一元素
给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数.示例 1:输入: [1,1,2,3,3,4,4,8,8]输出: 2 示例 2:输入: [3,3,7,7,10,1 ...
- csu 1551: Longest Increasing Subsequence Again BIT + 思维
预处理last[i]表示以第i个开始,的合法后缀. pre[i]表示以第i个结尾,的合法前缀. 那么每一个数a[i],肯定是一个合法后缀last[i] + 一个合法前缀,那么合法前缀的数字要小于a[i ...
- c#中的特性
c#中的特性 特性在我的理解就是在类或者方法或者参数上加上指定的标记,然后实现指定的效果. 和Java中的注解@Annotation类似. c#内置的特性之Obsolete [Obsolete(&qu ...
- Bluefish
Bluefish标榜其自身是“一款为熟练的Web设计员和程序员而设的编辑器,但它的UI却很直观,任何初学者都能够很快上手,并在不断地积累中发现和掌握它的其它功能. Bluefish标榜其自身是“一 ...
- Code First约定-数据注释
通过实体框架Code First,可以使用您自己的域类表示 EF 执行查询.更改跟踪和更新函数所依赖的模型.Code First 利用称为“约定先于配置”的编程模式.这就是说,Code First 将 ...
- Ubuntu下Postgres安装与配置
postgres8.4安装配置:1.安装postgres8.4~$ sudo apt-get install postgresql 2.修改超级管理员postgres密码:以系统用户运行psql~$ ...
- Block中__block实现原理
三.Block中__block实现原理 我们继续研究一下__block实现原理. 1.普通非对象的变量 先来看看普通变量的情况. #import <Foundation/Foundation.h ...
- url编码和解码平台
http://meyerweb.com/eric/tools/dencoder/
- 以太坊开发框架Truffle学习笔记
from http://truffleframework.com/docs/getting_started/project 1. 安装node.js 8.11.2 LTS 2. 安装Truffle $ ...