Leetcode Brush Title Record array article (1&4~two Sum&median of the Sorted Arrays)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Today wrote a relatively simple and a difficult

#Two Sum

Title: Given An array of integers, return indices of the both numbers such that they add-to a specific target.
You may assume this each input would has exactly one solution, and you could not use the same element
Twice.

A simple translation is to look for an array of two-digit subscripts that are equal to the target value, as long as the first match is returned and the subscript is equal.

Golang Code:

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 }

#

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

Simple translation: Calculate the median, time complexity of O (log (m+n)), I did not meet the requirements, because the blog to find the following requirements, but passed the test, but relatively slow speed

Golang Code:

Package main import ("FMT") func main () {nums1: = []int{1, 9}nums2: = []int{2, 5, 6}fmt. Println (Findmediansortedarrays (NUMS1, NUMS2)} func findmediansortedarrays (nums1 []int, Nums2 []int) float64 {l1, L2: = L En (NUMS1), Len (NUMS2) Numbers1: = Make ([]float64, L1) Numbers2: = Make ([]float64, L2)//Merge two arrays 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)//Sort (bubbling) 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} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.