diff --git a/solution/001.Two Sum/Solution.rb b/solution/001.Two Sum/Solution.rb index fbdbcc8ad3747..26235fca40c3f 100644 --- a/solution/001.Two Sum/Solution.rb +++ b/solution/001.Two Sum/Solution.rb @@ -8,4 +8,4 @@ def two_sum(nums, target) end next end -end \ No newline at end of file +end diff --git a/solution/002.Add Two Numbers/Solution.rb b/solution/002.Add Two Numbers/Solution.rb index 009b48b99f84f..e399fe9226dfd 100644 --- a/solution/002.Add Two Numbers/Solution.rb +++ b/solution/002.Add Two Numbers/Solution.rb @@ -34,4 +34,4 @@ def add_two_numbers(l1, l2) end l3 -end \ No newline at end of file +end diff --git a/solution/007.Reverse Integer/Solution.rb b/solution/007.Reverse Integer/Solution.rb index 641671a8e4f84..ca44c6f8f2925 100644 --- a/solution/007.Reverse Integer/Solution.rb +++ b/solution/007.Reverse Integer/Solution.rb @@ -18,4 +18,4 @@ def reverse(x) # have to explicitly constraint the int boundary as per the dummy test case res = s.to_i res <= 214_748_364_7 && res >= -214_748_364_8 ? res : 0 -end \ No newline at end of file +end diff --git a/solution/013.Roman to Integer/Solution.rb b/solution/013.Roman to Integer/Solution.rb index 327f6931bee24..024b8ebc22f8d 100644 --- a/solution/013.Roman to Integer/Solution.rb +++ b/solution/013.Roman to Integer/Solution.rb @@ -29,4 +29,4 @@ def roman_to_int(s) end res -end \ No newline at end of file +end diff --git a/solution/014.Longest Common Prefix/Solution.rb b/solution/014.Longest Common Prefix/Solution.rb index d19d8118e128c..ca78369b31894 100644 --- a/solution/014.Longest Common Prefix/Solution.rb +++ b/solution/014.Longest Common Prefix/Solution.rb @@ -22,4 +22,4 @@ def longest_common_prefix(strs) end idx > 0 ? strs[0][0..idx] : '' -end \ No newline at end of file +end diff --git a/solution/015.3Sum/Solution.rb b/solution/015.3Sum/Solution.rb new file mode 100644 index 0000000000000..6714ce328e4e7 --- /dev/null +++ b/solution/015.3Sum/Solution.rb @@ -0,0 +1,28 @@ +# @param {Integer[]} nums +# @return {Integer[][]} +def three_sum(nums) + res = [] + nums.sort! + + for i in 0..(nums.length - 3) + next if i > 0 && nums[i - 1] == nums[i] + j = i + 1 + k = nums.length - 1 + while j < k do + sum = nums[i] + nums[j] + nums[k] + if sum < 0 + j += 1 + elsif sum > 0 + k -= 1 + else + res += [[nums[i], nums[j], nums[k]]] + j += 1 + k -= 1 + j += 1 while nums[j] == nums[j - 1] + k -= 1 while nums[k] == nums[k + 1] + end + end + end + + res +end diff --git a/solution/019.Remove Nth Node From End of List/Solution.rb b/solution/019.Remove Nth Node From End of List/Solution.rb new file mode 100644 index 0000000000000..9654f08b4a9f0 --- /dev/null +++ b/solution/019.Remove Nth Node From End of List/Solution.rb @@ -0,0 +1,27 @@ +# Definition for singly-linked list. +# class ListNode +# attr_accessor :val, :next +# def initialize(val) +# @val = val +# @next = nil +# end +# end + +# @param {ListNode} head +# @param {Integer} n +# @return {ListNode} +def remove_nth_from_end(head, n) + return nil if head.next.nil? + count = 1 + q = head + s = head + + while q.next do + q = q.next + s = s.next if count > n + count += 1 + end + return head.next if count == n + s.next = s.next.next + head +end diff --git a/solution/020.Valid Parentheses/Solution.rb b/solution/020.Valid Parentheses/Solution.rb new file mode 100644 index 0000000000000..68a94038ad225 --- /dev/null +++ b/solution/020.Valid Parentheses/Solution.rb @@ -0,0 +1,22 @@ +# @param {String} s +# @return {Boolean} +def is_valid(s) + stack = '' + s.split('').each do |c| + if ['{', '[', '('].include?(c) + stack += c + else + if c == '}' && stack[stack.length - 1] == '{' + + stack = stack.length > 1 ? stack[0..stack.length - 2] : "" + elsif c == ']' && stack[stack.length - 1] == '[' + stack = stack.length > 1 ? stack[0..stack.length - 2] : "" + elsif c == ')' && stack[stack.length - 1] == '(' + stack = stack.length > 1 ? stack[0..stack.length - 2] : "" + else + return false + end + end + end + stack == '' +end