剑指Offer_第2题_替换空格_Python3.6实现

版权声明:转载请联系作者 https://blog.csdn.net/sinat_21591675/article/details/84098802

剑指Offer(牛客网)_第二题_替换空格_Python3.6实现

题目描述

请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

**

约束条件

时间限制:1秒 空间限制:32768K 热度指数:739357
本题知识点: 字符串

代码

解法1:遍历字符串的每个元素,根据条件进行替换

1.1 借助list进行替换

在Python中,str是不可改变的,因此需要借助list进行赋值替换


# -*- coding: utf-8 -*-
# Python 3.6


class Solution:

	def replaceSpace(self, source_str, target):
		# 在Python中,str是不可改变的,因此需要借助list进行赋值替换
		list_str = list(source_str)
		t = len(list_str)

		for i in range(t):
			if list_str[i]==' ':
				list_str[i] = target

		# 利用''.join(list)函数,将list 转换为 str;引号中是字符之间的分割符,如“,”,“;”,“\t”等等
		replaced = ''.join(list_str)

		return replaced

# test case
if __name__=='__main__':
	ori = 'We Are Happy'
	target = '%20'

	a = Solution()
	t = a.replaceSpace(ori, target)
	print(t)

Python的join和split函数的用法,例子见文末。

1.2 创建一个新的字符串,将替换操作看成条件复制,遇到空格就复制target,否则复制原元素。

class Solution:

	def replaceSpace(self, source_str, target):
		
		t = len(source_str)
		new = ''  # new null string

		for i in range(t):
			if source_str[i] == ' ':
				new += target  # str 的 “+” 相当于 concatenate
			else:
				new += source_str[i]

		return new

# test case
if __name__=='__main__':

	ori = 'We Are Happy'
	target = '%20'

	a = Solution()
	t = a.replaceSpace(ori, target)
	print(t)

解法2:调用str自带的replace函数

replace(old str, new str) ,将old_str替换成new_str。

# 直接调用 str 的 replace 函数
class Solution:
    
    def replaceSpace(self, source_str, target):
    	replaced = source_str.replace(' ', target)
    		
    	return replaced

# test case
if __name__=='__main__':
	ori = 'We Are Happy'
	target = '%20'
	a = Solution()
	new = a.replaceSpace(source_str=ori, target=target)
	print(new)

附: join和split函数的用法

python join 和 split方法简单的说是:join用来连接字符串,split用来拆分字符串的。

join 将 容器对象 拆分并以指定的字符将列表内的元素(element)连接起来,返回字符串(注:容器对象内的元素须为字符类型);

split 以指定的字符将字符串分割为单个元素(字符类型)并加入list中,返回一个list。

join用法举例

#  ''.join()表示直接连接,不采取间隔符号
l = ['I','love','SYSU']
s = ''.join(l)
print(s)  # 输出 IloveSYSU

# 以‘ ’连接
l = ['I','love','SYSU']
s = ' '.join(l)
print(s) # 输出 I love SYSU

# 以‘...’连接
l = ['I','love','SYSU']
s = '...'.join(l)
print(s)  # 输出 I...love...SYSU

split用法举例

ori = 'We..Are..Happy'
new_sp1 = ori.split()
print(new_sp1)   # 输出 ['We..Are..Happy'],一个list

ori = 'We..Are..Happy'
new_sp1 = ori.split('..')    # 默认全部切分
print(new_sp1)  # 输出 ['We', 'Are', 'Happy']


# split可以指定以特定标识来进行切分,以及切分次数

# 以'_'进行切分.
# 由于源字符串不包含'_',所以相当于不切分,返回一个以源字符串为元素的 list
ori = 'We..Are..Happy'
new_sp1 = ori.split('_',-1)
print(new_sp1)  # 输出['We..Are..Happy']

# 切分0次,即不切分
ori = 'We..Are..Happy'
new_sp1 = ori.split('..',0)
print(new_sp1)  # 输出['We..Are..Happy']

# 切分1次
ori = 'We..Are..Happy'
new_sp1 = ori.split('..',1)  
print(new_sp1)  # 输出 ['We', 'Are..Happy']

# 切分2次,在这里等价于全部分割
ori = 'We..Are..Happy'
new_sp1 = ori.split('..',2)  
print(new_sp1)  # 输出 ['We', 'Are', 'Happy']

# 切分-1次, 默认全部切分
ori = 'We..Are..Happy'
new_sp1 = ori.split('..',-1)
print(new_sp1)  # 输出['We', 'Are', 'Happy']



参考教程:
[1] Python的join和split函数的用法
[2] 剑指offer_第2题_替换空格

猜你喜欢

转载自blog.csdn.net/sinat_21591675/article/details/84098802