Week4 - 500.Keyboard Row & 557.Reverse Words in a String III
500.Keyboard Row & 557.Reverse Words in a String III
500.Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
Note:
1.You may use one character in the keyboard more than once.
2.You may assume the input string will only contain letters of alphabet.
#include<string>
#include<iostream>
#include<vector>
using namespace std;
static int alphabet[] = { 2,1,1,2,3,2,2,2,3,2,2,2,1,1,3,3,3,3,2,3,3,1,3,1,3,1,0,0,0,0,0,0,2,1,1,2,3,2,2,2,3,2,2,2,1,1,3,3,3,3,2,3,3,1,3,1,3,1 };
class Solution {
public:
vector<string> findWords(vector<string>& words) {
vector<string> result;
for (int i = 0; i < words.size(); i++) {
int flag = 0;
bool same = false;
if (words[i].size() > 0) flag = alphabet[(int)words[i][0] - 'A'];
same = true;
for (int j = 0; j < words.at(i).length(); j++) {
if (alphabet[words[i][j] - 'A'] != flag) {
same = false;
break;
}
}
if (same == true) result.push_back(words[i]);
}
return result;
}
};
557.Reverse Words in a String III
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
#include<string>
#include<iostream>
#include<vector>
#include<list>
using namespace std;
class Solution {
public:
string reverseWords(string s) {
string result;
int flag = 0;
for (int i = 0; i < s.length(); i++) {
if (s[i] == ' ') {
for (int j = i - 1; j >= flag; j--) {
result.push_back(s[j]);
}
result.push_back(' ');
flag = i + 1;
}
}
for (int j = s.length()-1; j >= flag; j--) {
result.push_back(s[j]);
}
return result;
}
};
Week4 - 500.Keyboard Row & 557.Reverse Words in a String III的更多相关文章
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 557. Reverse Words in a String III【easy】
557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters ...
- 【leetcode_easy】557. Reverse Words in a String III
problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...
- 557. Reverse Words in a String III 翻转句子中的每一个单词
[抄题]: Given a string, you need to reverse the order of characters in each word within a sentence whi ...
- [LeetCode] 557. Reverse Words in a String III 翻转字符串中的单词 III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- LeetCode 557 Reverse Words in a String III 解题报告
题目要求 Given a string, you need to reverse the order of characters in each word within a sentence whil ...
- [LeetCode&Python] Problem 557. Reverse Words in a String III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
随机推荐
- Ionic创建混合App(二)
ionic 2 启动应用进入欢迎引导页 1.首先,使用CLI命令,创建引导页面 ionic g page welcome 2.需改welcome.html模板文件 <ion-slides pag ...
- sql中关闭自增,并插入数据
ET IDENTITY_INSERT 允许将显式值插入表的标识列中. 语法 SET IDENTITY_INSERT [ database.[ owner.] ] { table } { ON | OF ...
- Django扩展内置User类
内置User类 使用内置User可以方便实现登录验证,利用Admin管理界面还可以方便添加.删除.修改用户. 一个内置的User类定义了以下字段: username: 用户名 password: 密码 ...
- 一、MyBatis基本使用,包括xml方式、注解方式、及动态SQL
一.简介 发展历史:MyBatis 的前 身是 iBATIS.最初侧重于 密码软件的开发 , 后来发展成为一款基于 Java 的持久层框架. 定 位:MyBatis 是一款优秀的支持自定义 ...
- SpringBoot整合redis把用户登录信息存入redis
首先引入redis的jai包 <dependency> <groupId>org.springframework.boot</groupId> <artifa ...
- 缓存算法LRU笔记
LRU原理与分析 LRU是Least Recently Used 的缩写,翻译过来就是“最近最少使用”,也就是说,LRU缓存把最近最少使用的数据移除,让给最新读取的数据.而往往最常读取的,也是读取 ...
- 消息中间件之 RabbitMQ
文章内容来源 https://www.cnblogs.com/jun-ma/p/4840869.html 延伸阅读文章 https://blog.csdn.net/growing_duck/artic ...
- Spring5最新完整教程IDEA版【通俗易懂2019.11月】
1.Maven找包: spring-webmvc spring-jdbc 2.Spring的本质是控制反转,依靠依赖注入来实现.以一个servcie对象为例,即是service暴露注入接口(构造,se ...
- POJ 3261 Milk Patterns ( 后缀数组 && 出现k次最长可重叠子串长度 )
题意 : 给出一个长度为 N 的序列,再给出一个 K 要求求出出现了至少 K 次的最长可重叠子串的长度 分析 : 后缀数组套路题,思路是二分长度再对于每一个长度进行判断,判断过程就是对于 Height ...
- Deque 容器
1.Deque (1)定义 deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容.deque容器类与vector类似,支持随机访问和快速插入删除, ...