Title:

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".

https://leetcode.com/problems/reverse-words-in-a-string/

思路:先将字符串全部逆转,在将每个单词逆转。具体实现,要注意空格,所以对字符串倒过来处理。

class Solution {
public:
void reverseWords(string &s)
{
string rs;
for (int i = s.length()-; i >= ; )
{
while (i >= && s[i] == ' ') i--;
if (i < ) break;
if (!rs.empty()) rs.push_back(' ');
string t;
while (i >= && s[i] != ' ') t.push_back(s[i--]);
reverse(t.begin(), t.end());
rs.append(t);
}
s = rs;
}
};

Rotate Array

Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].

https://leetcode.com/problems/rotate-array/

思路:一样,先将整个字符串反转,在对每个部分反转。另外注意k可能大于size。

class Solution {
public:
void rotate(vector<int>& nums, int k) {
if ( k < )
return ;
int size = nums.size();
k = (k-) % size + ;
reverse(nums,,size-);
reverse(nums,,k-);
reverse(nums,k,size-);
}
void reverse(vector<int>& nums,int start, int end){
while (start < end){
nums[start] ^= nums[end];
nums[end] ^= nums[start];
nums[start++] ^= nums[end--];
}
}
};

LeetCode: Reverse Words in a String && Rotate Array的更多相关文章

  1. [LeetCode] Reverse Words in a String II 翻转字符串中的单词之二

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  2. LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

    LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...

  3. LeetCode Reverse Words in a String II

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...

  4. 【LeetCode从零单排】No189 .Rotate Array

    称号 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...

  5. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  6. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

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

  8. LeetCode: Reverse Words in a String 解题报告

    Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...

  9. LeetCode Reverse Vowels of a String

    原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...

随机推荐

  1. jquery 图片背景透明度(支持IE5/IE6/IE7)

    设置背景图片,以突出透明度的效果及jquery png背景透明插件实例教程 <head> <title>toggle()</title> <style typ ...

  2. maven+springMVC+mybatis+junit详细搭建过程 ***

    springMVC+mybatis框架搭建 在上一遍博客中以及讲诉了新建maven项目的流程,现在紧跟上一遍文章,接着搭建spring项目 首先我们先要弄清搭建项目的一般流程,需要注意哪些方面,想要什 ...

  3. vc6命令行编译工程方法

    查vc++ 6.0 的 msdn找到下面的命令: msdev FileName [/MAKE "ProjectName – ConfigName | ALL"] [/REBUILD ...

  4. codeforces 397B

    #include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...

  5. 使用NodeJs,实现数据抓取

    学习笔记 前言 近期做一个数据抓爬工具,最开始使用的是C#控制台应用,同时正则表达式去过滤数据,看着还行,可每次运行都依附于.net framework很是不爽,于是想整点其他的方法.本人还是比较喜欢 ...

  6. NData BUG 记录

    一.collection 如果设计如下页面 页面模型如下 using UnityEngine; using System.Collections; using System.Collections.G ...

  7. SDUT2142数据结构实验之图论二:基于邻接表的广度优先搜索遍历

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜 ...

  8. Android Service 的一些笔记

    绑定服务: 用于间接调用服务里面的方法.如果调用者Activity被销毁了,服务也跟着销毁了,服务也会跟着销毁. 开启服务: 不可以调用服务里面的方法.如果调用者的Activity退出了,服务还会长期 ...

  9. 1050 Moving Tables

    Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  10. 【转】Android 实现“再按一次退出程序”

    From:http://blog.csdn.net/ldj299/article/details/7574365 个人觉得当用户按下后退键时,出现"再按一次退出"的提示防止误操作比 ...