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 ...
随机推荐
- C++实现简单的日志记录
C++实现简单的日志记录 //dlogger.h #ifndef DLOGGER_H #define DLOGGER_H #include <iostream> #include < ...
- js防抖和节流优化浏览器滚动条滚动到最下面时加载更多数据
防抖和节流,主要是用来防止过于平凡的执行某个操作,如浏览器窗口变化执行某个操作,监听某个input输入框keyup变化,瀑布流布局时Y轴滚动,图片加载. js函数的防抖 经过一段事件才执行某个操作,如 ...
- 2019-11-28-win10-uwp-提示-Cannot-find-a-Resource-with-the-Name-Key-找不到资源
title author date CreateTime categories win10 uwp 提示 Cannot find a Resource with the Name Key 找不到资源 ...
- JavaEE高级-JPA学习笔记
*JPA概述 *JPA是什么? - Java Persistence API :用于对象持久化的API - Java EE 5.0平台标准的ORM规范,使得应用程序以统一的方式访问持久化层 - JPA ...
- 14.Linux-CentOS系统proc文件系统丢失
问题: 在强制卸载根目录下的磁盘,导致/proc文件系统丢失. 解决: 重启服务器重新生成.
- 4、LayIM 开发者文档
一.配置文档目录 1.好友列表状态 2.当前会话状态 3.查看群成员 4.业务暂无此必要 5.发送消息 6.接受消息 7.监听我主面板的在线状态 8.弹出申请好友面板(业务场景用于好友添加需申请) 9 ...
- PAT Basic 1036 跟奥巴马一起编程 (15 分)
美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014 年底,为庆祝“计算机科学教育周”正式启动,奥巴马编写了很简单的计算机代码:在屏幕上画一个 ...
- hdu 4638 Group(离线+树状数组)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- linux如何在shell中自动生成1到100的数组
之前自己在写shell脚本的时候,需要自动创建1-100的文本确不知道该如何去创建.百度一翻终于知道了创建的方法. 在shell脚本中创建1-100的方法很多,那我在这里主要就说两种容易理解且方便的方 ...
- mnist 卷积神经网络
# from keras.models import Sequential# from keras.layers.core import Dense,Activation,Flatten #creat ...