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. AFNetworking2.0源码解析<三>

    本篇说说安全相关的AFSecurityPolicy模块,AFSecurityPolicy用于验证HTTPS请求的证书,先来看看HTTPS的原理和证书相关的几个问题. HTTPS HTTPS连接建立过程 ...

  2. windows上的QT发布

    1. 生成exe 1.1  release 条件下编译 1.2 将QT bin路径加入到path中去 1.3 将1.1生成的exe拷贝到单独的目录,然后cmd到这个目录,使用windeployqt编译 ...

  3. tee 多重定向

    1.命令功能 tee读取标准输入的数据,并将内容输出成文件. 2.语法格式 tee  option  file tee  [-ai]  文件 参数说明 参数 参数说明 -a 追加到文件后面,非覆盖 - ...

  4. Codeforces 939 时区模拟 三分

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

  5. python:实例属性和类属性

    由于Python是动态语言,根据类创建的实例可以任意绑定属性. 给实例绑定属性的方法是通过实例变量,或者通过self变量: class Student(object): def __init__(se ...

  6. Linux shell 批量验证端口连通性

    工作中会遇到验证到某某服务器端口是否连通,如果IP或端口多时,用shell还是很省时省力的,看下面的脚本: #!/bin/bash # #database check #set -o nounset ...

  7. Django【第27篇】:ModelForm

    基于Form组件实现的增删改和基于ModelForm实现的增删改 一.ModelForm的介绍 ModelForm a. class Meta: model, # 对应Model的 fields=No ...

  8. Pool数据池

    sql相关请点我!!! 1.普通的sql语句查询完成之后,就要断开,下次查的时候又要重新开启,这样的话,效率会很低,所以利用pool 数据池来解决这种问题,pool数据池查询完之后,就不用去重新链接数 ...

  9. man---中英文翻译

    一. 总览---SYNOPSIS 二.

  10. 详述 DB2 分页查询及 Java 实现的示例_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 博主说:有时候,我们需要对数据库中现有的数据进行大量处理操作(例如表中的某个字段需要全部更新等),如果直接使用selec ...