如給定:"the sky is blue",python:返回: "blue is sky the".(多個空格變成乙個空格)
class solution:
def str(self, s):
if len(s) == 0:
return ''
temp =
i = 0
while i < len(s):
j = i
while not s[j].isspace():#沒有遇到單詞之間的空格時,一直往下+
j += 1
if j == len(s):#遇到末尾時也停止
break
if j != i:#如果不相等,說明存在新單詞劃分
i = j + 1 #第j位是空格,從j+1位開始繼續判斷
i = len(temp) - 1
s = ''
while i > 0:
s += temp[i]
s += ' '
i -= 1
s += temp[i]#最後末尾不需要空格
return s
s = ' the sky is blue '
sou = solution()
print(sou.str(s))
#blue is sky the
字串反轉,單詞反轉
一 字串反轉,共蒐集了 7 種方法 public class stringreversed public static void reverse1 string s char c s.tochararray 方法二 for int i 0 i s.length 2 i for char l c sy...
01 字串反轉
result s 1 li b c f 4,5,6 a list reversed li print a 輸出 6,5,4,f c b result reduce lambda x,y y x,s 不認識,先記著 def func s 此處使用遞迴,不斷把第乙個字元,通過拼接的方式加到分片字串後面,...
字串單詞反轉
class solution param s,a string return a string def reversewords self,s if len s 0 return s s join s.split 去掉所有的空格,只保留字元 串 之間的空格 s s.strip strip 去掉字串兩...