题目:请实现一个函数,把字符串中的每个空格替换成"%20"。例如输入"We are happy.",则输出"We%20are%20happy."。

c } l a s s f } u S n o c l g } v f } l v v f } r u r u a o e a a o e t e a r r t r r r t i p r u o l d r n c i } l l l i } } r n a e u f e i i _ f e n c s t l i n s s , l { e . u l n c t t c c s S ( c r N n = I ) l l l l l l e l l t _ o n u s = u = n = i i i i i i i i r u m = l s d i = s s s s s s { s s i s n s { l . A e n t t t t t t t t n : t = " N c r x " [ I [ I [ I [ I g u o r s l n l n l n l n ( S > 0 " m u a = . " i d i d i d i d l t n y e s e s e s e s e i r 0 { + t < l n { t x t x t x t x s i = C e u I I I I t n e + h n m n - n - n - n - ) g l 1 a e d = d = d = d = ) s n r - r e e e e e u a a x 1 x 1 x 1 x 1 l c 1 t ] ] ] ] { l t e N e d = = = = S u r ( t m > ) " " " c r ( . 0 2 % i r r " " " n e e g 2 p v e e { a r t s i e n d g ( : ) " { x " , c o u n t : l e n )

解题思路:如果从前往后处理,则空格后面所有的字符都后移若干位。所以从后向前处理会有更优解。这里直接新建了一个数组模拟操作的。

github地址:https://github.com/cubegao/LeetCode