leetcode刷題記錄Array篇(1&4~Two Sum&Median of Two Sorted Arrays)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
今天寫了一道比較簡單的和一道難的
#Two Sum

題目:Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element
twice.

簡單翻譯,就是在一個數組中尋找相加和target值相等的兩個數的下標,只要返回最先匹配的,並且下標相等的不算。

golang代碼:

package mainimport ("fmt") func main() {nums := []int{2, 7, 11, 15}fmt.Println(twoSum(nums, 9)) }//自己的版本 O(n^2)// func twoSum(nums []int, target int) []int { //     arr := []int{}//     for index, val := range nums {//         for index1, val2 := range nums {//             if val+val2 == target && index != index1 {//                 arr = []int{index, index1}//                 return arr//             }//         }//     }//     return arr// } //時間複雜度小的 O(n)func twoSum(nums []int, target int) []int {  arr := []int{}  container := make(map[int]int)  for i := 0; i < len(nums); i++ {    if xy, ok := container[target-nums[i]]; ok && xy != i {        arr = []int{container[target-nums[i]], i}        return arr    }    container[nums[i]] = i   }   return arr }
#

題目:There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

簡單翻譯:計算中位元,時間複雜度為O(log(m+n)),我的沒能達到要求,因為寫部落格才發現這後面的要求,不過通過了測試,但是速度相對來說比較慢

golang代碼:

package main   import (  "fmt"    ) func main() {nums1 := []int{1, 87, 9}nums2 := []int{2, 5, 6}fmt.Println(findMedianSortedArrays(nums1, nums2))} func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {l1, l2 := len(nums1), len(nums2)numbers1 := make([]float64, l1)numbers2 := make([]float64, l2)//合并兩個數組mergeArr := make([]float64, len(nums1)+len(nums2))for i, val := range nums1 {    numbers1[i] = float64(val)}for i, val := range nums2 {    numbers2[i] = float64(val)}copy(mergeArr, numbers1)copy(mergeArr[len(nums1):], numbers2)//排序(冒泡)var temp float64var middle float64length := len(mergeArr)for i := 0; i < length-1; i++ {    for j := 0; j < length-i-1; j++ {        if mergeArr[j] > mergeArr[j+1] {            temp = mergeArr[j]            mergeArr[j] = mergeArr[j+1]            mergeArr[j+1] = temp        }    }}if length == 0 {    middle = 0} else if length%2 == 0 {    fmt.Println((mergeArr[length/2] + mergeArr[length/2-1]) / 2)    middle = (mergeArr[length/2] + mergeArr[length/2-1]) / 2} else {    middle = float64(mergeArr[length/2])}return middle}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.