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. Android群英传神兵利器读书笔记——第二章:版本控制神器——Git

    本人一直是徐医生的真爱粉,由于参加比赛耽误了8天,导致更新得有点慢,大家见谅 2.1 Git的前世今生 Git是什么 Git安装与配置 2.2 创建Git仓库 Git init Git clone 2 ...

  2. faster RCNN(keras版本)代码讲解(3)-训练流程详情

    转载:https://blog.csdn.net/u011311291/article/details/81121519 https://blog.csdn.net/qq_34564612/artic ...

  3. ..\OBJ\LED.axf: Error: L6218E: Undefined symbol EXTI_Init (referred from exti.o). 错误修改

    今天在移植野火的程序到元子的开发平台上时候,发现自己在中断初话中断函数的时候出现了:..\OBJ\LED.axf: Error: L6218E: Undefined symbol EXTI_Init ...

  4. vue form 验证

    vue 验证 <Form :model="formModel" label-position="center" :label-width="90 ...

  5. 洛谷 P1709 隐藏口令

    题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<=5,000,000),然后他把S顺时针绕成一个圈,每次取一个做开头字母并 ...

  6. mysql建表语句和数据类型

    1.创建表的完整语法 create table 表名( 字段名称 数据类型[(长度) 约束条件], 字段名称 数据类型[(长度) 约束条件] )   必须的:字段名 数据类型 表名 可选的:长度 约束 ...

  7. python期末考试复习

    期末考试复习 补修的python跟着大一一起学,考试肯定不会出难,于是就敲了一些代码,把他们放到博客上,来记录一下 代码都是一段一段的,且python代码不是很多,所以我都写到了一个文件里,作为练习 ...

  8. 干货 | 京东云托管Kubernetes集成镜像仓库并部署原生DashBoard

    在上一篇"Cloud Native 实操系列"文章中,我们为大家介绍了如何通过京东云原生容器实现Eureka的部署(

  9. (递归)P1036 选数

    #include<stdio.h>#include<math.h>int x[20],n,k,i; //判断是否质数 int isprime(int n){    for(i= ...

  10. 使用labelImg制作自己的数据集(VOC2007格式)用于Faster-RCNN训练

    https://blog.csdn.net/u011956147/article/details/53239325 https://blog.csdn.net/u011574296/article/d ...