剑指offer 第二题:替换空格

题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
思路1:申请辅助空间
思路2:在原数组上修改===》1.从前向后,每次遇到空格,后面的字符都要向后移动,复杂度很高O(n^2),不建议。2.从后向前,字符只需移动一次就能实现,复杂度O(n).

思路1class Solution {
public:
    void replaceSpace(char *str,int length) {
        if(str==NULL)
            return ;
        int old_length = 0;
        int space = 0;
        for (int i = 0; str[i] != '\0'; i++)
        {
            if (str[i] == ' ')
                space++;
            old_length++;
        }
        int new_length = old_length + 2 * space;
        char *str2= new char[new_length+1];
        int j = 0;
        for (int i = 0; i<=old_length; ++i)
        {
            if (str[i] == ' ') {
                *(str2 + j++) = '%';
                *(str2 + j++) = '2';
                *(str2 + j++) = '0';
            }
            else
                *(str2 + j++) = str[i];
        }
        strcpy(str,str2);
    }
};
思路2class Solution {
public:
    void replaceSpace(char *str,int length) {
        if(str == NULL)
            return;
        int str_length=0;
        int space_length=0;
        for(int i=0;str[i]!='\0';++i)
        {
            if(str[i]==' ')
                ++space_length;
            ++str_length;
        }
        if(length<=str_length+2*space_length)
            return;
        int new_length=str_length+2*space_length;
        for(int i=str_length;i>=0;--i)
        {
            if(str[i]==' ')
            {
                str[new_length--]='0';
                str[new_length--]='2';
                str[new_length--]='%';
            }
            else
                str[new_length--]=str[i];
        }
    }
};

python版

思路1# -*- coding:utf-8 -*-
class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        ss=''
        for i in s:
            if i==' ':
                ss=ss+'%20'
            else:
                ss+=i
        return ss
        # write code here
思路2# -*- coding:utf-8 -*-
class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        s=list(s)
        for i in range(len(s)):
            if s[i]==' ':
                s[i]='%20'
        return ''.join(s)
        # write code here

python 中join用法:
python中有join()和os.path.join()两个函数,具体作用如下:
join():连接字符串数组。将字符串、元组、列表中的元素以指定的字符(分隔符)连接生成一个新的字符串
os.path.join(): 根据系统将多个路径组合后返回

#对数组进行操作
>>> seq1 = ['hello','good','boy','doiido']
>>> print ' '.join(seq1)
hello good boy doiido

>>> print ':'.join(seq1)
hello:good:boy:doiido

#对字符串进行操作
>>> seq2 = "hello good boy doiido"
>>> print ':'.join(seq2)
h:e:l:l:o: :g:o:o:d: :b:o:y: :d:o:i:i:d:o

#对元组进行操作
>>> seq3 = ('hello','good','boy','doiido')
>>> print ':'.join(seq3)
hello:good:boy:doiido

#对字典进行操作
>>> seq4 = {'hello':1,'good':2,'boy':3,'doiido':4}
>>> print ':'.join(seq4)
boy:good:doiido:hello

#合并目录
>>> import os
>>> os.path.join('/hello/','good/boy','doiido')
linux:
'/hello/good/boy/doiido'
windows
'/hello/good/boy\doiido'
如果指定了,则不自动添加
os.path.join('/hello/','good/boy/','doiido')
'/hello/good/boy/doiido'

猜你喜欢

转载自blog.csdn.net/zd_nupt/article/details/80694661
今日推荐