-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bdcf9a
commit 2c979f0
Showing
8 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# @param {String} s | ||
# @return {Integer} | ||
def length_of_longest_substring(s) | ||
stack = [] | ||
highest_count = 0 | ||
s.each_char do |c| | ||
unless index = stack.index(c) | ||
stack.push(c) | ||
else | ||
stack.push(c) | ||
(index + 1).times {stack.shift} | ||
end | ||
highest_count = stack.length if stack.length > highest_count | ||
end | ||
highest_count | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
def DFS(grid, i, j) | ||
if i < 0 || j < 0 || i > grid.length - 1 || j > grid[0].length - 1 || grid[i][j] == 0 | ||
return 0 | ||
end | ||
|
||
grid[i][j] = 0 | ||
|
||
return 1 + DFS(grid, i - 1, j) + DFS(grid, i + 1, j) + DFS(grid, i, j - 1) + DFS(grid, i, j + 1) | ||
end | ||
|
||
# @param {Integer[][]} grid | ||
# @return {Integer} | ||
def max_area_of_island(grid) | ||
maxArea = 0 | ||
|
||
for i in 0..grid.length - 1 | ||
for j in 0..grid[0].length - 1 | ||
maxArea = [maxArea, DFS(grid, i, j)].max | ||
end | ||
end | ||
|
||
return maxArea | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def middle_node(head) | ||
org_head = head | ||
count = 0 | ||
while !head.nil? | ||
count+=1 | ||
head = head.next | ||
end | ||
|
||
i = 0 | ||
head = org_head | ||
while i < count/2 | ||
i+=1 | ||
head = head.next | ||
end | ||
|
||
return head | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# @param {String} s1 | ||
# @param {String} s2 | ||
# @return {Boolean} | ||
def check_inclusion(s1, s2) | ||
return false if s2.size < s1.size | ||
window = Array.new(26, 0) | ||
|
||
s1.each_char do |ch| | ||
window[ch.ord - 'a'.ord] += 1 | ||
end | ||
|
||
s2.each_char.with_index do |ch, i| | ||
window[ch.ord - 'a'.ord] -= 1 | ||
if i >= s1.size | ||
window[s2[i - s1.size].ord - 'a'.ord] += 1 | ||
end | ||
if window.all?(&:zero?) | ||
return true | ||
end | ||
end | ||
|
||
false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
def remove_nth_from_end(head, n) | ||
node = head | ||
prev = nil | ||
arr_node = [] | ||
while !node.nil? do | ||
arr_node << node | ||
node = node.next | ||
end | ||
if arr_node.length - n == 0 | ||
head = head.next | ||
else | ||
node = arr_node[-n-1] | ||
node.next = node.next.next | ||
end | ||
|
||
return head | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
def reverse_words(s) | ||
s.split.map(&:reverse).join(" ") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Definition for singly-linked list. | ||
# class ListNode | ||
# attr_accessor :val, :next | ||
# def initialize(val) | ||
# @val = val | ||
# @next = nil | ||
# end | ||
# end | ||
|
||
# @param {ListNode} l1 | ||
# @param {ListNode} l2 | ||
# @return {ListNode} | ||
def merge_two_lists(l1, l2) | ||
solution = [] # head = nil | ||
ho = l1 | ||
ht = l2 | ||
|
||
while !ho.nil? && !ht.nil? do | ||
if ho.val < ht.val | ||
solution << ho.val | ||
ho = ho.next | ||
else | ||
solution << ht.val | ||
ht = ht.next | ||
end | ||
end | ||
|
||
if ho.nil? | ||
while !ht.nil? do | ||
solution << ht.val | ||
ht = ht.next | ||
end | ||
elsif ht.nil? | ||
while !ho.nil? do | ||
solution << ho.val | ||
ho = ho.next | ||
end | ||
end | ||
|
||
solution | ||
end | ||
|
||
list1 = [1,2,4], list2 = [1,3,4] | ||
|
||
re = merge_two_lists(list1, list2) | ||
|
||
puts re |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# @param {Integer[]} nums | ||
# @param {Integer} val | ||
# @return {Integer} | ||
def remove_element(nums, val) | ||
return 0 if nums.length == 0 | ||
nums.each_with_index do |num, index| | ||
if num == val | ||
nums[index] = nil | ||
end | ||
end | ||
nums.compact! | ||
nums.length | ||
|
||
end | ||
|
||
|
||
nums = [3,2,2,3], val = 3 | ||
|
||
puts remove_element(nums,val) |