LF.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”
 1 public String removeSpaces(String input) {
 2         // Write your solution here
 3         //corner case
 4         if (input.isEmpty()){
 5             return input ;
 6         }
 7         /*
 8         return [0, i) j is the counter
 9         * case 1: not space: input[i++] = input[j++]
10         * case 2: if space
11         *         2.1: leading space: skip
12         *         i = 0
13         *         2.2: duplicate space: keep the first space
14         *         j != j-1 : j一定是SPACE, J-1 如果不是的话,把当前的SPACE 给I 吃
15         *         2.3: trailing space : remove
16         *         j to the end, and i = "", i--
17         * */
18         int slow = 0, fast = 0;
19         char[] chars = input.toCharArray();
20         while (fast < chars.length){
21             if (chars[fast] !=‘ ‘ ){
22                 chars[slow++] = chars[fast++] ;
23             } else{
24                 //非开头, 中间没连续_ 末尾可能进去一个 _
25                 if (slow>0 && chars[fast] != chars[fast-1]){
26                     chars[slow++] = chars[fast++] ;
27                 } else{
28                     fast++ ;
29                 }
30             }
31         }
32         //post-processing: it is possible we still have one trailing ‘ ‘
33         if (slow>0 && chars[slow-1] == ‘ ‘){
34             return new String(chars, 0, slow-1) ;
35         }
36         return new String(chars, 0, slow) ;
37     }

原文地址:https://www.cnblogs.com/davidnyc/p/8721937.html

时间: 2024-10-08 19:26:15

LF.281.Remove Spaces的相关文章

LF.79.Remove Adjacent Repeated Characters I

Remove adjacent, repeated characters in a given string, leaving only one character for each group of such characters. Assumptions Try to do it in place.Examples "aaaabbbc" is transferred to "abc"Corner Cases If the given string is null

JAVA_build_ant_FixCRLF

Description Adjusts a text file to local conventions. The set of files to be adjusted can be refined with the includes, includesfile, excludes, excludesfile and defaultexcludes attributes. Patterns provided through the includes or includesfile attrib

亲手教会你如何用苹果ios应用的推送

1. 什么是推送通知 消息通知分本地通知和远程推送通知,是没有运行在前台的应用程序可以让它们的用户获得相关消息通知的方式.消息通知可能是一条消息,即将发生的日历事件,或远程服务器的新数据.当被操作系统显示时,本地通知和推送通知看起来一样.它们可以显示一个警告信息或在应用程序的图标上面显示一个徽标.它们也可以在警告窗或徽标显示时播放一段声音.推送通知是在 iOS 3.0 和 Mac OS X v7.0 之后引入的.本地通知是在 iOS 4.0 之后引入的.它们都不支持 Mac OS X,当用户被通

65. Valid Number

Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true Note: It is intended for the problem statement to be ambiguous. You

手把手教你配置苹果APNS推送服务|钿畑的博客 | 钿畑的博客

http://www.360doc.com/content/15/0118/17/1073512_441822850.shtml# 钿畑的文章索引 1. 什么是推送通知 2. 什么是APNS? 3. 推送流程 3.1 获取设备device_token阶段 3.2 消息推送过程 3.3 完整流程介绍 4. Push机制类型 5. 正式开工 5.1 准备工作 5.2 证书生成 6. 客户端制作 7. php服务器端配置 8. 测试 8. 附录: 8.1 JSON示例 8.2 检验证书是否正确的方法:

125. Valid Palindrome

https://leetcode.com/problems/valid-palindrome/#/description Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a

Scala method call syntax

There are two standard ways of calling methods: obj.method(params) // dot notation obj method (params) // operator notation The above can be modified in the following ways: If params is a single parameter, you can replace () with {}. If params is a s

Google C++ 风格指南内容整理

之前一直没有全面的看过Google C++风格指南,现在很多公司进行C++开发都要求按照Google C++风格.在这个网站 http://zh-google-styleguide.readthedocs.org/en/latest/contents/  有人已经把其翻译成中文.为了便于以后查看,下面的内容完全是来自于这个网站,只是把多个网页的内容整理放在了一起. 1.      头文件: 通常每一个.cc文件都有一个对应的.h文件.也有一些常见例外,如单元测试代码和只包含main()函数的.c

Google C++ 代码规范

Google C++ Style Guide Table of Contents Header Files Self-contained Headers The #define Guard Forward Declarations Inline Functions Names and Order of Includes Scoping Namespaces Unnamed Namespaces and Static Variables Nonmember, Static Member, and