Skip to content

Commit 6dd36d5

Browse files
authored
Updated readme
1 parent 3f6f597 commit 6dd36d5

File tree

11 files changed

+2001
-2
lines changed

11 files changed

+2001
-2
lines changed

README.md

Lines changed: 1227 additions & 2 deletions
Large diffs are not rendered by default.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 1\. Two Sum
5+
6+
Easy
7+
8+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
9+
10+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
11+
12+
You can return the answer in any order.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,7,11,15], target = 9
17+
18+
**Output:** [0,1]
19+
20+
**Explanation:** Because nums[0] + nums[1] == 9, we return [0, 1].
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [3,2,4], target = 6
25+
26+
**Output:** [1,2]
27+
28+
**Example 3:**
29+
30+
**Input:** nums = [3,3], target = 6
31+
32+
**Output:** [0,1]
33+
34+
**Constraints:**
35+
36+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
37+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
38+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
39+
* **Only one valid answer exists.**
40+
41+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code> time complexity?
42+
43+
## Solution
44+
45+
```ruby
46+
# @param {Integer[]} nums
47+
# @param {Integer} target
48+
# @return {Integer[]}
49+
def two_sum(nums, target)
50+
dict = {}
51+
nums.each_with_index do |n, i|
52+
if dict[target - n]
53+
return dict[target - n], i
54+
end
55+
dict[n] = i
56+
end
57+
[-1, -1]
58+
end
59+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 2\. Add Two Numbers
5+
6+
Medium
7+
8+
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
9+
10+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
15+
16+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
17+
18+
**Output:** [7,0,8]
19+
20+
**Explanation:** 342 + 465 = 807.
21+
22+
**Example 2:**
23+
24+
**Input:** l1 = [0], l2 = [0]
25+
26+
**Output:** [0]
27+
28+
**Example 3:**
29+
30+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
31+
32+
**Output:** [8,9,9,9,0,0,0,1]
33+
34+
**Constraints:**
35+
36+
* The number of nodes in each linked list is in the range `[1, 100]`.
37+
* `0 <= Node.val <= 9`
38+
* It is guaranteed that the list represents a number that does not have leading zeros.
39+
40+
## Solution
41+
42+
```ruby
43+
require_relative '../../com_github_leetcode/list_node'
44+
45+
# Definition for singly-linked list.
46+
# class ListNode
47+
# attr_accessor :val, :next
48+
# def initialize(val = 0, _next = nil)
49+
# @val = val
50+
# @next = _next
51+
# end
52+
# end
53+
# @param {ListNode} l1
54+
# @param {ListNode} l2
55+
# @return {ListNode}
56+
def add_two_numbers(l1, l2)
57+
dummy_head = ListNode.new(0)
58+
p = l1
59+
q = l2
60+
curr = dummy_head
61+
carry = 0
62+
63+
while p || q
64+
x = p ? p.val : 0
65+
y = q ? q.val : 0
66+
sum = carry + x + y
67+
carry = sum / 10
68+
curr.next = ListNode.new(sum % 10)
69+
curr = curr.next
70+
71+
p = p.next if p
72+
q = q.next if q
73+
end
74+
75+
curr.next = ListNode.new(carry) if carry > 0
76+
77+
dummy_head.next
78+
end
79+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 3\. Longest Substring Without Repeating Characters
5+
6+
Medium
7+
8+
Given a string `s`, find the length of the **longest substring** without repeating characters.
9+
10+
**Example 1:**
11+
12+
**Input:** s = "abcabcbb"
13+
14+
**Output:** 3
15+
16+
**Explanation:** The answer is "abc", with the length of 3.
17+
18+
**Example 2:**
19+
20+
**Input:** s = "bbbbb"
21+
22+
**Output:** 1
23+
24+
**Explanation:** The answer is "b", with the length of 1.
25+
26+
**Example 3:**
27+
28+
**Input:** s = "pwwkew"
29+
30+
**Output:** 3
31+
32+
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
33+
34+
**Example 4:**
35+
36+
**Input:** s = ""
37+
38+
**Output:** 0
39+
40+
**Constraints:**
41+
42+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
43+
* `s` consists of English letters, digits, symbols and spaces.
44+
45+
## Solution
46+
47+
```ruby
48+
# #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
49+
50+
# @param {String} s
51+
# @return {Integer}
52+
def length_of_longest_substring(s)
53+
last_indices = Array.new(256, -1)
54+
max_len = 0
55+
cur_len = 0
56+
start = 0
57+
58+
s.each_char.with_index do |cur, i|
59+
cur = cur.ord
60+
if last_indices[cur] < start
61+
last_indices[cur] = i
62+
cur_len += 1
63+
else
64+
last_index = last_indices[cur]
65+
start = last_index + 1
66+
cur_len = i - start + 1
67+
last_indices[cur] = i
68+
end
69+
70+
max_len = cur_len if cur_len > max_len
71+
end
72+
73+
max_len
74+
end
75+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 4\. Median of Two Sorted Arrays
5+
6+
Hard
7+
8+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
9+
10+
The overall run time complexity should be `O(log (m+n))`.
11+
12+
**Example 1:**
13+
14+
**Input:** nums1 = [1,3], nums2 = [2]
15+
16+
**Output:** 2.00000
17+
18+
**Explanation:** merged array = [1,2,3] and median is 2.
19+
20+
**Example 2:**
21+
22+
**Input:** nums1 = [1,2], nums2 = [3,4]
23+
24+
**Output:** 2.50000
25+
26+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
27+
28+
**Example 3:**
29+
30+
**Input:** nums1 = [0,0], nums2 = [0,0]
31+
32+
**Output:** 0.00000
33+
34+
**Example 4:**
35+
36+
**Input:** nums1 = [], nums2 = [1]
37+
38+
**Output:** 1.00000
39+
40+
**Example 5:**
41+
42+
**Input:** nums1 = [2], nums2 = []
43+
44+
**Output:** 2.00000
45+
46+
**Constraints:**
47+
48+
* `nums1.length == m`
49+
* `nums2.length == n`
50+
* `0 <= m <= 1000`
51+
* `0 <= n <= 1000`
52+
* `1 <= m + n <= 2000`
53+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
54+
55+
## Solution
56+
57+
```ruby
58+
# @param {Integer[]} nums1
59+
# @param {Integer[]} nums2
60+
# @return {Float}
61+
def find_median_sorted_arrays(nums1, nums2)
62+
combined = nums1.concat(nums2)
63+
combined = combined.sort
64+
var4 = 0
65+
if combined.length.odd?
66+
var1 = ((combined.length).div(2))
67+
result = combined[var1]
68+
end
69+
if combined.length.even?
70+
var2 = ((combined.length).div(2))
71+
var3 = (var2) - 1
72+
var4 = (combined[var2]) + (combined[var3])
73+
result = var4 / (2.to_f)
74+
end
75+
result
76+
end
77+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 5\. Longest Palindromic Substring
5+
6+
Medium
7+
8+
Given a string `s`, return _the longest palindromic substring_ in `s`.
9+
10+
**Example 1:**
11+
12+
**Input:** s = "babad"
13+
14+
**Output:** "bab" **Note:** "aba" is also a valid answer.
15+
16+
**Example 2:**
17+
18+
**Input:** s = "cbbd"
19+
20+
**Output:** "bb"
21+
22+
**Example 3:**
23+
24+
**Input:** s = "a"
25+
26+
**Output:** "a"
27+
28+
**Example 4:**
29+
30+
**Input:** s = "ac"
31+
32+
**Output:** "a"
33+
34+
**Constraints:**
35+
36+
* `1 <= s.length <= 1000`
37+
* `s` consist of only digits and English letters.
38+
39+
## Solution
40+
41+
```ruby
42+
# @param {String} s
43+
# @return {String}
44+
def longest_palindrome(s)
45+
new_str = Array.new(s.length * 2 + 1, 0)
46+
new_str[0] = '#'
47+
48+
(0...s.length).each do |i|
49+
new_str[2 * i + 1] = s[i]
50+
new_str[2 * i + 2] = '#'
51+
end
52+
53+
dp = Array.new(new_str.length, 0)
54+
friend_center = 0
55+
friend_radius = 0
56+
lps_center = 0
57+
lps_radius = 0
58+
59+
(0...new_str.length).each do |i|
60+
dp[i] = friend_center + friend_radius > i ? [dp[2 * friend_center - i], friend_center + friend_radius - i].min : 1
61+
62+
while i + dp[i] < new_str.length && i - dp[i] >= 0 && new_str[i + dp[i]] == new_str[i - dp[i]]
63+
dp[i] += 1
64+
end
65+
66+
if friend_center + friend_radius < i + dp[i]
67+
friend_center = i
68+
friend_radius = dp[i]
69+
end
70+
71+
lps_center, lps_radius = i, dp[i] if lps_radius < dp[i]
72+
end
73+
74+
s[(lps_center - lps_radius + 1) / 2, lps_radius - 1]
75+
end
76+
```

0 commit comments

Comments
 (0)