Skip to content

Commit 4d3a61b

Browse files
committed
Added elixir
1 parent b78e32c commit 4d3a61b

File tree

168 files changed

+5586
-386
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+5586
-386
lines changed

README.md

Lines changed: 385 additions & 385 deletions
Large diffs are not rendered by default.

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>com.github.javadev</groupId>
66
<artifactId>leetcode-in-all</artifactId>
77
<packaging>jar</packaging>
8-
<version>1.1</version>
8+
<version>1.2</version>
99
<name>leetcode-in-all</name>
1010
<description>104 LeetCode algorithm problem solutions</description>
1111
<url>https://github.com/javadev/LeetCode-in-All</url>
@@ -90,6 +90,7 @@
9090
<source>src/main/ts</source>
9191
<source>src/main/python</source>
9292
<source>src/main/swift</source>
93+
<source>src/main/elixir</source>
9394
</sources>
9495
</configuration>
9596
</execution>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
2+
# #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Big_O_Time_O(n)_Space_O(n)
3+
# #2024_07_06_Time_261_ms_(94.89%)_Space_77.3_MB_(46.59%)
4+
5+
defmodule Solution do
6+
@spec two_sum(nums :: [integer], target :: integer) :: [integer]
7+
def two_sum(nums, target) do
8+
nums
9+
|> Enum.with_index()
10+
|> Enum.reduce_while(%{}, fn {num, index}, map ->
11+
case map[target - num] do
12+
nil -> {:cont, Map.put(map, num, index)}
13+
found -> {:halt, [index, found]}
14+
end
15+
end)
16+
end
17+
end
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1\. Two Sum
2+
3+
Easy
4+
5+
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.
6+
7+
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.
8+
9+
You can return the answer in any order.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [2,7,11,15], target = 9
14+
15+
**Output:** [0,1]
16+
17+
**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [3,2,4], target = 6
22+
23+
**Output:** [1,2]
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [3,3], target = 6
28+
29+
**Output:** [0,1]
30+
31+
**Constraints:**
32+
33+
* <code>2 <= nums.length <= 10<sup>4</sup></code>
34+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
35+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
36+
* **Only one valid answer exists.**
37+
38+
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
2+
# #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
3+
# #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) #2024_07_06_Time_353_ms_(88.68%)_Space_72.6_MB_(9.43%)
4+
5+
# Definition for singly-linked list.
6+
#
7+
# defmodule ListNode do
8+
# @type t :: %__MODULE__{
9+
# val: integer,
10+
# next: ListNode.t() | nil
11+
# }
12+
# defstruct val: 0, next: nil
13+
# end
14+
defmodule Solution do
15+
@spec add_two_numbers(l1 :: ListNode.t | nil, l2 :: ListNode.t | nil) :: ListNode.t | nil
16+
def add_two_numbers(l1, l2) do
17+
add(l1, l2, 0)
18+
end
19+
20+
defp add(nil, nil, 0), do: nil
21+
defp add(l1, l2, carry) do
22+
{val1, next1} = content(l1)
23+
{val2, next2} = content(l2)
24+
sum = val1 + val2 + carry
25+
%ListNode{val: rem(sum, 10), next: add(next1, next2, div(sum, 10))}
26+
end
27+
28+
defp content(nil), do: {0, nil}
29+
defp content(node), do: {node.val, node.next}
30+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2\. Add Two Numbers
2+
3+
Medium
4+
5+
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.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)
12+
13+
**Input:** l1 = [2,4,3], l2 = [5,6,4]
14+
15+
**Output:** [7,0,8]
16+
17+
**Explanation:** 342 + 465 = 807.
18+
19+
**Example 2:**
20+
21+
**Input:** l1 = [0], l2 = [0]
22+
23+
**Output:** [0]
24+
25+
**Example 3:**
26+
27+
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
28+
29+
**Output:** [8,9,9,9,0,0,0,1]
30+
31+
**Constraints:**
32+
33+
* The number of nodes in each linked list is in the range `[1, 100]`.
34+
* `0 <= Node.val <= 9`
35+
* It is guaranteed that the list represents a number that does not have leading zeros.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
2+
# #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
3+
# #Big_O_Time_O(n)_Space_O(1) #2024_07_06_Time_356_ms_(93.33%)_Space_84.7_MB_(8.89%)
4+
5+
defmodule Solution do
6+
@spec length_of_longest_substring(s :: String.t()) :: integer
7+
def length_of_longest_substring(s) do
8+
sublen([], String.codepoints(s), 0, 0, 0)
9+
end
10+
11+
defp sublen(_, [], _, _, m), do: m
12+
13+
defp sublen(left, [next | rest] = right, start, stop, m) do
14+
if Enum.member?(left, next) do
15+
[_ | mid] = left
16+
sublen(mid, right, start + 1, stop, m)
17+
else
18+
stop = stop + 1
19+
sublen(left ++ [next], rest, start, stop, max(m, stop - start))
20+
end
21+
end
22+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
3\. Longest Substring Without Repeating Characters
2+
3+
Medium
4+
5+
Given a string `s`, find the length of the **longest substring** without repeating characters.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "abcabcbb"
10+
11+
**Output:** 3
12+
13+
**Explanation:** The answer is "abc", with the length of 3.
14+
15+
**Example 2:**
16+
17+
**Input:** s = "bbbbb"
18+
19+
**Output:** 1
20+
21+
**Explanation:** The answer is "b", with the length of 1.
22+
23+
**Example 3:**
24+
25+
**Input:** s = "pwwkew"
26+
27+
**Output:** 3
28+
29+
**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.
30+
31+
**Example 4:**
32+
33+
**Input:** s = ""
34+
35+
**Output:** 0
36+
37+
**Constraints:**
38+
39+
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
40+
* `s` consists of English letters, digits, symbols and spaces.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
2+
# #Big_O_Time_O(log(min(N,M)))_Space_O(1) #2024_07_06_Time_474_ms_(86.21%)_Space_75.1_MB_(55.17%)
3+
4+
defmodule Solution do
5+
@spec find_median_sorted_arrays(nums1 :: [integer], nums2 :: [integer]) :: float
6+
def find_median_sorted_arrays(nums1, nums2) do
7+
list = Enum.sort(nums1 ++ nums2)
8+
9+
cond do
10+
length(list) === 1 ->
11+
Enum.at(list, 0)
12+
13+
rem(length(list), 2) === 0 ->
14+
start_index = trunc(length(list) / 2 - 1)
15+
(Enum.at(list, start_index) + Enum.at(list, start_index + 1)) / 2
16+
17+
true ->
18+
start_index = div(length(list), 2)
19+
Enum.at(list, start_index)
20+
end
21+
end
22+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
4\. Median of Two Sorted Arrays
2+
3+
Hard
4+
5+
Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.
6+
7+
The overall run time complexity should be `O(log (m+n))`.
8+
9+
**Example 1:**
10+
11+
**Input:** nums1 = [1,3], nums2 = [2]
12+
13+
**Output:** 2.00000
14+
15+
**Explanation:** merged array = [1,2,3] and median is 2.
16+
17+
**Example 2:**
18+
19+
**Input:** nums1 = [1,2], nums2 = [3,4]
20+
21+
**Output:** 2.50000
22+
23+
**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
24+
25+
**Example 3:**
26+
27+
**Input:** nums1 = [0,0], nums2 = [0,0]
28+
29+
**Output:** 0.00000
30+
31+
**Example 4:**
32+
33+
**Input:** nums1 = [], nums2 = [1]
34+
35+
**Output:** 1.00000
36+
37+
**Example 5:**
38+
39+
**Input:** nums1 = [2], nums2 = []
40+
41+
**Output:** 2.00000
42+
43+
**Constraints:**
44+
45+
* `nums1.length == m`
46+
* `nums2.length == n`
47+
* `0 <= m <= 1000`
48+
* `0 <= n <= 1000`
49+
* `1 <= m + n <= 2000`
50+
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy