151 Reverse Words in a String

 class Solution {
public:
void reverseWords(string &s) {
string result;
for (int i = s.size() - ; i >= ;) {
while (i >= && s[i] == ' ') {
i--;
}
if (i < ) {
break;
} string word;
while (i >= && s[i] != ' ') {
word += s[i];
i--;
}
reverse(word.begin(), word.end());
if (!result.empty()) {
result += ' ';
}
result += word;
}
s = result;
}
};

细节处理。

186 Reverse Words in a String II

 class Solution {
public:
void reverseWords(string& s) {
reverse(s, , s.length);
for (int i=, j=; j<=s.length; j++) {
if (j==s.length || s[j]==' ') {
reverse(s, i, j);
i = j + ;
}
}
} void reverse(string& s, int begin, int end) {
while (begin < end - ) {
swap(s[begin], s[end - ]);
begin++; end--;
}
}
};

是上一题的简化版。先翻转整个string,再逐个单词翻转。其中,专门用一个下标 i 来指示单词的起始位置。

233 number of digit 1s

leetcode summary-section II的更多相关文章

  1. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  2. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  3. LeetCode:课程表II【210】

    LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...

  4. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  5. P1182 数列分段Section II

    P1182 数列分段Section II 题目描述 对于给定的一个长度为N的正整数数列A[i],现要将其分成M(M≤N)段,并要求每段连续,且每段和的最大值最小. 关于最大值最小: 例如一数列4 2 ...

  6. LeetCode:子集 II【90】

    LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...

  7. 洛谷 P1182 数列分段 Section II

    洛谷 P1182 数列分段 Section II 洛谷传送门 题目描述 对于给定的一个长度为N的正整数数列A-iA−i,现要将其分成M(M≤N)M(M≤N)段,并要求每段连续,且每段和的最大值最小. ...

  8. [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用

    [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用 考虑到现实因素,LeetCode每日一题不再每天都写题解了(甚至有可能掉题目?--)但对 ...

  9. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  10. [LeetCode] Summary Ranges 总结区间

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

随机推荐

  1. 使用InstallUtil安装及卸载Windows服务的具体操作 Visual Studio 2012版本

    关于Visual Studio 2012中使用InstallUtil对Windows服务进行安装与卸载的文章,在MSDN中的http://msdn.microsoft.com/en-us/librar ...

  2. Thinkphp3.2.X自动生成应用目录

    从3.2.2版本开始,可以支持自动生成默认模块之外的模块目录以及批量生成控制器和模型类. 例如,如果我们需要生成一个Admin模块用于后台应用,在应用入口文件中定义如下: // 绑定Admin模块到当 ...

  3. Swagger与SpringMVC项目整合

    Swagger与SpringMVC项目整合 来源:http://www.2cto.com/kf/201502/376959.html 为了方便的管理项目中API接口,在网上找了好多关于API接口管理的 ...

  4. 性能调优-CPU方面,内存方面

    CPU调优 首先要清楚数据库应用的分类,一般分为两类:OLTP(Online Transaction Processing,在线事务处理)和OLAP(Online Analytical Process ...

  5. Java 中 String 的构造方法

    String 对于所有 Java 程序员来说都不会陌生,几乎每天甚至每个程序都会和 String 打交道,因此将 String 的常用知识汇集在此,方便查阅. 概叙: Java 中是如此定义 Stri ...

  6. 向已有的table中插入数据

    table: <table id="seleted-table" class="table table-bordered table-hover" sty ...

  7. Mysql日期类型大小比较---拉取给定时间段的记录

    我们知道,mysql里边,日期类型有很多表现形式,date, datetime,timestamp等类型.考虑这样一种场景: 按时间段拉取给定时间段的内容,这时,我们就得使用日期类型的比较了. 表结构 ...

  8. 如何处理服务器SSL收到了一个弱临时Diffie-Hellman 密钥?

    当我们用火狐浏览器打开某个HTTPS网站时可能会失败,并且出现如下错误提示:         安全连接失败连接某个URL网址时发生错误. 在服务器密钥交换握手信息中 SSL 收到了一个弱临时 Diff ...

  9. eclipse下JAVA的搭建

    练手JAVA用eclipse比android studio快很多,android studio啥都好,就是太慢 参考资料:http://blog.csdn.net/21aspnet/article/d ...

  10. GroupBox 分组框控件

    GroupBox 控件是由System.Windows.Forms.GroupBox类提供的,作用是为其他控件提供可识别的分组.   可在同一页面,实现多个单选的RadioButton   通常,使用 ...