Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何提高嵌套循环的算法效率_Ruby_Algorithm_Loops_Iteration - Fatal编程技术网

Ruby 如何提高嵌套循环的算法效率

Ruby 如何提高嵌套循环的算法效率,ruby,algorithm,loops,iteration,Ruby,Algorithm,Loops,Iteration,给定一个整数列表和一个求和值,返回构成求和的前两个值(从左开始) 例如,假设: sum_pairs([10, 5, 2, 3, 7, 5], 10) [5,5](在[10,5,2,3,7,5]的索引[1,5])加起来就是10,而[3,7](在索引[3,4])加起来就是10。其中,整对[3,7]都在前面,因此是正确的答案 这是我的密码: def sum_pairs(ints, s) result = [] i = 0 while i < ints.length - 1

给定一个整数列表和一个求和值,返回构成求和的前两个值(从左开始)

例如,假设:

sum_pairs([10, 5, 2, 3, 7, 5], 10)
[5,5]
(在
[10,5,2,3,7,5]
的索引
[1,5]
)加起来就是
10
,而
[3,7]
(在索引
[3,4]
)加起来就是
10
。其中,整对
[3,7]
都在前面,因此是正确的答案

这是我的密码:

def sum_pairs(ints, s)
  result = []
  i = 0
  while i < ints.length - 1
    j = i+1
    while j < ints.length
      result << [ints[i],ints[j]] if ints[i] + ints[j] == s
      j += 1
    end
    i += 1
  end
  puts result.to_s
  result.min
end
def sum_对(整数、整数)
结果=[]
i=0
而i
  • 将所看到的数字设置为一组,开始时为空
  • 查看输入列表中的每个数字
  • 计算你需要加上哪个数字才能算出总数
  • 看看这个号码是否在集合中
  • 如果是,则返回它和当前元素
  • 如果不是,则将当前元素添加到集合中,并继续循环
  • 当循环结束时,您确定没有这样的对;任务没有指定,但返回
    nil
    可能是最好的选择
  • 应该是超高速的,因为只有一个循环。它还将在找到第一对匹配项时终止,因此通常在得到答案之前,您甚至不会遍历每个元素

    作为一种风格,以这种方式使用
    while
    是非常不拘泥的。在实现上述功能时,我建议您使用
    ints.each do | int |。。。结束时
    而不是

    编辑:正如Cary Swoveland评论的那样,出于一个奇怪的原因,我认为您需要的是索引,而不是值。

    需要“设置”
    
    require 'set'
    
    def sum_pairs(arr, target)
      s = Set.new
      arr.each do |v|
        return [target-v, v] if s.include?(target-v)
        s << v
      end
      nil
    end
    
    sum_pairs [10, 5, 2, 3, 7, 5], 10
      #=> [3, 7]
    sum_pairs [10, 5, 2, 3, 7, 5], 99
      #=> nil
    
    def总和对(arr,目标) s=设置为新 每一道菜| 如果s.include?(target-v),则返回[target-v,v] s[3,7] 和对[10,5,2,3,7,5],99 #=>零

    我使用了一些方法来加速
    包括?
    查找(不太重要的是,只保存唯一的值)。

    尝试下面的方法,因为它更具可读性

    def sum_pairs(ints, s)
      ints.each_with_index.map do |ele, i|
        if ele < s
          rem_arr = ints.from(i + 1)
          rem = s - ele
          [ele, rem] if rem_arr.include?(rem)
        end
      end.compact.last
    end
    
    def sum_对(整数、整数)
    ints.each_with_index.map do | ele,i|
    如果ele
    一个班轮(最快的?)

    ari=[10,0,8,5,2,7,3,5,5]
    总和=10
    定义和对(ary、sum)
    用{e,i{e,i]}组合(2).如果{a}a.first.first+a.last.first==sum}.map{e}[e,e.max{a,b.last b.last}.min{a,b.last b.last}.first.map{e{e,e.max{a,b.last}.last}.last}
    终止
    

    是的,它不是真正可读的,但是如果你从
    ari.map.with_index{e,i |[e,i]}
    开始一步一步地添加方法,很容易理解它是如何工作的。

    试试,找到返回和的对,然后在原始数组中查找它们的索引。我需要66毫秒。你是在一个更大的集合上运行的吗?我认为OP不需要索引,只需要数字,如果你这样读的话,我希望你会在我的回答中建议我所做的。@CarySwoveland:啊,确实,不知道我为什么认为需要索引:/是的,你的代码就是我试图让OP写的:DSorry我还是个初学者,我不明白
    end.compact.last
    做什么。我的理解是这样的,我们不必定义一个变量来存储rem\u arr的
    .map
    结果,对吗?还有,为什么
    .from
    方法没有定义?
    ary = [10, 0, 8, 5, 2, 7, 3, 5, 5]
    sum = 10
    
    def sum_pairs(ary, sum)
      ary.map.with_index { |e, i|  [e, i] }.combination(2).to_a.keep_if { |a| a.first.first + a.last.first == sum }.map { |e| [e, e.max { |a, b| a.last <=> b.last }.last] }.min { |a, b| a.last <=> b.last }.first.map{ |e| e.first }
    end