LeetCode: Reverse Words in a String && Rotate Array
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的更多相关文章
- [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 ...
- 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 ...
- LeetCode Reverse Words in a String II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- 【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 ...
- [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 ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [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 ...
- LeetCode: Reverse Words in a String 解题报告
Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...
- LeetCode Reverse Vowels of a String
原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...
随机推荐
- 【UVA】【11021】麻球繁衍
数序期望 刘汝佳老师的白书上的例题……参见白书 //UVA 11021 #include<cmath> #include<cstdio> #define rep(i,n) fo ...
- 20160729noip模拟赛zld
首先显然有多少个奇数,就有多少个回文串是最优的(没有奇数时构造一个回文串 然后有了k个“核心”,把剩下的字符顺序安排到这些的两侧,最后最短的回文串长度就是答案 #include<map> ...
- hdu1022 Train Problem I
http://acm.hdu.edu.cn/showproblem.php?pid=1022 #include<iostream> #include<stdio.h> #inc ...
- python 判断操作系统类型
#!/bin/python # import platform def TestPlatform(): print ("----------Operation System--------- ...
- AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
转载:http://blog.csdn.net/mhmyqn/article/details/25561535 HTTP请求中,如果是get请求,那么表单参数以name=value&name1 ...
- 核心思想:百度网盘怎么盈利(互联网的高速更新决定了:亏钱你还有点机会,放弃连门都进不了),战略预备队 good
百度做网盘很大程度就是为了防止别人依靠网盘做大和积累点技术储备.腾讯邮箱怎么赚钱?腾讯影音怎么赚钱?互联网的高速更新决定了,一些你看不起眼的软件很可能就会席卷整个市场,所以互联网大佬宁愿一些项目亏钱也 ...
- 致诸位新程序员:来自Chuck Jazdzewski慈父般的忠告
记住这几句话,学无止境.(Never stop learning.)沟通至关重要.(Communication is critical.)履行承诺,胜过交付.(Under promise, over ...
- Android:Context的作用
Context字面意思上下文,Activity中我们直接用this代替,而到了一个button的onClick(View view)等方法时,我们用this时就会报错,改用ActivityName.t ...
- IDEA使用的点点滴滴
查找一个类可以使用快捷键Ctrl + N 那么怎么看这个类中有哪些属性,哪些方法,就像Eclipse中的outline功能呢? 如查看NIO中的Buffer类,Ctrl + N-->
- 65. Valid Number
题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " = ...