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的更多相关文章

  1. Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

    题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...

  2. 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 ...

  3. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  4. 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 ...

  5. 【leetcode_easy】557. Reverse Words in a String III

    problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. LVS集群的ipvsadm命令用法

    准备一台Linux服务器,安装ipvsadm软件包,练习使用ipvsadm命令,实现如下功能: - 使用命令添加基于TCP一些的集群服务 - 在集群中添加若干台后端真实服务器 - 实现同一客户端访问, ...

  2. Linux知识点拾遗-磁盘UUID

    查看磁盘UUID 方法1 ls -l /dev/disk/by-uuid example: [root@dplinux ~]# ll /dev/disk/by-uuid/ total 0 lrwxrw ...

  3. 2017 BJ ICPC 石子合并变种 向量基本功及分类考察

    E 模拟 #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) # ...

  4. python笔记(2)--字符串

    一.创建字符串 使用 '' , "" , ''' 和 """ 来创建字符串 var1 = 'hello, world' var2 = "ja ...

  5. DevExpress ASP.NET Core Controls 2019发展蓝图(No.5)

    本文主要为大家介绍DevExpress ASP.NET Core Controls 2019年的官方发展蓝图,更多精彩内容欢迎持续收藏关注哦~ [DevExpress ASP.NET Controls ...

  6. 如何用DNS+GeoIP+Nginx+Varnish做世界级的CDN

    如何用DNS+GeoIP+Nginx+Varnish做世界级的CDN     如何用BIND, GeoIP, Nginx, Varnish来创建你自己的高效的CDN网络?CDN,意思是Content ...

  7. 用C语音编写python的扩展模块,也就是python调c库

    用C语音编写python的扩展模块,也就是python调c库   1.用C语言扩展Python的功能: http://www.ibm.com/developerworks/cn/linux/l-pyt ...

  8. Python操作 Excel表格

    python 读写 excel 有好多选择,但是,方便操作的库不多,在我尝试了几个库之后,我觉得两个比较方便的库分别是 xlrd/xlwt.openpyxl. 我使用openpyxl 安装: pip ...

  9. HTML页面滑动到最底部触发事件

    其实基本原理做一个判断,如果 页面总高度  =  视口高度  +  浏览器窗口上边界内容高度 ,那么就是把页面滑动到了最低部,然后执行一个事件. //要触发的事件(自己定义事件的内容) functio ...

  10. js 将时间戳转成时间格式化

    一.时间戳 时间戳是以时间元年1970年开始算起到当前时间的一个值,以秒为单位,比如1535694719秒,如何转化为我们想要的格式,yyyy/mm/dd或者yyyy-MM-dd hh:mm,格式根据 ...