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ụ Giải pháp mẫu Mã Java Đầu ra mẫu Sơ đồ 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 đã choMảng Java. Bài tập-68 có lời giải
Đầ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]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;
}
}
}
}
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]
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 Facebook và Twitter để 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
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 cba0
Enter the string : abc All possible strings are : abc acb bac bca cab cba1
Enter the string : abc All possible strings are : abc acb bac bca cab cba2
{
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba6
Enter the string : abc All possible strings are : abc acb bac bca cab cba7
Enter the string : abc All possible strings are : abc acb bac bca cab cba8
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
// Java program to print all
0 // Java program to print all
1Enter the string : abc All possible strings are : abc acb bac bca cab cba5
// Java program to print all
3Enter the string : abc All possible strings are : abc acb bac bca cab cba5
// Java program to print all
5 // Java program to print all
6Enter the string : abc All possible strings are : abc acb bac bca cab cba5
// 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.
8public
1
// permutations of a given string.
8public
3
public
5
Enter the string : abc All possible strings are : abc acb bac bca cab cba1
public
7public
8// Java program to print all
0 class
0// Java program to print all
0 class
2
{
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
class
6 class
7class
8class
9
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Permutation
1Enter the string : abc All possible strings are : abc acb bac bca cab cba5
{
class
8Permutation
5 Permutation
6// Java program to print all
0 Permutation
8
class
8{
{
1{
2
{
1{
4// permutations of a given string.
1{
6
{
1{
2
class
8// permutations of a given string.
4
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
// 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.
8public
1
// permutations of a given string.
8public
3
public
public
6
public
7_______99_______0 public
9// Java program to print all
0
Enter the string : abc All possible strings are : abc acb bac bca cab cba01
{
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba05
Enter the string : abc All possible strings are : abc acb bac bca cab cba06
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba05
Enter the string : abc All possible strings are : abc acb bac bca cab cba09
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba11
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba13
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba15
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba17
Enter the string : abc All possible strings are : abc acb bac bca cab cba18
// 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 cba22
đầ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 cba23
Enter the string : abc All possible strings are : abc acb bac bca cab cba24
Enter the string : abc All possible strings are : abc acb bac bca cab cba25
Enter the string : abc All possible strings are : abc acb bac bca cab cba26
class
Enter the string : abc All possible strings are : abc acb bac bca cab cba28
Enter the string : abc All possible strings are : abc acb bac bca cab cba29
Enter the string : abc All possible strings are : abc acb bac bca cab cba0
Enter the string : abc All possible strings are : abc acb bac bca cab cba1
Enter the string : abc All possible strings are : abc acb bac bca cab cba32
Enter the string : abc All possible strings are : abc acb bac bca cab cba33
Enter the string : abc All possible strings are : abc acb bac bca cab cba34
Enter the string : abc All possible strings are : abc acb bac bca cab cba35
class
6
Enter the string : abc All possible strings are : abc acb bac bca cab cba38_______99_______9
Enter the string : abc All possible strings are : abc acb bac bca cab cba40
Enter the string : abc All possible strings are : abc acb bac bca cab cba42
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba44
Enter the string : abc All possible strings are : abc acb bac bca cab cba45
Enter the string : abc All possible strings are : abc acb bac bca cab cba46
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba17
Enter the string : abc All possible strings are : abc acb bac bca cab cba49
Enter the string : abc All possible strings are : abc acb bac bca cab cba51
Enter the string : abc All possible strings are : abc acb bac bca cab cba52
Permutation
5_______103_______6// Java program to print all
0
Enter the string : abc All possible strings are : abc acb bac bca cab cba57
// Java program to print all
9 Enter the string : abc All possible strings are : abc acb bac bca cab cba59
Enter the string : abc All possible strings are : abc acb bac bca cab cba42
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba05
Enter the string : abc All possible strings are : abc acb bac bca cab cba64
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba66
// Java program to print all
9Enter the string : abc All possible strings are : abc acb bac bca cab cba68
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba70
// permutations of a given string.
1Enter the string : abc All possible strings are : abc acb bac bca cab cba46
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba74
Enter the string : abc All possible strings are : abc acb bac bca cab cba5
Enter the string : abc All possible strings are : abc acb bac bca cab cba76
Enter the string : abc All possible strings are : abc acb bac bca cab cba51
Enter the string : abc All possible strings are : abc acb bac bca cab cba51
// permutations of a given string.
5
Enter the string : abc All possible strings are : abc acb bac bca cab cba81
public
Enter the string : abc All possible strings are : abc acb bac bca cab cba0
Enter the string : abc All possible strings are : abc acb bac bca cab cba1
Enter the string : abc All possible strings are : abc acb bac bca cab cba85
Enter the string : abc All possible strings are : abc acb bac bca cab cba42
Enter the string : abc All possible strings are : abc acb bac bca cab cba88_______99_______5
Enter the string : abc All possible strings are : abc acb bac bca cab cba90
Enter the string : abc All possible strings are : abc acb bac bca cab cba92
Enter the string : abc All possible strings are : abc acb bac bca cab cba94_______3_______95_______3_______49
Enter the string : abc All possible strings are : abc acb bac bca cab cba52
Enter the string : abc All possible strings are : abc acb bac bca cab cba99
// 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 cba52
Enter the string : abc All possible strings are : abc acb bac bca cab cba99
// 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 cba51
Enter the string : abc All possible strings are : abc acb bac bca cab cba51
// 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ị