[Algo] 281. Remove Spaces
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的更多相关文章
- How to remove spaces of a string with regular expression
left 's/^\s*//g' right 's/\s*$//g' all 's/\s+//g'
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- [WPF系列]-DataBinding 绑定计算表达式
Width="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth, Converter={Stat ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- N-Gram
N-Gram是大词汇连续语音识别中常用的一种语言模型,对中文而言,我们称之为汉语语言模型(CLM, Chinese Language Model). 中文名 汉语语言模型 外文名 N-Gram 定 ...
- PIC24FJ64GB002 with bluetooth USB dongle
PIC24FJ64GB002 with bluetooth USB dongle I will explain my project (how to control a bluetooth USB d ...
- Codeigniter 集成sphinx搜索 这里采用的是coreseek中文搜索引擎,具体安装请参考官方网站
先上效果图 加入sphinx类库(/application/libraries/sphinx_client.php) 0001 <?php 0002 0003 // 0004 // $Id: s ...
- Oracle Database 11g express edition
commands : show sys connect sys as sysdba or connect system as sysdba logout or disc clear screen or ...
- php 连接测试sphinx
shpinx.php <?php header("Content-type:text/html;charset=utf-8"); include 'SphinxClient. ...
随机推荐
- Day 11:静态导入、增强for循环、可变参数的自动装箱与拆箱
jdk1.5新特性-------静态导入 静态导入的作用: 简化书写. 静态导入可以作用一个类的所有静态成员. 静态导入的格式:import static 包名.类名.静态的成员: 静态导入要注意的 ...
- opencv3。4安装出错
https://www.samontab.com/web/2017/06/installing-opencv-3-2-0-with-contrib-modules-in-ubuntu-16-04-lt ...
- oracle (6)---SQL 数据关联查询
SQL 数据关联查询 Structure Query Language 从多(n)张表查询对应记录信息,必须有至少n-1个关联条件,否则会出现笛卡尔积的情况.1. 等值连接:没有连接关系的数据不会被查 ...
- 19 01 15 django 数据库设计模型 管理站点 注意:在引入外键在django 2以上改版
模型设计 我们之前操作数据库是通过写sql语句 ORM框架 可以通过不写sql 语句来进行操作数据库 1.定义模型类 模型类定义在models.py文件中,继承自models.Model类. ...
- HashMap面试总结
作者:孤独烟 出处: http://rjzheng.cnblogs.com/ 文章由点及线再及面,写的非常好.修改部分内容 (1) HashMap的实现原理 看过HashMap源码吗,知道原理吗? h ...
- 文献阅读报告 - Pedestrian Trajectory Prediction With Learning-based Approaches A Comparative Study
概述 本文献是一篇文献综述,以自动驾驶载具对外围物体行动轨迹的预测为切入点,介绍了基于运动学(kinematics-based)和基于机器学习(learning-based)的两大类预测方法. 并选择 ...
- 吴裕雄--天生自然TensorFlow2教程:Broadcasting
Broadcasting可以理解成把维度分成大维度和小维度,小维度较为具体,大维度更加抽象.也就是小维度针对某个示例,然后让这个示例通用语大维度. import tensorflow as tf x ...
- int preg_match( string pattern
preg_match -- 进行正则表达式匹配.并且只匹配一次,注意与preg_match_all区别. int preg_match( string pattern, string subject ...
- C++如何输入含空格的字符串
1.scanf函数(包含头文件#include <stdio.h>) scanf函数一般格式为scanf(“%s”,st),但scanf默认回车和空格是输入不同组之间的间隔和结束符号,所以 ...
- Insulator|enhancer|LCR|EKLF|CTCF|调控基因印记| A-USF|HATs|ChIP|Chip-seq|PAGE|
表观遗传学 转录因子 基本转录因子:TFIID.A.B.F.E.H. Pol II… 基转录因子具有稳定作用 组织特异性转录因子:GATA.EKLF.Bcl11A… 特异性是在特定组织中的细胞中时与细 ...