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.

  1. #include<string>
  2. #include<iostream>
  3. #include<vector>
  4. using namespace std;
  5. 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 };
  6. class Solution {
  7. public:
  8. vector<string> findWords(vector<string>& words) {
  9. vector<string> result;
  10. for (int i = 0; i < words.size(); i++) {
  11. int flag = 0;
  12. bool same = false;
  13. if (words[i].size() > 0) flag = alphabet[(int)words[i][0] - 'A'];
  14. same = true;
  15. for (int j = 0; j < words.at(i).length(); j++) {
  16. if (alphabet[words[i][j] - 'A'] != flag) {
  17. same = false;
  18. break;
  19. }
  20. }
  21. if (same == true) result.push_back(words[i]);
  22. }
  23. return result;
  24. }
  25. };

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.

  1. #include<string>
  2. #include<iostream>
  3. #include<vector>
  4. #include<list>
  5. using namespace std;
  6. class Solution {
  7. public:
  8. string reverseWords(string s) {
  9. string result;
  10. int flag = 0;
  11. for (int i = 0; i < s.length(); i++) {
  12. if (s[i] == ' ') {
  13. for (int j = i - 1; j >= flag; j--) {
  14. result.push_back(s[j]);
  15. }
  16. result.push_back(' ');
  17. flag = i + 1;
  18. }
  19. }
  20. for (int j = s.length()-1; j >= flag; j--) {
  21. result.push_back(s[j]);
  22. }
  23. return result;
  24. }
  25. };

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. 神经网络中的反向传播法--bp【转载】

    from: 作者:Charlotte77 出处:http://www.cnblogs.com/charlotte77/ 一文弄懂神经网络中的反向传播法——BackPropagation 最近在看深度学 ...

  2. Spring基础19——Spring中几种注解的区别

    1.@Autowired:注解是用来装配bean的,需要注入的bean必须是已经被IOC容器创建的bean,这个注解是利用类型装配的,如果容器中出现一个以上要装配的类或其子类就需要用@Qualifie ...

  3. Spring基础13——Spring表达式语言:SpEL

    1.SpEL简介 Spring表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言.语法上类似于EL:SpEL使用#{...}作为界定符,所有在大框号中的字符都将被认为是Sp ...

  4. Linux系统性能测试工具(五)——磁盘io性能工具之fio

    本文介绍关于Linux系统(适用于centos/ubuntu等)的磁盘io性能测试工具-fio.磁盘io性能测试工具包括: fio: dd

  5. 魔咒词典 HDU - 1880 (字符串hash 单hash转int或者 双hash )

    哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...

  6. thymeleaf常用模板方法收集

    判断是不是为空1.th:if="${xxx} != null" th:if="${xxx != null}" 是不是为空字符串 1.th:if="${ ...

  7. 关于CSS你应该知道的基础知识 - 盒模型篇

    浏览器渲染引擎通过盒模型的方式来布局html元素.我们可以将每一个html元素都看做是一个盒子,每一个盒子都有长和款,多个这样的盒子组成了我们的网页. Margin,Border,Padding 每一 ...

  8. 真正解决iframe高度自适应问题

    1.前言 解决iframe高度自适应问题有两种方法1.pym2.手动设置iframe的高度 本文主要是总结第二种实现方式,因为第一种pym.js插件我没用懂 如果使用iframe时,遇到以下的需求: ...

  9. 【leetcode】837. New 21 Game

    题目如下: 解题思路:这个题目有点像爬楼梯问题,只不过楼梯问题要求的计算多少种爬的方式,但是本题是计算概率.因为点数超过或者等于K后就不允许再增加新的点数了,因此我们可以确定最终Alice拥有的点数的 ...

  10. js+php大文件分片上传

    1 背景 用户本地有一份txt或者csv文件,无论是从业务数据库导出.还是其他途径获取,当需要使用蚂蚁的大数据分析工具进行数据加工.挖掘和共创应用的时候,首先要将本地文件上传至ODPS,普通的小文件通 ...