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. ubuntu下opencv2.4.9安装测试

    ubuntu下opencv2.4.9安装测试 whowhoha@outlook.com 一.依赖包安装 1.  build-essential 软件包 sudo apt-get install bui ...

  2. Java的synchronized关键字:同步机制总结

    JAVA中synchronized关键字能够作为函数的修饰符,也可作为函数内的语句,也就是平时说的同步方法和同步语句块.搞清楚synchronized锁定的是哪个对象,就能帮助我们设计更安全的多线程程 ...

  3. POJ 1928

    #include <iostream> #include <algorithm> #define MAXN 3000 using namespace std; struct n ...

  4. java基础知识回顾之javaIO类--File类应用:获取指定目录下面的指定扩展名的文件,将文件的绝对路径写入到目的文件当中

    /** * File文件综合应用 * 需求:获取指定目录下面,指定扩展名的文件,将文件的绝对路径写到文本文件当中. *  * 思路:1.需要深度遍历.--递归 * 2.遍历的过程中过滤指定扩展名的文件 ...

  5. Jedis 操作

    http://www.cnblogs.com/liuling/p/2014-4-19-04.html

  6. hdu 4315 Climbing the Hill 博弈论

    题意:有n个人爬山,山顶坐标为0,其他人按升序给出,不同的坐标只能容纳一个人(山顶不限),Alice和Bob轮流选择一个人让他移动任意步,但不能越过前面的人,且不能和前面一个人在相同的位置.现在有一个 ...

  7. Android 调节当前Activity的屏幕亮度

    调节的关键代码: WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.screenB ...

  8. Ubuntu 14.04 + Linux 3.14.34 系统调用实现文件拷贝

    采用 64位系统, ubuntu 14.04 + 新内核linux-3.14.34 下载地址https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.1 ...

  9. 三分初练QAQ

    求凸函数的极值的一般方法是三分 三分的思想大概是这样的: 例如我们要求下凸函数的极值 在区间[L,R]上, 我们定义m1为区间的第一个三等分点 定义m2为区间的第二个三等分点 设函数值为F(x) 则若 ...

  10. Makefile笔记

    一个简单的Makefile描述规则组成: TARGET...:PREREQUISITES... COMMANDS... ... target:规则的目标.通常是程序中间或者最后要生成的文件名,也可以是 ...