In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Many beginners are not very clear about the arrangement of strings. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
Enter a string and print out all the characters in the string in lexicographical order. For example, if you enter the string abc, you will print out all the strings abc,acb,bac,bca,cab and cba that can be arranged by the character aline bdiary c.
#-*-coding: utf-8-*-# @ Time: 2019-07-08 9 utf-8 @ Author: Jayce Wong# @ ProjectName: job# @ FileName: stringPermutation.py# @ Blog: https://blog.51cto.com/jayce1111# @ Github: https://github.com/SysuJayceclass Solution: "" if you encounter the problem of this arrangement, you can solve it by dividing and conquering. The string to be arranged (n bits) is considered to be composed of two parts, the first part contains the first character (1), the second part contains the remaining characters (nMel 1), then the first part is fixed, and the second part is further arranged. This is the beginning of recursion. The core of recursion here is to select one character at a time as the first part and then the remaining characters as the second part. The recursive exit is: the second part contains 0 characters, that is, all the characters of the string have been arranged. "" def Permutation (self, ss): "" fully arranges a given string: param ss: with permutation string: return: a list containing all possible permutations Where the order of the elements is in accordance with the dictionary order "" def helper (s, begin): # here the starting subscript of the second part is set to exceed the legal bound if begin > = len (s): ans.add (''.join (s)) else: # starts from a given starting point Swap all the following characters with the starting point in turn Then arrange (recursively) for idx in range (begin, len (s)): s [idx], s [begin] = s [begin], s [idx] helper (s) Begin + 1) # remember that strings should be restored to the pre-exchange order at the end of an exchange Otherwise, this loop cannot guarantee that all characters of # can exchange with the starting character s [idx], s [begin] = s [begin], s [idx] if not ss: return [] ans = set () helper (list (ss)) 0) return sorted (list (ans)) def main (): s = "abc" solution = Solution () ans = solution.Permutation (s) print (ans) if _ _ name__ = ='_ main__': main () is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.