n numbers, find the number of combinations and the content of each combination, including deduplication with the same elements (kotlin)


foreword

I was doing happy work (fishing), but my colleague said, are you bored? I'm bored, I'll find you something to do.
Me:
insert image description here
OK, OK!


1. Problems

insert image description here
Before you see deduplication, don’t you look down on people? It’s too simple.
insert image description here

After seeing the deduplication, do you look down on me too much? Who can do this.
insert image description here

Two, solve the problem

1. Before deduplication

First write out the unrepeated ones

fun generateCombinations(numbers: List<Int>): List<List<Int>> {
    
    
  val combinations = mutableListOf<List<Int>>()
  for (i in 0 until numbers.size) {
    
    
   val combination = mutableListOf<Int>()
   combination.add(numbers[i])
   combinations.add(combination)
   for (j in i + 1 until numbers.size) {
    
    
    combination.add(numbers[j])
    combinations.add(combination.toList())
   }
  }
  return combinations
 }

Double for loop traverses all possibilities, nothing to say

2. After deduplication

I can't do it (dog head), so what should I do, I'm not a bad showman! Then the problem came again, what should I do? ! chatgpt! ! !

fun generateCombinations(numbers: List<Int>): List<List<Int>> {
    
    
  val combinations = mutableListOf<List<Int>>()
  for (i in 0 until numbers.size) {
    
    
   val combination = mutableListOf<Int>()
   combination.add(numbers[i])
   combinations.add(combination)
   for (j in i + 1 until numbers.size) {
    
    
    combination.add(numbers[j])
    combinations.add(combination.toList())
   }
  }
  return combinations.distinct()
 }

That's right, you read that right, there is only one more distinct function, chatgpt is simply my god, let's take a look at the source code,
insert image description here
mom no longer has to worry about my study!


Summarize

Finally, come to a list of 1 to 256 to test

 fun SummationAnddeDuplication() {
    
    
  val long = System.currentTimeMillis()
  val numbers = mutableListOf<Int>()
  for (i in 1..256){
    
    
   numbers.add(i)
  }
  val combinations = generateCombinations(numbers)
  println("Combinations:")
  combinations.forEach {
    
     println(it) }
  val long1 = System.currentTimeMillis()-long
  println("Number of combinations: ${
      
      combinations.size}" +"   time :  ${
      
      long1/1000}s" )

 }

 fun generateCombinations(numbers: List<Int>): List<List<Int>> {
    
    
  val combinations = mutableListOf<List<Int>>()
  for (i in 0 until numbers.size) {
    
    
   val combination = mutableListOf<Int>()
   combination.add(numbers[i])
   combinations.add(combination)
   for (j in i + 1 until numbers.size) {
    
    
    combination.add(numbers[j])
    combinations.add(combination.toList())
   }
  }
  return combinations.distinct()
 }

The output
insert image description here
is really good, chatgpt is really useful for learning to write code!
insert image description here

Guess you like

Origin blog.csdn.net/shop_and_sleep/article/details/129277942