【leetcode】557. Reverse Words in a String III
Algorithm
【leetcode】557. Reverse Words in a String III
https://leetcode.com/problems/reverse-words-in-a-string-iii/
1)problem
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.
2)answer
判断是否为空格,然后每个单词转换。有一个很好用的交换函数swap。
3)solution
class Solution {
public:
string reverseWords(string s) {
int begin = 0;
int end = 0;
for (int i = 0; i <= s.length(); ++i) {
//如果遇到了空格,就准备颠倒每个字符串的值
if (s[i] == ' ' || s[i] == '\0') {
end = i;
// 转换每个单词的值,end是单词的结尾长度,j是单词开始长度。
for (int j = begin; j < end; ++j) {
std::swap(s[j], s[--end]);
}
begin = i + 1;
}
}
return s;
}
};
【leetcode】557. Reverse Words in a String III的更多相关文章
- 【LeetCode】557. Reverse Words in a String III 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【leetcode_easy】557. Reverse Words in a String III
problem 557. Reverse Words in a String III solution1:字符流处理类istringstream. class Solution { public: s ...
- 【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) { , ...
- 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...
- 【LeetCode】151. Reverse Words in a String 翻转字符串里的单词(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.co ...
- 【LeetCode】186. Reverse Words in a String II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 每个单词单独翻转+总的翻转 日期 题目地址:https ...
- 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" 输 ...
随机推荐
- 2018ccpc湖南邀请赛后记
第一次出省去打邀请赛,赛前给队友定的目标是打个铜,这样奖金就可以报销我们的伙食费了 5.12 热身赛,ak的心态冲进去,爆零逃出来 (为什么热身赛没有签到题啊),出来一度以为这场比赛要打铁,毕竟老远过 ...
- servlet模拟SpringMVC
1. web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&q ...
- SearchServer Elasticsearch Cluster / kibana
S 使用nginx代理kibana并设置身份验证 https://blog.csdn.net/wyl9527/article/details/72598112 使用nginx代理kibana并设置身份 ...
- MyBatis-${}与#{}
一.看两种取值的效果 <select id="selectMyUserIdAndAge" resultType="myUser"> select * ...
- ffmpeg的各种黑科技
获取音频的时长 /** * 获取视频文件的时长 * @param ffmpegPath 是ffmpeg软件存放的目录,sourceFile是目标文件 * @return */ public Strin ...
- Linux记录-HDFS副本机制
1. 副本策略 NameNode具有RackAware机架感知功能,这个可以配置. 若client为DataNode节点,那存储block时,规则为:副本1,同client的节点上:副本2,不同机架节 ...
- JAVA-String,StringBuilder,StringBuffer例子(基础必备)
package com.net.xinfang.reflect; import java.io.UnsupportedEncodingException; /** * String不属于8种基本数据类 ...
- 在CentOS7上安装Zabbix3.0
#!/bin/bash # # .配置无人值守的安装,定义安装过程中需要用到的一些信息 # mysql_root_pw=root_pw mysql_zabbix_pw=zabbix_pw DBPass ...
- MDB数据类型注意事项
在ArcGIS中,对MDB数据类型的支持较少,如下图: 当时当你打开Access软件的时候,你会发现文本类型,其实有很多种: 假如涉及到的字段类型是文本,而且又比较长,最好设置为“备注”
- Excel公式使用
IF语句: 想要给F16自动求值,可以利用IF语句,如下: 语句: 同时,还需要设置B27的单元格格式为“常规”