File tree 3 files changed +77
-0
lines changed
019.Remove Nth Node From End of List
3 files changed +77
-0
lines changed Original file line number Diff line number Diff line change
1
+ # @param {Integer[]} nums
2
+ # @return {Integer[][]}
3
+ def three_sum ( nums )
4
+ res = [ ]
5
+ nums . sort!
6
+
7
+ for i in 0 ..( nums . length - 3 )
8
+ next if i > 0 && nums [ i - 1 ] == nums [ i ]
9
+ j = i + 1
10
+ k = nums . length - 1
11
+ while j < k do
12
+ sum = nums [ i ] + nums [ j ] + nums [ k ]
13
+ if sum < 0
14
+ j += 1
15
+ elsif sum > 0
16
+ k -= 1
17
+ else
18
+ res += [ [ nums [ i ] , nums [ j ] , nums [ k ] ] ]
19
+ j += 1
20
+ k -= 1
21
+ j += 1 while nums [ j ] == nums [ j - 1 ]
22
+ k -= 1 while nums [ k ] == nums [ k + 1 ]
23
+ end
24
+ end
25
+ end
26
+
27
+ res
28
+ end
Original file line number Diff line number Diff line change
1
+ # Definition for singly-linked list.
2
+ # class ListNode
3
+ # attr_accessor :val, :next
4
+ # def initialize(val)
5
+ # @val = val
6
+ # @next = nil
7
+ # end
8
+ # end
9
+
10
+ # @param {ListNode} head
11
+ # @param {Integer} n
12
+ # @return {ListNode}
13
+ def remove_nth_from_end ( head , n )
14
+ return nil if head . next . nil?
15
+ count = 1
16
+ q = head
17
+ s = head
18
+
19
+ while q . next do
20
+ q = q . next
21
+ s = s . next if count > n
22
+ count += 1
23
+ end
24
+ return head . next if count == n
25
+ s . next = s . next . next
26
+ head
27
+ end
Original file line number Diff line number Diff line change
1
+ # @param {String} s
2
+ # @return {Boolean}
3
+ def is_valid ( s )
4
+ stack = ''
5
+ s . split ( '' ) . each do |c |
6
+ if [ '{' , '[' , '(' ] . include? ( c )
7
+ stack += c
8
+ else
9
+ if c == '}' && stack [ stack . length - 1 ] == '{'
10
+
11
+ stack = stack . length > 1 ? stack [ 0 ..stack . length - 2 ] : ""
12
+ elsif c == ']' && stack [ stack . length - 1 ] == '['
13
+ stack = stack . length > 1 ? stack [ 0 ..stack . length - 2 ] : ""
14
+ elsif c == ')' && stack [ stack . length - 1 ] == '('
15
+ stack = stack . length > 1 ? stack [ 0 ..stack . length - 2 ] : ""
16
+ else
17
+ return false
18
+ end
19
+ end
20
+ end
21
+ stack == ''
22
+ end
You can’t perform that action at this time.
0 commit comments