Given a string, remove all leading/trailing/duplicated empty spaces.

Assumptions:

  • The given string is not null.

Examples:

  • “  a” --> “a”
  • “   I     love MTV ” --> “I love MTV”

Solution 1:

public class Solution {
public String removeSpaces(String input) {
// Write your solution here
int i = 0;
int slow = 0;
int count = 0;
int len = input.length();
char[] charArr = input.toCharArray();
while(true) {
while (i < len && charArr[i] == ' ') {
i += 1;
}
if (i == len) {
break;
}
if (count > 0) {
charArr[slow++] = ' ';
} while (i < len && charArr[i] != ' ') {
charArr[slow++] = charArr[i++];
}
count += 1;
}
return new String(charArr, 0, slow);
}
}

Solution 2:

public class Solution {
public String removeSpaces(String input) {
// Write your solution here
int slow = 0;
char[] charArr = input.toCharArray();
for (int i = 0; i < charArr.length; i++) {
if (charArr[i] == ' ' && (i == 0 || charArr[i - 1] == ' ')) {
continue;
}
charArr[slow++] = charArr[i];
}
if (slow > 0 && charArr[slow - 1] == ' ') {
return new String(charArr, 0, slow - 1);
}
return new String(charArr, 0, slow);
}
}

[Algo] 281. Remove Spaces的更多相关文章

  1. How to remove spaces of a string with regular expression

    left 's/^\s*//g' right 's/\s*$//g' all 's/\s+//g'

  2. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  3. [WPF系列]-DataBinding 绑定计算表达式

            Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth, Converter={Stat ...

  4. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  5. N-Gram

    N-Gram是大词汇连续语音识别中常用的一种语言模型,对中文而言,我们称之为汉语语言模型(CLM, Chinese Language Model).   中文名 汉语语言模型 外文名 N-Gram 定 ...

  6. PIC24FJ64GB002 with bluetooth USB dongle

    PIC24FJ64GB002 with bluetooth USB dongle I will explain my project (how to control a bluetooth USB d ...

  7. Codeigniter 集成sphinx搜索 这里采用的是coreseek中文搜索引擎,具体安装请参考官方网站

    先上效果图 加入sphinx类库(/application/libraries/sphinx_client.php) 0001 <?php 0002 0003 // 0004 // $Id: s ...

  8. Oracle Database 11g express edition

    commands : show sys connect sys as sysdba or connect system as sysdba logout or disc clear screen or ...

  9. php 连接测试sphinx

    shpinx.php <?php header("Content-type:text/html;charset=utf-8"); include 'SphinxClient. ...

随机推荐

  1. mysql第四篇--SQL逻辑查询语句执行顺序

    mysql第四篇--SQL逻辑查询语句执行顺序 一.SQL语句定义顺序 SELECT DISTINCT <select_list> FROM <left_table> < ...

  2. Nginx系列p5:进程管理(信号)

    通过上图我们可以看到:信号与命令行的关系,下面我们来简单总结一下上述命令的作用: CHLD: 当子进程终止的时候,会向父进程发送 CHLD 信号,这样,如果子进程由于某些模块出现了 Bug,导致子进程 ...

  3. maven解决大项目打包慢的问题

    裁剪反应堆 -am, --also-make 同时构建所列模块的依赖模块.必须和-pl同时使用.如 mvn -pl test  install -am ,将同时构建test的依赖模块. -amd, - ...

  4. [Java-基础] 注解

    引言 在进行Spring Boot 入门的时候,主程序需要@SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用.这个的作用是什么?去掉的话会报错.如 ...

  5. 2019杭电暑假多校训练 第六场 Snowy Smile HDU - 6638

    很多题解都是简单带过,所以打算自己写一篇,顺便也加深自己理解 前置知识:线段树.线段树维护最大字段和.二维坐标离散化 题解: 1.很容易想到我们需要枚举所有子矩阵来得到一个最大子矩阵,所以我们的任务是 ...

  6. 到头来还是逃不过Java - 流程控制

    流程控制 没有特殊说明,我的所有这个系列的Java13的笔记都是从廖老师那里摘抄.总结过来的,侵删 引言 兜兜转转到了大四,学过了C,C++,C#,Java,Python,学一门丢一门,到了最后还是要 ...

  7. jquery时钟

    <script type="text/javascript"> function getDate(){ var mydate = new Date(); //时间对象 ...

  8. Python3中bytes和HexStr之间的转换

    1 Python3中bytes和HexStr之间的转换 ByteToHex的转换 def ByteToHex( bins ): """ Convert a byte st ...

  9. target到底是什么?

    xmake是一个基于Lua的轻量级现代化c/c++的项目构建工具,主要特点是:语法简单易上手,提供更加可读的项目维护,实现跨平台行为一致的构建体验. 本文主要详细讲解下,如果在一个项目中维护和生成多个 ...

  10. 使用git submodule

    git submodule 引用 $ git help submodule $ git submodule add https://github.com/aditya-grover/node2vec. ...