[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. ...
随机推荐
- 18 12 07 MySQL 与python 的交互
---恢复内容开始--- python 中 关于SQL语句的查询 from pymysql import * # 由于只能用了一个MySQL 的包所以全部引进 def main(): # 创建Conn ...
- Iterator迭代器,获取集合元素
* Object next() :返回下一个元素 * boolean hasNext():判断时是否有元素可以获取 public static void main(String[] args ...
- nginx基础知识小结
配置文件讲解: #user nobody; #开启进程数 <= CPU数 worker_processes 1; #错误日志保存位置 #error_log logs/error.log; #er ...
- springcloud之Eureka上
0 环境 系统环境:win10 编辑器:IDEA 1 注册中心 Eureka是springcloud中的注册中心.原因: 当是单体应用 类似一条直线 随着项目越来越大 系统拆分 类似那个藕(模块间相互 ...
- 【Pytyon模块】logging模块-日志处理
一.日志相关概念 1.日志的作用 通过log的分析,可以方便用户了解系统或软件.应用的运行情况:如果你的应用log足够丰富,也可以分析以往用户的操作行为.类型喜好.地域分布或其他更多信息:如果一个应用 ...
- 白痴级教程,新手看过来,具详细实操文档 (word图片复制不过来,0202年了还有这样的不便利,下回研究一下,图片下次补)
一.环境配置(win10): 1.配置cmd的python环境为arcmap10.2 自带的python解释器(2.7.3)(自带arcpy库) 具体操作: 1我的电脑右击属性,打开 (选中path点 ...
- CodeForces-1076B Divisor Subtraction 找规律
题目链接:https://vjudge.net/problem/CodeForces-1076B 题意: 题目要求给定一个数,要求每次减去其最小素因数(既是素数又是其因数),问直到n=0需要做几次运算 ...
- Ubuntu16.04编译OpenCV3.4.7
原文:https://www.bearoom.xyz/2019/08/20/ubuntu16-04-make-opencv3-4-7/ 一.前言 因为之前作死乱搞系统,然后就把Ubuntu的系统搞垮了 ...
- 深入分析Java反射(四)-动态代理
动态代理的简介 Java动态代理机制的出现,使得Java开发人员不用手工编写代理类,只要简单地指定一组接口及委托类对象,便能动态地获得代理类.代理类会负责将所有的方法调用分派到委托对象上反射执行,在分 ...
- PHP语言编写的磁力搜索工具下载BT种子 支持transmission、qBittorrent
磁力搜索网站2020/01/12更新 https://www.cnblogs.com/cilisousuo/p/12099547.html PT种子.BT种子搜索功能 IYUU自动辅种工具,目前能对国 ...