LF.281.Remove Spaces

2023-10-20 04:30
文章标签 remove spaces lf.281

本文主要是介绍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

这篇关于LF.281.Remove Spaces的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/244667

相关文章

27. Remove Elements

题目: 解答: 类似题26,注意下删除后的元素的移动方式即可 代码: class Solution {public:int removeElement(vector<int>& nums, int val) {if(nums.empty()) return 0;int len = nums.size();int lenafter = 0, head = 0;for(int i

White spaces are required between publicId and systemId.

在配置applicationContext.xml时,编译器报错。 是因为XML的标签顺序导致。 <?xml version="1.0" encoding="UTF-8"?> <beans   xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema

[转载]python:remove方法的使用,remove、pop、del三者的区别

remove方法 描述 删除列表中的给定的对象 语法 list.remove() 参数 obj 参数(可选择性插入)obj的作用是要从列表中删除的对象的索引 使用如:list.remove(obj = list[0]) 返回值 remove方法删除后不会返回值 实例 list = [1, 2, 3, 4, 5]List1 = list.remove(1)print (li

Jquery empty() remove() detach() 方法的区别

引言: 最近项目中用到了这几个方法,感觉很大程度上有些相似,查了Jquery的api,同时也找了很多博客文章,发现还是理解不到区别。最后在很多材料和自己的事例验证中,终于找到了区别,不敢独占特拿出来分享。   方法简介:  empty() This method removes not only child (and other descendant)

详解PyTorch中的`remove_self_loops`函数及其在图神经网络中的应用

remove_self_loops 引言什么是自环?`remove_self_loops`函数简介函数的基本用法为什么要移除自环?`remove_self_loops`的实际应用 引言 在图神经网络(GNN)的研究与应用中,数据预处理是实现高效和精确模型的关键步骤之一。remove_self_loops函数在这一过程中扮演了重要角色。本文将深入探讨这一函数的定义、作用以及在

C#中List集合使用Remove方法详解——List使用Remove方法需要注意的坑?

目录 一、基本使用 1、简单类型的例子 2、复杂类型的例子 二、思考 三、深度解析 四、正确的使用方式 1、重写 Equals 和 GetHashCode 2、使用 LINQ 的 FirstOrDefault 方法 五、性能考虑 六、注意事项 总结         在C#中,List<T> 是一个常用的数据结构,它提供了一系列操作方法来管理其内部

remove_if详解

std::remove_if 函数是 C++ 标准库中的一个算法,它用于移除容器中满足特定条件的元素。然而,重要的是要理解 std::remove_if 的工作原理及其返回值,因为它并不真正地从容器中删除元素。 std::remove_if 接受三个参数: 指向容器中第一个元素的迭代器。指向容器中最后一个元素之后位置的迭代器(即尾后迭代器)。一个谓词(通常是一个函数或函数对象),该函数接受容器

skimage包的小优化(2):模仿remove_small_objects()函数保留图片中连通域最大的区域

python模仿remove_small_objects()函数保留图片中连通域最大的区域 skimage包的morphology子模块中,提供了一个remove_small_objects()函数,可以通过自己设定的连通域面积阈值有效去掉图片中的噪点,但是在具体使用过程中会发现:这个函数使用起来还有诸多的不便,好在这个函数的源代码并不长,在在skimage包的小优化(1):模仿remove_s

c:remove标签的使用

属性 var 要移除的变量 scope 变量所属的作用域 代码示例 <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><html>

【LeetCode】Remove Duplicates from Sorted List I II

I、每个数据只允许出现1次      Remove Duplicates from Sorted List       Total Accepted: 7120 Total Submissions: 20880 My Submissions      Given a sorted linked list, delete all duplicates such that each element a