File tree 1 file changed +22
-0
lines changed
1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
1
+ # ================================================================================
2
+ # LeetCode - Algorithms - 1. Two Sum
3
+ # https://leetcode.com/problems/two-sum/description
4
+ #
5
+ # Language: Ruby
6
+ # Auther: ConradG
7
+ # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8
+ # 第一反应为双循环遍历,发现题目归类于 Hash Table 中后也尝试使Hash Table 进行处理。
9
+ # 在单线程场景下,使用Hash的方式有更优秀的时间复杂度,而双循环则更易拆分为多个线程。
10
+ #
11
+ # * Ruby中 []= 的优先级和 [] 一致,为除 () 外的最高优先级
12
+ # ================================================================================
13
+
14
+ # @param {Integer[]} nums
15
+ # @param {Integer} target
16
+ # @return {Integer[]}
17
+
18
+ def two_sum ( nums , target )
19
+ s = Hash . new
20
+ num , i = nums . each_with_index . detect { |( num , i ) | s [ target - num ] || !s [ num ] = i }
21
+ [ s [ target - num ] , i ]
22
+ end
You can’t perform that action at this time.
0 commit comments