码迷,mamicode.com
首页 > 其他好文 > 详细

LF.79.Remove Adjacent Repeated Characters I

时间:2018-04-05 14:31:52      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:fast   cas   repeat   abc   length   ons   ==   覆盖   examples   

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     }

 

LF.79.Remove Adjacent Repeated Characters I

标签:fast   cas   repeat   abc   length   ons   ==   覆盖   examples   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!