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

package myprac.LeetCode;

import java.util.ArrayList;
import java.util.List;

public class StringPermutation {
   /* public static ArrayList<String> Permutation(String str) {
        ArrayList<String> list = new ArrayList<>();
        if(str==null || str.length()==0){
            return list;
        }
        char[] chars = str.toCharArray();
        sort(list,0,chars);
        Collections.sort(list);
        return list;
    }
    private static void sort(ArrayList<String> list, int i, char[] chars) {
        if(i==chars.length-1){//i=2执行
            list.add(new String(chars));
        }
        for(int index = i;i<chars.length;i++){
            if (index==i){
                sort(list, index+1, chars);
            }else if(chars[i]!=chars[index]){
                swap(chars,i,index);
                sort(list, index+1, chars);
                swap(chars,i,index);
            }
        }
    }
    private static void swap(char[] chars, int i, int index) {
        char c=chars[i];
        chars[i]=chars[index];
        chars[index] = c;
    }
    public static void main(String[] args) {
        String s = "aba";
        System.out.println(Permutation(s));

    }*/
   public static List<List<Character>> permute(String str) {

       List<List<Character>> list = new ArrayList<>();

       backtrack(list, new ArrayList<Character>(), str.toCharArray());
       return list;
   }

    private static void backtrack(List<List<Character>> list, List<Character> tempList, char[] chars){

        if(tempList.size() == chars.length){

            list.add(new ArrayList<>(tempList));
        } else{
            for(int i = 0; i < chars.length; i++){
                if(tempList.contains(chars[i])){
                    continue;
                }

                tempList.add(chars[i]);

                backtrack(list, tempList, chars);

                tempList.remove(tempList.size() - 1);
            }
        }
    }

    public static void main(String[] args){
       String string = "abc";
        System.out.println(permute(string));
    }
}

原文地址:https://www.cnblogs.com/q-1993/p/10864393.html

时间: 2024-12-08 11:57:54

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

【C语言】输入一个整数,输出该数二进制表示中1的个数(三种方法)

输入一个整数,输出该数二进制表示中1的个数.如输入32,输出1. 代码实现: 方法1:与运算 #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; int FindOneNumber(unsigned int num) {     int numberofOne = 0;     while (num)     {         num = num & (num - 1);         

22、输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 思路: n &(n-1)把n的最右边的1去掉,用count++计算1的个数  eg: 101 & 100 = 100   1 class Solution { 2 public: 3 int NumberOf1(int n) { 4 int count = 0; 5             while(n!=0){ 6                 count++; 7                 n = n&

剑指offer11:输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。(进制转换,补码反码)

1. 题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 2. 思路和方法 使用移位(<<)和 “| & !”操作来实现.1的二进制是:前面都是0,最后一位为1.每次向左移位一下,使得flag的二进制表示中始终只有一个位为1,每次与n做位与操作,这样就相当于逐个检测n的每一位是否是1.unsigned int flag = 1; 3. C++核心代码 3.1 位运算 1 class Solution { 2 public: 3 int NumberOf1(int

三种方式求: 输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示

package com.example; public class Solution { /* * 转化成2进制数计算 */ public int NumberOf1(int n) { String string = Integer.toBinaryString(n); int count = 0; for (int i = 0;i < string.length();i++) { if (string.charAt(i) == '1') { count++; } } return count;

输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

public class Solution { public int NumberOf1(int n) { int index = 1; int number = 0; while(index!=0){ if((n & index)!=0) number++; index = index << 1; } return number; } } 先上第一种 两种方法.第一种,是用位运算,将1每次左移,和数字进行&运算,如果成功,则返回1.   第二种,将整数通过方法转换为二进制数,

输入一个字符串,打印出该字符串的所有排列

目录 输入一个字符串,打印出该字符串的所有排列. 题解 输入一个字符串,打印出该字符串的所有排列. 例如输入字符串abc,则输出由字符a.b.c所能排列出来的所有字符串 abc,acb,bac,bca,cab,cba. 题解 了解下排列的数学知识: 排列的定义:从n个不同元素中,任取m(m≤n,m与n均为自然数,下同)个元素按照一定的顺序排成一列,叫做从n个不同元素中取出m个元素的一个排列:从n个不同元素中取出m(m≤n)个元素的所有排列的个数,叫做从n个不同元素中取出m个元素的排列数,用符号

Python3基础 if else 格式 输入一个整数并判断是8吗

镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.------------------------------------------ code: 1 temp=input('请输入一个数字:') 2 guess=int(temp) 3 4 if guess==8: 5 print('输入的数字是8') 6 else: 7 print('输入的数字不是8') result: step1: 1 ============= R

【20190405】算法-输入一个字符串,按字典序打印出该字符串中字符的所有排列

方法一:利用递归 利用递归求全排列的过程真的很难理解,先把代码贴上来吧 function Permutation(str) { // write code here if(!str){ return str; } let len=str.length, result=[]; str=str.split('');// 字符串的索引属性都是只读,后面要交换两个字符的话还是要先把字符串转化为数组 str.sort();// 直接把字符串按字典序排列了,后面求全排列时也会按照字典序 Permutate(

输入一个维度,逆时针打印出一个指定的矩阵

题目:用户给定一个维度,打印出指定的一个矩阵,例如用户给定10,输出应该如下图所示: 程序如下: #include <stdio.h> #include <malloc.h> int main() { int dimension; int *p; int startx, starty, endx, endy; int i, j, value = 0; printf("Please input dimension:"); scanf("%d",