Skip to content

Solution 015, 019 and 020 in Ruby #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solution/001.Two Sum/Solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ def two_sum(nums, target)
end
next
end
end
end
2 changes: 1 addition & 1 deletion solution/002.Add Two Numbers/Solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ def add_two_numbers(l1, l2)
end

l3
end
end
2 changes: 1 addition & 1 deletion solution/007.Reverse Integer/Solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
end
2 changes: 1 addition & 1 deletion solution/013.Roman to Integer/Solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ def roman_to_int(s)
end

res
end
end
2 changes: 1 addition & 1 deletion solution/014.Longest Common Prefix/Solution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ def longest_common_prefix(strs)
end

idx > 0 ? strs[0][0..idx] : ''
end
end
28 changes: 28 additions & 0 deletions solution/015.3Sum/Solution.rb
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions solution/019.Remove Nth Node From End of List/Solution.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions solution/020.Valid Parentheses/Solution.rb
Original file line number Diff line number Diff line change
@@ -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