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, we do not need to do anything.

* *//*Try to do it in place.这是重要的不让用字典的提示
 1 public String deDup(String input) {
 2         // Write your solution here
 3         if (input == null || input.length() == 0){
 4             return input ;
 5         }
 6         char[] inputs = input.toCharArray() ;
 7         char[] res = new char[input.length()] ;
 8         //valid area is [0, slow]
 9         int slow = 0, fast = 0;
10         while (fast < inputs.length){
11             if (inputs[fast] != inputs[slow]) {
12                 /*  f f
13                     a b c
14                     s
15                     这里我们要先移动SLOW 然后赋予新值,否则原先的 A 就会被覆盖掉
16                 * */
17                 inputs[++slow] = inputs[fast++];
18             } else{
19                 fast++ ;
20             }
21         }
22         //special notice here, slow+1 参考上面的分析,每次SLOW 都是要包括的真实index!!! 所以长度是要SLOW+1
23         return new String(res, 0, slow+1) ;
24     }

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

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

LF.79.Remove Adjacent Repeated Characters I的相关文章

[LC] 82. Remove Adjacent Repeated Characters IV

Repeatedly remove all adjacent, repeated characters in a given string from left to right. No adjacent characters should be identified in the final string. Examples "abbbaaccz" → "aaaccz" → "ccz" → "z" "aabccdc&

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) {

[Leetcode 3, Medium] Longest Substring Without Repeating Characters

Problem: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the long

LeetCode 1156. Swap For Longest Repeated Character Substring

原题链接在这里:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/ 题目: Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters. Example 1: In

Linux命令学习总结:dos2unix - unix2dos

命令简介: dos2unix是将Windows格式文件转换为Unix.Linux格式的实用命令.Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n. dos2unix命令其实就是将文件中的\r\n 转换为\n. 而unix2dos则是和dos2unix互为孪生的一个命令,它是将Linux&Unix格式文件转换为Windows格式文件的命令. 命令语法: dos2unix [options] [-c convmode] [-o file ...] [-n i

(转)Linux命令学习总结:dos2unix - unix2dos

Linux命令学习总结:dos2unix - unix2dos 命令简介: 原文:http://www.cnblogs.com/kerrycode/p/5077969.html dos2unix是将Windows格式文件转换为Unix.Linux格式的实用命令.Windows格式文件的换行符为\r\n ,而Unix&Linux文件的换行符为\n. dos2unix命令其实就是将文件中的\r\n 转换为\n. 而unix2dos则是和dos2unix互为孪生的一个命令,它是将Linux&Un

python之路第二篇

python语言介绍 编译型和解释型 静态语言和动态语言 强类型定义语言和弱类型语言 python数据类型介绍 python数据类型分:数字.布尔型.字符串.列表.元组.字典 1.整数 例如:1,2,33,44等 整数的功能如下: 1 class int(object): 2 """ 3 int(x=0) -> int or long 4 int(x, base=10) -> int or long 5 6 Convert a number or string t

PHP 一个可以过滤非法脚本的函数

这里提供一个过滤非法脚本的函数: function RemoveXSS($val) { // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed // this prevents some character re-spacing such as <java\0script> // note that you have to handle splits with \n, \r, and \t

ThinkPHP/Common/extend.php

<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2010 http://thinkphp.cn All ri