Skip to content

Commit 2c979f0

Browse files
solved
1 parent 7bdcf9a commit 2c979f0

8 files changed

+165
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# @param {String} s
2+
# @return {Integer}
3+
def length_of_longest_substring(s)
4+
stack = []
5+
highest_count = 0
6+
s.each_char do |c|
7+
unless index = stack.index(c)
8+
stack.push(c)
9+
else
10+
stack.push(c)
11+
(index + 1).times {stack.shift}
12+
end
13+
highest_count = stack.length if stack.length > highest_count
14+
end
15+
highest_count
16+
end

Max_Area_of_Island.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def DFS(grid, i, j)
2+
if i < 0 || j < 0 || i > grid.length - 1 || j > grid[0].length - 1 || grid[i][j] == 0
3+
return 0
4+
end
5+
6+
grid[i][j] = 0
7+
8+
return 1 + DFS(grid, i - 1, j) + DFS(grid, i + 1, j) + DFS(grid, i, j - 1) + DFS(grid, i, j + 1)
9+
end
10+
11+
# @param {Integer[][]} grid
12+
# @return {Integer}
13+
def max_area_of_island(grid)
14+
maxArea = 0
15+
16+
for i in 0..grid.length - 1
17+
for j in 0..grid[0].length - 1
18+
maxArea = [maxArea, DFS(grid, i, j)].max
19+
end
20+
end
21+
22+
return maxArea
23+
end

MiddleoftheLinkedList.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def middle_node(head)
2+
org_head = head
3+
count = 0
4+
while !head.nil?
5+
count+=1
6+
head = head.next
7+
end
8+
9+
i = 0
10+
head = org_head
11+
while i < count/2
12+
i+=1
13+
head = head.next
14+
end
15+
16+
return head
17+
end

Permutation_in_String.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# @param {String} s1
2+
# @param {String} s2
3+
# @return {Boolean}
4+
def check_inclusion(s1, s2)
5+
return false if s2.size < s1.size
6+
window = Array.new(26, 0)
7+
8+
s1.each_char do |ch|
9+
window[ch.ord - 'a'.ord] += 1
10+
end
11+
12+
s2.each_char.with_index do |ch, i|
13+
window[ch.ord - 'a'.ord] -= 1
14+
if i >= s1.size
15+
window[s2[i - s1.size].ord - 'a'.ord] += 1
16+
end
17+
if window.all?(&:zero?)
18+
return true
19+
end
20+
end
21+
22+
false
23+
end

Remove_Nth_Node_From_End_of_List.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def remove_nth_from_end(head, n)
2+
node = head
3+
prev = nil
4+
arr_node = []
5+
while !node.nil? do
6+
arr_node << node
7+
node = node.next
8+
end
9+
if arr_node.length - n == 0
10+
head = head.next
11+
else
12+
node = arr_node[-n-1]
13+
node.next = node.next.next
14+
end
15+
16+
return head
17+
end

ReverseWordsinaStringIII.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def reverse_words(s)
2+
s.split.map(&:reverse).join(" ")
3+
end

merge_two_lists.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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} l1
11+
# @param {ListNode} l2
12+
# @return {ListNode}
13+
def merge_two_lists(l1, l2)
14+
solution = [] # head = nil
15+
ho = l1
16+
ht = l2
17+
18+
while !ho.nil? && !ht.nil? do
19+
if ho.val < ht.val
20+
solution << ho.val
21+
ho = ho.next
22+
else
23+
solution << ht.val
24+
ht = ht.next
25+
end
26+
end
27+
28+
if ho.nil?
29+
while !ht.nil? do
30+
solution << ht.val
31+
ht = ht.next
32+
end
33+
elsif ht.nil?
34+
while !ho.nil? do
35+
solution << ho.val
36+
ho = ho.next
37+
end
38+
end
39+
40+
solution
41+
end
42+
43+
list1 = [1,2,4], list2 = [1,3,4]
44+
45+
re = merge_two_lists(list1, list2)
46+
47+
puts re

remove_element.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# @param {Integer[]} nums
2+
# @param {Integer} val
3+
# @return {Integer}
4+
def remove_element(nums, val)
5+
return 0 if nums.length == 0
6+
nums.each_with_index do |num, index|
7+
if num == val
8+
nums[index] = nil
9+
end
10+
end
11+
nums.compact!
12+
nums.length
13+
14+
end
15+
16+
17+
nums = [3,2,2,3], val = 3
18+
19+
puts remove_element(nums,val)

0 commit comments

Comments
 (0)