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 ...
随机推荐
- Vue 中如何定义全局的变量和常量
Vue 中如何定义全局的变量和常量 我想要定义一个变量, 在项目的任何地方都可以访问到, 不需要每一次使用的时候, 都引入. 尝试1:创建 global.js 并且在其中定义 let a = 10 ...
- markdown编辑器学习
markdown是一块文本编辑器,属于纯文本文件,可以使用任何编辑器打开.对于写作来说是一个好帮手,它的好处有很多,比如可以直接转成html,制作电子书等.今天开始学习一下这个神奇的编辑器.从今天起把 ...
- java复习(6)String、StringBuffer以及StringBuilder
0.常见的编码表 ASC||:美国标准信息交换码,用一个字节的7位可以表示. ISO8859-1:拉丁码表.欧洲码表,用一个字节的8位来表示.无法存储汉字,或者只取了汉字的一半使用 GB2312:中文 ...
- 华为设备acl配置
拓扑图: 需求: 1.-vlan10内所有的主机,只能通过http访问vlan30-server的服务器;不能访问vlan40-server服务器2.-vlan20-pc1主机,可以访问vlan40- ...
- glup安装
资料参考:http://www.w3ctrain.com/2015/12/22/gulp-for-beginners/ 1.在安装 node 的环境后: npm install gulp -g 全局安 ...
- Utorrent死机恢复种子下载
死机保存Utorrent种子不被删除方法: 保了200多个种,死机了重启就没有什么下载的种子的记录,要一个个导入实在奔溃. 从被删除的resume.dat恢复很有压力. 简单的方法: 在还没有死机前, ...
- CF 937
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- 做股票软件用的各种k线图
这是各种k线图地址: http://echarts.baidu.com/echarts2/doc/example.html 个人公众号谢谢各位老铁支持 本人qq群也有许多的技术文档,希望可以为你提供一 ...
- 洛谷P4003 [国家集训队2017]无限之环 网络流 最小费用最大流
题意简述 有一个\(n\times m\)棋盘,棋盘上每个格子上有一个水管.水管共有\(16\)种,用一个\(4\)位二进制数来表示当前水管向上.右.下.左有个接口.你可以旋转除了\((0101)_2 ...
- Python中的网络扫描大杀器Scapy初探
Python中的网络扫描大杀器Scapy初探 最近经历了Twisted的打击,这个网络编程实在看不懂,都摸不透它的内在逻辑,看来网络编程不是那么好弄的.还好,看到了scapy,这种网络的大杀器 ...