原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/

题目:

Given an input string , reverse the string word by word.

Example:

Input:  ["t","h","e"," ","s","k","y"," ","i","s"," ","b","l","u","e"]
Output: ["b","l","u","e"," ","i","s"," ","s","k","y"," ","t","h","e"]

Note:

  • A word is defined as a sequence of non-space characters.
  • The input string does not contain leading or trailing spaces.
  • The words are always separated by a single space.

Follow up: Could you do it in-place without allocating extra space?

题解:

input变成 char array.

先reverse 全部 char array. 从头往后走,遇到空格或者array尾部,就reverse中间的单词.

Note: Do NOT forget i == n, reverse.

Time Compelxity: O(s.length()). Space: O(1).

AC Java:

 public class Solution {
public void reverseWords(char[] s) {
if(s == null || s.length == 0){
return;
}
reverse(s, 0, s.length-1); int lo = 0;
for(int i = 1; i<=s.length; i++){
if(i == s.length || s[i] == ' '){
reverse(s, lo, i-1);
lo = i+1;
}
}
} private void reverse(char [] s, int i, int j){
while(i < j){
swap(s, i++, j--);
}
} private void swap(char [] s, int i , int j){
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}

类似Reverse Words in a String.

跟上Reverse Words in a String IIIRotate Array.

LeetCode Reverse Words in a String II的更多相关文章

  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 Vowels of a String 翻转字符串中的元音字母

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  3. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  4. [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 ...

  5. 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 ...

  6. [LeetCode] 186. Reverse Words in a String II 翻转字符串中的单词 II

    Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...

  7. Reverse Words in a String I & Reverse Words in a String II

    Reverse Words in a String I Given an input string, reverse the string word by word. For example,Give ...

  8. LeetCode Reverse Words in a String III

    原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string ...

  9. [Swift]LeetCode186. 翻转字符串中的单词 II $ 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 ...

随机推荐

  1. MySQL设置当前时间为默认值的方法

    方法一.是用alert table语句: 复制代码代码如下: use test_db1; create table test_ta1( id mediumint(8) unsigned not nul ...

  2. JS保留小数点(四舍五入、四舍六入)实例

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  3. No configuration found for the specified action解决办法

    http://blog.csdn.net/carefree31441/article/details/4857546 使用Struts2,配置一切正常,使用常用tag也正常,但是在使用<s:fo ...

  4. Codeforces Round #244 (Div. 2) A. Police Recruits

    题目的意思就是找出未能及时处理的犯罪数, #include <iostream> using namespace std; int main(){ int n; cin >> ...

  5. ACM n-1位数

    n-1位数 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的 ...

  6. AJAX 跨域请求 - JSONP获取JSON数据

    Asynchronous JavaScript and XML (Ajax ) 是驱动新一代 Web 站点(流行术语为 Web 2.0 站点)的关键技术.Ajax 允许在不干扰 Web 应用程序的显示 ...

  7. 使用command对象操作数据库

    1.Command对象查询数据库 protected void Button1_Click(object sender, EventArgs e) { //读取web.config节点配置 strin ...

  8. springboot 添加job定时任务

    @SpringBootApplication@ComponentScan("com.xx")@EnableScheduling //定时任务扫描 此处用该注解,容器启动自动扫描pu ...

  9. github提交失败并报错java.io.IOException: Authentication failed:

    一.概述 我最近在写一个android的项目. 软件:android studio.Android studio VCS integration(插件) Android studio VCS inte ...

  10. [LintCode] Decode Ways 解码方法

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...