LeetCode Reverse Words in a String III
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description
题目:
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.
题解:
遇到空格就把之前标记的部分reverse.
Note: 最后一次reverse别忘了.
Time Complexity: O(n), n = s.length().
Space: O(n), char array.
AC Java:
class Solution {
public String reverseWords(String s) {
if(s == null || s.length() == 0){
return s;
} int len = s.length();
char [] charArr = s.toCharArray(); int lo = 0;
for(int i = 0; i<=len; i++){
if(i==len || charArr[i]==' '){
reverse(charArr, lo, i-1);
lo = i+1;
}
} return new String(charArr);
} 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;
}
}
LeetCode Reverse Words in a String III的更多相关文章
- [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#557. Reverse Words in a String III(反转字符串中的单词 III)
题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输 ...
- 【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 、151. Reverse Words in a String
557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...
- [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 II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- 53. leetcode557. Reverse Words in a String III
557. Reverse Words in a String III Given a string, you need to reverse the order of characters in ea ...
- ledecode Reverse Words in a String III
557. Reverse Words in a String III Given a string, you need to reverse the order of characters in ea ...
- 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 ...
随机推荐
- 安装mysql到ubuntu
Ubuntu 16.04上安装MySQL步骤: 如果你使用的是Ubuntu 16.04以前的版本,可以看这里:Ubuntu 14.04/15.10升级到Ubuntu 16.04 LTS.一. 安装My ...
- 吐槽 坑爹的MySQL安装路径选择
一般再windows下面安装MySQL我们都会选择msi安装模式,然而安装最新版的MySQL(mysql-installer-community-5.7.11.0.msi 下载地址)发现MySQL默认 ...
- javascript重置(base层)(。。。。不完整)
1.nextSibling浏览器兼容问题 <ul> <li id="item1"></li> <li id="item2&quo ...
- R基本画图
参考内容:闻博,R语言的绘图功能及应用案例 https://wenku.baidu.com/view/80f22fa50029bd64783e2c22.html R画图是以函数操作为基本的画图模式. ...
- accept= 'image/*'反映缓慢
input[type='file']的accept属性用来指定上传文件的MIME类型. 将其设为accept= 'image/*',顾名思义,过滤掉所有非图片文件, 但在实际操作中,发现有时会出现响应 ...
- React Native的导入导出
1.组件的导入导出方式 问1:如何导出一个组件? export default class EIComponent extends Component{ render(){ return( <T ...
- unity json解析IPA后续
以前说到的,有很大的限制,只能解析简单的类,如果复杂的就会有问题,从老外哪里看到一片博客,是将类中的list 等复杂对象序列化, using UnityEngine; using System.C ...
- Java List 增删改查
定义2个类,课程类和选课类 package com.imooc.collection; /** * 课程类 */ public class Course { private String id; pr ...
- Kubernetes Controller Manager
Controller Manager 作为集群内部的管理控制中心,负责集群内的Node.Pod副本.Service Endpoint.NameSpace.ServiceAccount.Resource ...
- CCNA 课程 三
交换机的MAC地址学习情况: 1.从一个接口收到数据帧,根据数据帧的原mac地址查找交换机的mac地址表,如果没有找到,将会添加数据帧的原mac地址和收到数据帧接口的对应条目,放进交换机的mac地址表 ...