zoukankan      html  css  js  c++  java
  • 字符串的排序 --剑指offer

    题目描述

    输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。

    输入描述:

    输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
    递归方法:代码有点难理解 就记下了 有机会再来
    具体就是全排序方法
    import java.util.ArrayList;
    import java.util.*;
    public class Solution {
        public ArrayList<String> Permutation(String str) {
            ArrayList<String> list=new ArrayList<>();
            if (str!= null && str.length() > 0){
                PermutationHelper(str.toCharArray(),0,list);
                Collections.sort(list);
            }
            
            return  list;
        }
    
        public void PermutationHelper(char[] chars, int i, ArrayList<String> list) {
            if(i == chars.length -1){
                String str=String.valueOf(chars);
                if(!list.contains(str)){
                    list.add(str);
                }
            }else {
                    for(int j =i;j < chars.length;j ++){
                        swap(chars,i,j);
                        PermutationHelper(chars,i+1,list);
                        swap(chars,i,j);
                    }
                }
            }
    
        public void swap(char[] chars, int i, int j) {
            char tem=chars[i];
            chars[i]=chars[j];
            chars[j] = tem;
        }
    }
  • 相关阅读:
    0593. Valid Square (M)
    0832. Flipping an Image (E)
    1026. Maximum Difference Between Node and Ancestor (M)
    0563. Binary Tree Tilt (E)
    0445. Add Two Numbers II (M)
    1283. Find the Smallest Divisor Given a Threshold (M)
    C Primer Plus note9
    C Primer Plus note8
    C Primer Plus note7
    C Primer Plus note6
  • 原文地址:https://www.cnblogs.com/nlw-blog/p/12434526.html
Copyright © 2011-2022 走看看