【2023-10-14】常见面试题

24 阅读3分钟

模版

package main

import (
   "fmt"
   "sort"
   "sync"
)

var wg sync.WaitGroup

func main() {
   SortTest()

   ThreadTest()
}

func ThreadTest() {
   ThreadTest1()
   ThreadTest2()
}

func ThreadTest1() {

   fmt.Println()
}

func ThreadTest2() {
   
   fmt.Println()
}

func SortTest() {
   // Test Case 1
   nums1 := []int{7, 15, 11, 7, 3, 14, 1, 18, 13, 12, 17, 7, 2, 19, 4, 9, 3, 8, 9, 9}
   Sort(nums1)

   // Test Case 2
   nums2 := []int{5, 4, 3, 2, 1}
   Sort(nums2)

   // Test Case 3
   nums3 := []int{1, 2, 3, 4, 5}
   Sort(nums3)

   // Test Case 4
   nums4 := []int{9, 8, 7, 6, 5, 4, 3, 2, 1}
   Sort(nums4)

   // Test Case 5
   nums5 := []int{}
   Sort(nums5)

   // Test Case 6
   nums6 := []int{5, 5, 5, 5, 5}
   Sort(nums6)

   // Test Case 7
   nums7 := []int{3, 7, 1, 4, 2, 8, 6, 5}
   Sort(nums7)

   // Test Case 8
   nums8 := []int{10}
   Sort(nums8)

   // Test Case 9
   nums9 := []int{2, 2, 2, 1, 1, 1}
   Sort(nums9)

   // Test Case 10
   nums10 := []int{100, 3, 25, 12, 6, 3, 1}
   Sort(nums10)

}

func Sort(nums []int) {
   n := len(nums)
   ans := make([]int, n)
   copy(ans, nums)
   sort.Ints(ans)

   mergeAns := MergeSort(nums)
   for i, v := range mergeAns {
      if v != ans[i] {
         fmt.Println(nums)
         break
      }
   }

   QuickSort(nums, 0, n-1)
   for i, v := range nums {
      if v != ans[i] {
         fmt.Println(nums)
         break
      }
   }
}

func MergeSort(nums []int) []int {
   n := len(nums)
   ans := make([]int, 0, n)
   
   return ans
}

func QuickSort(nums []int, low int, high int) {

}

答案

package main

import (
   "fmt"
   "sort"
   "sync"
)

var wg sync.WaitGroup

func main() {
   SortTest()

   ThreadTest()
}

func ThreadTest() {
   ThreadTest1()
   ThreadTest2()
}

func ThreadTest1() {
   wg.Add(2)
   ch1 := make(chan int, 1)
   ch2 := make(chan int, 0)

   ch1 <- 0
   go func() {
      for {
         now := <-ch1
         if now == 5 {
            break
         }
         fmt.Print(string('a' + now))
         ch2 <- now + 1
      }
      wg.Done()
   }()

   go func() {
      for {
         now := <-ch2
         fmt.Print(now)

         ch1 <- now
         if now == 5 {
            break
         }
      }
      wg.Done()
   }()
   wg.Wait()
   fmt.Println()
}

func ThreadTest2() {
   wg.Add(3)
   ch1 := make(chan int, 1)
   ch2 := make(chan int, 0)
   ch3 := make(chan int, 0)

   ch1 <- 0
   go func() {
      for {
         now := <-ch1
         if now == 3 {
            break
         }
         fmt.Print(1)
         ch2 <- now
      }
      wg.Done()
   }()
   go func() {
      for {
         now := <-ch2
         fmt.Print(2)
         ch3 <- now
         if now == 2 {
            break
         }
      }
      wg.Done()
   }()
   go func() {
      for {
         now := <-ch3
         fmt.Print(3)
         ch1 <- now + 1
         if now == 2 {
            break
         }
      }
      wg.Done()
   }()

   wg.Wait()
   fmt.Println()
}

func SortTest() {
   // Test Case 1
   nums1 := []int{7, 15, 11, 7, 3, 14, 1, 18, 13, 12, 17, 7, 2, 19, 4, 9, 3, 8, 9, 9}
   Sort(nums1)

   // Test Case 2
   nums2 := []int{5, 4, 3, 2, 1}
   Sort(nums2)

   // Test Case 3
   nums3 := []int{1, 2, 3, 4, 5}
   Sort(nums3)

   // Test Case 4
   nums4 := []int{9, 8, 7, 6, 5, 4, 3, 2, 1}
   Sort(nums4)

   // Test Case 5
   nums5 := []int{}
   Sort(nums5)

   // Test Case 6
   nums6 := []int{5, 5, 5, 5, 5}
   Sort(nums6)

   // Test Case 7
   nums7 := []int{3, 7, 1, 4, 2, 8, 6, 5}
   Sort(nums7)

   // Test Case 8
   nums8 := []int{10}
   Sort(nums8)

   // Test Case 9
   nums9 := []int{2, 2, 2, 1, 1, 1}
   Sort(nums9)

   // Test Case 10
   nums10 := []int{100, 3, 25, 12, 6, 3, 1}
   Sort(nums10)

}

func Sort(nums []int) {
   n := len(nums)
   ans := make([]int, n)
   copy(ans, nums)
   sort.Ints(ans)

   mergeAns := MergeSort(nums)
   for i, v := range mergeAns {
      if v != ans[i] {
         fmt.Println(nums)
         break
      }
   }

   QuickSort(nums, 0, n-1)
   for i, v := range nums {
      if v != ans[i] {
         fmt.Println(nums)
         break
      }
   }
}

func MergeSort(nums []int) []int {
   n := len(nums)
   if n <= 1 {
      return nums
   }

   left := MergeSort(nums[:n/2])
   right := MergeSort(nums[n/2:])

   ans := make([]int, 0, n)
   i, j := 0, 0
   for i < len(left) || j < len(right) {
      if i == len(left) {
         ans = append(ans, right[j])
         j++
      } else if j == len(right) {
         ans = append(ans, left[i])
         i++
      } else if left[i] < right[j] {
         ans = append(ans, left[i])
         i++
      } else {
         ans = append(ans, right[j])
         j++
      }
   }
   return ans
}

func QuickSort(nums []int, low int, high int) {
   if low >= high {
      return
   }
   i, j := low, high
   value := nums[low]

   for i < j {
      for i < j && nums[j] > value {
         j--
      }
      nums[i], nums[j] = nums[j], nums[i]
      for i < j && nums[i] <= value {
         i++
      }
      nums[i], nums[j] = nums[j], nums[i]
   }
   QuickSort(nums, low, i-1)
   QuickSort(nums, i+1, high)
}