python剑指offer面试题_剑指Offer(Python语言)面试题38
生活随笔
收集整理的這篇文章主要介紹了
python剑指offer面试题_剑指Offer(Python语言)面试题38
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
面試題38:字符串的排列
題目:輸入一個字符串,打印出該字符串中字符的所有排列。例如,輸入字符串abc,則打印出由字符a,b,c所能排列出來的所有字符串abc,acb,bac,bca和cba。
# -*- coding:utf-8 -*-
class Solution:
def Permutation(self, ss):
# write code here
ss = list(ss)
result = []
if not ss:
return []
self.permutation(ss,0,result)
result = sorted(result)
return result
def permutation(self, ss, begin, result):
if begin == len(ss)-1:
result.append("".join(ss))
for i in range(begin, len(ss)):
if i!=begin and ss[i]==ss[begin]:
continue
ss[begin],ss[i] = ss[i], ss[begin]
self.permutation(ss,begin+1,result)
ss[begin],ss[i] = ss[i], ss[begin]
總結
以上是生活随笔為你收集整理的python剑指offer面试题_剑指Offer(Python语言)面试题38的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python位置参数ppt_如何在Pyt
- 下一篇: python batch_size_py