主页

索引

模块索引

搜索页面

实例

实例1

  • 题目:

    package main
    
    var trips = [][]string{
     []string{"重庆", "新疆"},
     []string{"广州", "南京"},
     []string{"上海", "广州"},
     []string{"西藏", "重庆"},
     []string{"北京", "上海"},
     []string{"南京", "西藏"},
    }
    
    func main() {
     //本题时间为30分钟
     //trips是小明出差的行程, 请找出小明这次行程,并注释时间复杂度与空间复杂度
     //结果 [北京","上海", "广州", "南京", "西藏", "重庆","新疆"]
     for
    }
    
  • 解决1:

package main

import "log"

var trips = [][]string{
  []string{"重庆", "新疆"},
  []string{"广州", "南京"},
  []string{"上海", "广州"},
  []string{"西藏", "重庆"},
  []string{"北京", "上海"},
  []string{"南京", "西藏"},
}

func main() {
  //本题时间为30分钟
  //trips是小明出差的行程, 请找出小明这次行程,并注释时间复杂度与空间复杂度
  //结果 [北京","上海", "广州", "南京", "西藏", "重庆","新疆"]
  var result []string
  i := 1
  for _, trip := range trips {
    log.Println("-", trip)
    if len(result) == 0 {
      result = trip
      continue
    }
    //log.Println(newTrips)
    for _, trip := range trips {
      //log.Println("$", trip)
      if result[0] == trip[1] {
        result= append(trip, result[1:]...)
        log.Println("=====", result)
        break
      } else if result[len(result)-1] == trip[0] {
        result= append(result, trip[1])
        log.Println("====>", result)
        break
      } else {
        log.Println("=====")
      }
    }
    i++
  }
  log.Println(result)
}
  • 解决2:

package main

import "log"

var trips = [][]string{
  []string{"重庆", "新疆"},
  []string{"广州", "南京"},
  []string{"上海", "广州"},
  []string{"西藏", "重庆"},
  []string{"北京", "上海"},
  []string{"南京", "西藏"},
}

func main()  {
  newTrips := trips[1:]
  result := trips[0]

  for len(newTrips) != 0  {
    log.Println("^^^", len(newTrips))
    for i,trip := range newTrips {
      if result[0] == trip[1] {
        result= append([]string{trip[0]}, result...)
        newTrips = new_trip(newTrips, i)
        log.Println("=====", result)
        break
      } else if result[len(result)-1] == trip[0] {
        result= append(result, trip[1])
        newTrips = append(newTrips[:i], newTrips[i+1:]...)
        log.Println("====>", result)
        break
      } else {
        log.Println("=====")
      }
    }
  }
  log.Println(result)
}


func new_trip(newTrips [][]string, i int) [][]string {
  if i ==0 {
    return newTrips[1:]
  } else if i == len(newTrips)-1 {
    return  newTrips[:i]
  } else {
    return append(newTrips[:i], newTrips[i+1:]...)
  }
}

主页

索引

模块索引

搜索页面