【问题标题】:stdout can print correct result but not output in ruby although i have return the valuesstdout 可以打印正确的结果,但不能在 ruby​​ 中输出,尽管我已经返回了值
【发布时间】:2022-10-01 07:27:27
【问题描述】:
def two_sum(nums, target)
    for i in 0..3 - 1
        for j in 0..3 - 1
            if nums[i] + nums[j] == target && i < j && i != j
                puts \'[\' + (i - 1).to_s + \',\' + (j - 1).to_s + \']\'
            end
        end
    end
    return (i - 1), (j - 1)
end

def main()
    nums = Array.new()
    target = gets().to_i
    nums = gets().to_i
    two_sum(nums, target)
end

main()

练习的要求是打印出总和等于目标数字的数字。你首先需要得到一个整数数组和目标数。

谁能帮我调试一下?谢谢你。

    标签: ruby algorithm


    【解决方案1】:

    我会让其他人调试您的代码。相反,我想提出另一种可以相对有效地进行计算的方法。

    def two_sum(nums, target)
      h = nums.each_with_index.with_object(Hash.new { |h,k| h[k] = [] }) do |(n,i),h|
        h[n] << i
      end
      n,i = nums.each_with_index.find { |n,_i| h.key?(target-n) }
      return nil if n.nil?
      indices = h[target-n]
      return [i,indices.first] unless n == target/2
      return nil if indices.size == 1      
      [i, indices.find { |j| j !=i }]
    end
    
    ​​​
    two_sum([2,7,11,15], 9)           #=> [0, 1]
    two_sum([2,7,11,15], 10)          #=> nil
    two_sum([2,7,11,15], 4)           #=> nil
    two_sum([2,7,11,2,15], 4)         #=> [0, 3]
    two_sum([2,11,7,11,2,15,11], 22)  #=> [1, 3]
    

    在最后一个例子中

    h #=> {2=>[0, 4], 11=>[1, 3, 6], 7=>[2], 15=>[5]}
    

    请注意,哈希中的键查找非常快,特别是行的执行

    indices = h[target-n]
    

    构建h 的计算复杂度为 O(n),其中n = num.size 和余数非常接近 O(n)(“非常接近”,因为键查找接近于恒定时间),整体计算复杂度接近 O(n),而考虑num 中每对值的蛮力方法是 O(n^2)。


    如果定义了哈希

    h = Hash.new { |h,k| h[k] = [] }
    

    h 没有键k 时执行h[k] 导致

    h[k] = []
    

    被执行。例如,如果

    h #=> { 2=>[0] }
    

    然后

    h[11] << 1
    

    原因

    h[11] = []
    

    被执行(因为h没有密钥11),之后

    h[11] << 1
    

    被执行,导致

    h #=> { 2=>[0], 11=>[1] }
    

    相比之下,如果那时

    h[2] << 3
    

    执行我们得到

    h #=> { 2=>[0,3], 11=>[1] }
    

    没有 h[2] = [] 被执行,因为 h 已经有一个密钥 2。见Hash::new


    将块变量表示为

    |(n,i),h|
    

    array decomposition 的一种形式。

    【讨论】:

      猜你喜欢
      • 2021-04-25
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 2019-03-11
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      相关资源
      最近更新 更多