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
Leetcode 1: two sum | |
# @param {Integer[]} nums | |
# @param {Integer} target | |
# @return {Integer[]} | |
def two_sum(nums, target) | |
temp_hash = {} | |
nums.each_with_index do |i, ind| | |
return [temp_hash[i], ind] if temp_hash[i] | |
temp_hash[target - i] = ind |
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
Leetcode 2: Add two numbers linkedlist | |
# Definition for singly-linked list. | |
# class ListNode | |
# attr_accessor :val, :next | |
# def initialize(val = 0, _next = nil) | |
# @val = val | |
# @next = _next | |
# end | |
# 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
Leetcode 3: Longest substring without repeating character | |
# @param {String} s | |
# @return {Integer} | |
def length_of_longest_substring(s) | |
return 0 if s == "" || s == nil | |
arr = s.split("") | |
curr_count = 1 | |
max_len = 1 | |
curr_arr = [arr[0]] |
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
Leetcode 4: Median of two sorted array | |
# @param {Integer[]} nums1 | |
# @param {Integer[]} nums2 | |
# @return {Float} | |
def find_median_sorted_arrays(nums1, nums2) | |
res=[] | |
a, b, i = 0, 0, 0 | |
sum_m_n = nums1.count + nums2.count | |
if sum_m_n%2 == 0 #even |
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
Leetcode 7 reverse integer atoi | |
# @param {Integer} x | |
# @return {Integer} | |
def reverse(x) | |
o_num = if x < 0 | |
"-#{x.to_s.split("")[1..-1].join("").reverse}".to_i | |
else | |
x.to_s.reverse.to_i | |
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
Leetcode 8: String to integer atoi | |
# @param {String} s | |
# @return {Integer} | |
def my_atoi(s) | |
return nil if s == nil | |
r_n = s.gsub(/[^0-9-]/, "").to_i | |
max_n = (1 << 31) - 1 | |
r_n > max_n || r_n < (max_n * -1) ? 0 : r_n | |
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
Leetcode 9: palindrom number | |
# @param {Integer} x | |
# @return {Boolean} | |
def is_palindrome(x) | |
str = x.to_s | |
str == str.reverse | |
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
Leetcode 11: Container with most water storage | |
# @param {Integer[]} height | |
# @return {Integer} | |
def max_area(height) | |
i = 0 | |
j = height.length - 1 | |
max_r = 0 | |
while i < j do | |
if height[i] < height[j] |
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
Leetcode 14: Longest common prefix problem | |
# @param {String[]} strs | |
# @return {String} | |
# def longest_common_prefix(strs) | |
# smallest = strs.min {|x,y| x.length <=> y.length} | |
# #smallest = strs[0] | |
# has_to_cmp = {} | |
# smallest.split("").each_with_index {|char_x, ind| has_to_cmp["#{char_x}"] ? has_to_cmp["#{char_x}"] << ind : has_to_cmp["#{char_x}"] = [ind]} | |
# # will generate {'f': [0], 'l': [1], ...} |
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
Leetcode : 17 Letter combination of phone number | |
# @param {String} digits | |
# @return {String[]} | |
def letter_combinations(digits) | |
return [] if digits.length == 0 | |
hash_phone = { | |
'2': ['a', 'b', 'c'], | |
'3': ['d', 'e', 'f'], | |
'4': ['g', 'h', 'i'], |