在Swift中缺少回归

时间:2018-03-16 05:39:51

标签: swift

这是我的代码。

import UIKit

var str = "Hello, playground"

//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)).

//Example 1:
//nums1 = [1, 3]
//nums2 = [2]
//
//The median is 2.0
//Example 2:
//nums1 = [1, 2]
//nums2 = [3, 4]
//
//The median is (2 + 3)/2 = 2.5
var num1 = [1,2,2,5]
var num2 = [2,3,9,9]

class Solution {
    func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
        var A = nums1
        var B = nums2
        var m = nums1.count
        var n = nums2.count
        var max_of_left : Int = 0
        var min_of_right = 0
        if n < m {
            var temp : [Int]
            var tempt : Int
            temp = nums1
            tempt = m
            A = nums2
            B = temp
            m = n
            n = tempt
        }
        if n == 0{
        fatalError("Arrays must be fulfilled")
        }

        var imin = 0
        var imax = m
        let half_len = Int((m+n+1)/2)

        while imin <= imax {
            let i = Int((imin + imax) / 2)
            let j = half_len - i

            if i > 0 && A[i-1] > B[j]{
                imax = i - 1
            }
            else if i < m && A[i] < B[j-1]{
                imin = i + 1
            }
            else
            {
                if i == 0{
                    max_of_left = B[j-1]
                }
                else if j == 0{
                    max_of_left = A[i-1]
                }
                else
                {
                    max_of_left = max(A[i-1], B[j-1])
                }

                if m+n % 2 == 1{
                    return Double(max_of_left)
                }

                if i==m{
                    min_of_right = B[j]
                }
                else if j == n{
                    min_of_right = A[i]

                }
                else{
                    min_of_right = min(A[i], B[j])
                 //editor indicates error here
                }


                return Double((Double(max_of_left+min_of_right) / 2.0))

            }
        }
    }
}

var a = Solution()
print(a.findMedianSortedArrays(num1, num2))
  

错误:day4_Median_of_Two_Sorted_Arrays.playground:86:13:错误:在预期返回'Double'的函数中缺少返回

由于我将returnif语句中删除,我认为它会没问题,因为它会在遇到返回时循环播放时停止。

但是编辑说这不是。

我想知道原因。请解释一下原因。

2 个答案:

答案 0 :(得分:1)

findMedianSortedArrays()的每个代码路径都必须返回Double。

所以你需要在while循环之外返回一个Double。即使你让while循环中的每个代码路径都有一个返回double,如果imin> gt; imax你甚至不会进入while循环,所以需要在它之外返回一个double。

答案 1 :(得分:0)

我通过将另一个退出while循环来修复它。

    //: Playground - noun: a place where people can play

    import UIKit

    var str = "Hello, playground"

    //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)).

    //Example 1:
    //nums1 = [1, 3]
    //nums2 = [2]
    //
    //The median is 2.0
    //Example 2:
    //nums1 = [1, 2]
    //nums2 = [3, 4]
    //
    //The median is (2 + 3)/2 = 2.5
    var num1 = [1,2,2,5]
    var num2 = [2,3,9,9]

    class Solution {
        func findMedianSortedArrays(_ nums1: [Int], _ nums2: [Int]) -> Double {
            var A = nums1
            var B = nums2
            var m = nums1.count
            var n = nums2.count
            var max_of_left : Int = 0
            var min_of_right = 0
            if n < m {
                var temp : [Int]
                var tempt : Int
                temp = nums1
                tempt = m
                A = nums2
                B = temp
                m = n
                n = tempt
            }
            if n == 0{
            fatalError("Arrays must be fulfilled")
            }

            var imin = 0
            var imax = m
            let half_len = Int((m+n+1)/2)

            while imin <= imax {
                let i = Int((imin + imax) / 2)
                let j = half_len - i

                if i > 0 && A[i-1] > B[j]{
                    imax = i - 1
                }
                else if i < m && A[i] < B[j-1]{
                    imin = i + 1
                }
                else
                {
                    if i == 0{
                        max_of_left = B[j-1]
                    }
                    else if j == 0{
                        max_of_left = A[i-1]
                    }
                    else
                    {
                        max_of_left = max(A[i-1], B[j-1])
                    }

                    if m+n % 2 == 1{
                        return Double(max_of_left)
                    }

                    if i==m{
                        min_of_right = B[j]
                    }
                    else if j == n{
                        min_of_right = A[i]

                    }
                    else{
                        min_of_right = min(A[i], B[j])
                    }

                    return Double((Double(max_of_left+min_of_right) / 2.0))

                }

            }
            return Double((Double(max_of_left+min_of_right) / 2.0))
        }
    }

    var a = Solution()
    print(a.findMedianSortedArrays(num1, num2))