【leetcode_easy】557. Reverse Words in a String III
problem
557. Reverse Words in a String III
solution1:字符流处理类istringstream.
class Solution {
public:
string reverseWords(string s) {
string ans = "", t = "";
istringstream is(s);//
while(is >> t)
{
reverse(t.begin(), t.end());
ans += t + " ";//err.
}
ans.pop_back();
return ans;
}
};
solution2:单词首尾指针。
class Solution {
public:
string reverseWords(string s) {
int start = , end = ;
while(start<s.size() && end<s.size())
{
while(end < s.size() && s[end]!=' ') end++;
for(int i=start, j=end-; i<j; i++, j--)//
{
swap(s[i], s[j]);//
}
start = ++end;
//start = end + 1;
//end++;
}
return s;
}
};
参考
1. Leetcode_easy_557. Reverse Words in a String III;
2. Grandyang;
完
【leetcode_easy】557. Reverse Words in a String III的更多相关文章
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 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 ...
- Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 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 ...
- Week4 - 500.Keyboard Row & 557.Reverse Words in a String III
500.Keyboard Row & 557.Reverse Words in a String III 500.Keyboard Row Given a List of words, ret ...
- 【LeetCode】151. Reverse Words in a String
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...
- 【leetcode】345. Reverse Vowels of a String
problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...
- 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 ...
随机推荐
- 转: Javascript收藏
搜集的一些javascript小技巧! 事件源对象 event.srcElement.tagName event.srcElement.type 捕获释放 event.srcElement.set ...
- ubuntu 18.04 enp8s0更改网口名称为eth0
尝试各种方法均不成功,后参考如下方法终于成功: 方法一:单纯改网卡名,重启后显示原网卡名. 如我的网卡名是enp8s0 >> ip link set enp8s0 down //关闭网卡 ...
- 记录一下关于DQN的想法
下载了几份代码,就两份没有报错通过了 DQN玩FlappyBird https://github.com/yenchenlin/DeepLearningFlappyBird DQN玩Cartpole ...
- django加载本地html
django加载本地html from django.shortcuts import render from django.http import HttpResponse from django. ...
- javascript---操作节点
快捷键:jiedian(节点) 节点增删改appendChild() insertBefore() replaceNode() <!DOCTYPE html><html lang=& ...
- Asteroids POJ - 3041 【最小点覆盖集】
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...
- Shell 02 数据运算/条件测试
一.整数运算工具 1.使用expr命令(运算两边必须有空格,引用变量时必须加$符号) [root@svr5 ~]# x=10 //定义变量x expr $x + 10 20 ...
- 参数类型 (@Controller层)
@RequestMapping(path = "/listPage")@SuppressWarnings("unchecked")@BussinessLog(v ...
- history API,判断页面是否是在跳转链接后返回
https://www.cnblogs.com/accordion/p/5699372.html history.replaceState(history.state, null, "htt ...
- [洛谷201704R1]开心派对小火车
OJ题号:洛谷P3697 思路: 贪心.首先从起点出发,开特急电车,对于每一个特急车站$s_{i}$,分别下一次车,计算从当前车站$s_{i}$出发坐各停电车在指定时限内$t$最远能够到达的车站$r_ ...