Hoán vị của các số trong Java

Bài tập mảng Java. Tạo tất cả các hoán vị có thể có của một mảng các số nguyên riêng biệt đã choCập nhật lần cuối vào ngày 19 tháng 8 năm 2022 21. 50. 33 (UTC/GMT +8 giờ)

Mảng Java. Bài tập-68 có lời giải

Viết chương trình Java để tạo tất cả các hoán vị có thể có của một mảng các số nguyên riêng biệt đã cho

Thí dụ
Đầu vào
số1 = {1, 2, 3, 4}
số2 = {1, 2, 3}
đầu ra
Các hoán vị có thể có của mảng đã nói
[1, 2, 3, 4]
[1, 2, 4, 3]
[1, 3, 2, 4]
[1, 3, 4, 2]
....
[4, 3, 2, 1]
[4, 3, 1, 2]
[4, 1, 3, 2]
[4, 1, 2, 3]
Các hoán vị có thể có của mảng đã nói
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, 1, 2]

Giải pháp mẫu

Mã Java

import java.util.*;
import java.util.List;

 public class solution {
 public static void main(String[] args) throws Exception {
    int[] nums1 = {1, 2, 3, 4};
	System.out.println("\nOriginal array: "+Arrays.toString(nums1));
    List> result1 = new solution().permute(nums1);
	System.out.println("\nPossible permutations of the said array:");
	result1.forEach(System.out::println);
    int[] nums2 = {1, 2, 3};
	System.out.println("\nOriginal array: "+Arrays.toString(nums2));
    List> result2 = new solution().permute(nums2);
	System.out.println("\nPossible permutations of the said array:");
	result2.forEach(System.out::println);	
	  }

  public List> permute(int[] nums) {
    List> result = new ArrayList<>();
    Permutation(0, nums, result);
    return result;
  }

  private void Permutation(int i, int[] nums, List> result) {
    if (i == nums.length - 1) {
      List list = new ArrayList<>();
      for (int n : nums) list.add(n);
      result.add(list);
    } else {
      for (int j = i, l = nums.length; j < l; j++) {
        int temp = nums[j];
        nums[j] = nums[i];
        nums[i] = temp;
        Permutation(i + 1, nums, result);
        temp = nums[j];
        nums[j] = nums[i];
        nums[i] = temp;
      }
    }
  }
}

Đầu ra mẫu

Original array: [1, 2, 3, 4]

Possible permutations of the said array:
[1, 2, 3, 4]
[1, 2, 4, 3]
[1, 3, 2, 4]
[1, 3, 4, 2]
[1, 4, 3, 2]
[1, 4, 2, 3]
[2, 1, 3, 4]
[2, 1, 4, 3]
[2, 3, 1, 4]
[2, 3, 4, 1]
[2, 4, 3, 1]
[2, 4, 1, 3]
[3, 2, 1, 4]
[3, 2, 4, 1]
[3, 1, 2, 4]
[3, 1, 4, 2]
[3, 4, 1, 2]
[3, 4, 2, 1]
[4, 2, 3, 1]
[4, 2, 1, 3]
[4, 3, 2, 1]
[4, 3, 1, 2]
[4, 1, 3, 2]
[4, 1, 2, 3]

Original array: [1, 2, 3]

Possible permutations of the said array:
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, 1, 2]

Sơ đồ

Hoán vị của các số trong Java

Trình soạn thảo mã Java

Cải thiện giải pháp mẫu này và đăng mã của bạn qua Disqus

Trước. Viết chương trình Java để tìm mảng con có tổng lớn nhất trong một mảng các số nguyên tròn đã cho
Kế tiếp. Viết chương trình Java để tìm tổng mảng con tối thiểu có kích thước được chỉ định trong một mảng số nguyên đã cho

Mức độ khó của bài tập này là gì?

Dễ dàng trung bình khó

Kiểm tra kỹ năng Lập trình của bạn với bài kiểm tra của w3resource



Theo dõi chúng tôi trên FacebookTwitter để cập nhật thông tin mới nhất.

Java. Lời khuyên trong ngày

Java. Trung bình

Trả về trung bình cộng của hai hoặc nhiều số

public static double average(int[] arr) {
    return IntStream.of(arr)
            .average()
            .orElseThrow(() -> new IllegalArgumentException("Array is empty"));
}

Giới thiệu. https. //chút. ly/3aTwVB1

 



  • Xu hướng hàng tuần
  • Bài tập lập trình Java cơ bản
  • Truy vấn con SQL
  • Bài tập cơ sở dữ liệu Adventureworks
  • Bài tập cơ bản C# Sharp
  • SQL COUNT() với sự khác biệt
  • Bài tập chuỗi JavaScript
  • Xác thực biểu mẫu HTML JavaScript
  • Bài tập bộ sưu tập Java
  • hàm SQL COUNT()
  • Tham gia bên trong SQL
  • Hàm JavaScript Bài tập
  • Hướng dẫn Python
  • Bài tập mảng Python
  • Tham gia chéo SQL
  • Bài tập về mảng Sharp trong C#


Hoán vị còn được gọi là “số sắp xếp” hoặc “thứ tự”, là sự sắp xếp lại các phần tử của danh sách có thứ tự S thành một tương ứng một đối một với chính S. Xâu có độ dài n có n. hoán vị.  

Nguồn. từ toán học(http. //thế giới toán học. chó sói. com/Hoán vị. html)

Dưới đây là các hoán vị của chuỗi ABC.  
ABC ACB BAC BCA CBA CAB

Khuyến khích. Vui lòng giải quyết nó trên “PRACTICE” trước khi chuyển sang giải pháp

Đây là một giải pháp được sử dụng làm cơ sở trong quay lui

Hoán vị của các số trong Java

Java




// Java program to print all 

// permutations of a given string. 

public class Permutation 

    public

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
0
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
1
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
2

    

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
6
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
7
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
8

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5// Java program to print all 0 // Java program to print all 1

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5// Java program to print all 3

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5// Java program to print all 5 // Java program to print all 6

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5// Java program to print all 8// Java program to print all 9// permutations of a given string. 0// permutations of a given string. 1// permutations of a given string. 2

    // permutations of a given string. 4

// permutations of a given string. 5

    // permutations of a given string. 7

// permutations of a given string. 8// permutations of a given string. 9

// permutations of a given string. 8public1

// permutations of a given string. 8public3

    public5

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
1 public7

public8// Java program to print all 0 class0// Java program to print all 0 class2

    

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5class6 class7

class8class9

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5Permutation 1

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5

class8Permutation 5 Permutation 6// Java program to print all 0 Permutation 8

class8

12

14// permutations of a given string. 16

12

class8// permutations of a given string. 4

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5// permutations of a given string. 4

    // permutations of a given string. 4

// permutations of a given string. 5

________ 105 _______________ 105 _______ 7

// permutations of a given string. 8    9

// permutations of a given string. 8public1

// permutations of a given string. 8public3

    public public6

public7_______99_______0 public9// Java program to print all 0

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
01

    

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
05
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
06

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
05
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
09

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
11

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
13

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
15

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
17
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
18

    // permutations of a given string. 4

// permutations of a given string. 4

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
22

đầu ra.  

ABC
ACB
BAC
BCA
CBA
CAB

Mô hình thuật toán. quay lui

Thời gian phức tạp. O(n*n. ) Lưu ý rằng có n. hoán vị và cần O(n) thời gian để in một hoán vị

Không gian phụ trợ. O(r – l)

Ghi chú. Giải pháp trên in các hoán vị trùng lặp nếu có các ký tự lặp lại trong chuỗi đầu vào. Vui lòng xem liên kết bên dưới để biết giải pháp chỉ in các hoán vị riêng biệt ngay cả khi có các bản sao trong đầu vào
In tất cả các hoán vị riêng biệt của một chuỗi đã cho với các bản sao.  
Hoán vị của một chuỗi đã cho bằng STL

Cách tiếp cận khác

Java




Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
23
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
24

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
25

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
26

class

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
28

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
29

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
0
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
1
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
32

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
33
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
34

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
35

    class6

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
38_______99_______9
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
40

    

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
42

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
44
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
45
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
46

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
17
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
49

    

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
51

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
52

    Permutation 5_______103_______6// Java program to print all 0

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
57// Java program to print all 9
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
59

    

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
42

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
05
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
64

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
66// Java program to print all 9
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
68

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
70// permutations of a given string. 1
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
46

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
74

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
76

    

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
51

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
51

// permutations of a given string. 5

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
81

public

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
0
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
1
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
85

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
42

    

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
88_______99_______5
Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
90

    

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
92

    

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
94_______3_______95_______3_______49

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
52

    

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
99

    // Java program to print all 01_______3_______46

    // Java program to print all 04

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
52

    

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
99

    // Java program to print all 09_______3_______46

    // Java program to print all 12

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
51

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba
51

// Java program to print all 15

đầu ra

Enter the string : abc
All possible strings are : abc  acb  bac  bca  cab  cba

Thời gian phức tạp. O(n*n. ) Độ phức tạp về thời gian giống như cách tiếp cận trên, tôi. e. có n. hoán vị và cần O(n) thời gian để in một hoán vị

Làm cách nào để tính toán hoán vị trong Java?

Chúng ta sử dụng phương thức size() để lấy số phần tử trong danh sách. Chúng tôi đặt giá trị không đổi 3 thành r, i. e. , số lượng mục được thực hiện cho Hoán vị. Sau đó, chúng tôi sử dụng công thức hoán vị, i. e. , fact(n)/fact(n-r) và lưu kết quả vào biến kết quả. Cuối cùng, chúng tôi hiển thị kết quả cuối cùng cho người dùng.

Hoán vị trong Java với ví dụ là gì?

Hoán vị của chuỗi có nghĩa là tất cả các chuỗi mới có thể được hình thành bằng cách hoán đổi vị trí của các ký tự trong chuỗi . Ví dụ, xâu ABC có các hoán vị [ABC, ACB, BAC, BCA, CAB, CBA].

Hoán vị trong Java là gì?

Hoán vị là sự sắp xếp của tất cả hoặc một phần của một tập hợp các đối tượng, liên quan đến thứ tự sắp xếp . Chẳng hạn, các từ 'bat' và 'tab' đại diện cho hai hoán vị (hoặc sắp xếp) riêng biệt của một từ có ba chữ cái giống nhau.

Công thức hoán vị của số là gì?

Công thức hoán vị là gì? . P(n,r) = n. /(n-r).