Skip to content

Commit eade80a

Browse files
committed
Added elixir
1 parent 5f718a0 commit eade80a

File tree

83 files changed

+5531
-0
lines changed

Some content is hidden

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

83 files changed

+5531
-0
lines changed
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/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/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+
**Output:** 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+
```elixir
46+
defmodule Solution do
47+
@spec two_sum(nums :: [integer], target :: integer) :: [integer]
48+
def two_sum(nums, target) do
49+
nums
50+
|> Enum.with_index()
51+
|> Enum.reduce_while(%{}, fn {num, index}, map ->
52+
case map[target - num] do
53+
nil -> {:cont, Map.put(map, num, index)}
54+
found -> {:halt, [index, found]}
55+
end
56+
end)
57+
end
58+
end
59+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/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+
```elixir
43+
# Definition for singly-linked list.
44+
#
45+
# defmodule ListNode do
46+
# @type t :: %__MODULE__{
47+
# val: integer,
48+
# next: ListNode.t() | nil
49+
# }
50+
# defstruct val: 0, next: nil
51+
# end
52+
defmodule Solution do
53+
@spec add_two_numbers(l1 :: ListNode.t | nil, l2 :: ListNode.t | nil) :: ListNode.t | nil
54+
def add_two_numbers(l1, l2) do
55+
add(l1, l2, 0)
56+
end
57+
58+
defp add(nil, nil, 0), do: nil
59+
defp add(l1, l2, carry) do
60+
{val1, next1} = content(l1)
61+
{val2, next2} = content(l2)
62+
sum = val1 + val2 + carry
63+
%ListNode{val: rem(sum, 10), next: add(next1, next2, div(sum, 10))}
64+
end
65+
66+
defp content(nil), do: {0, nil}
67+
defp content(node), do: {node.val, node.next}
68+
end
69+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/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+
```elixir
48+
defmodule Solution do
49+
@spec length_of_longest_substring(s :: String.t()) :: integer
50+
def length_of_longest_substring(s) do
51+
sublen([], String.codepoints(s), 0, 0, 0)
52+
end
53+
54+
defp sublen(_, [], _, _, m), do: m
55+
56+
defp sublen(left, [next | rest] = right, start, stop, m) do
57+
if Enum.member?(left, next) do
58+
[_ | mid] = left
59+
sublen(mid, right, start + 1, stop, m)
60+
else
61+
stop = stop + 1
62+
sublen(left ++ [next], rest, start, stop, max(m, stop - start))
63+
end
64+
end
65+
end
66+
```
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/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/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+
```elixir
58+
defmodule Solution do
59+
@spec find_median_sorted_arrays(nums1 :: [integer], nums2 :: [integer]) :: float
60+
def find_median_sorted_arrays(nums1, nums2) do
61+
list = Enum.sort(nums1 ++ nums2)
62+
63+
cond do
64+
length(list) === 1 ->
65+
Enum.at(list, 0)
66+
67+
rem(length(list), 2) === 0 ->
68+
start_index = trunc(length(list) / 2 - 1)
69+
(Enum.at(list, start_index) + Enum.at(list, start_index + 1)) / 2
70+
71+
true ->
72+
start_index = div(length(list), 2)
73+
Enum.at(list, start_index)
74+
end
75+
end
76+
end
77+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-All?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-All)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-All?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-All/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+
```elixir
42+
defmodule Solution do
43+
@spec longest_palindrome(s :: String.t()) :: String.t()
44+
def longest_palindrome(<<>>), do: ""
45+
def longest_palindrome(<<f::binary-size(1)>>), do: f
46+
def longest_palindrome(s) do
47+
{ri,rj} =String.to_charlist(s) |> List.to_tuple |> do_longest_palindrome(0, {0,0})
48+
String.slice(s,ri..rj)
49+
end
50+
51+
defp do_longest_palindrome(s_t,i,{ri,rj}) when i == tuple_size(s_t), do: {ri,rj}
52+
defp do_longest_palindrome(s_t,i,{ri,rj}) do
53+
{ri,rj} = do_longest_palindrome(s_t,i,i,{ri,rj})
54+
{ri,rj} = do_longest_palindrome(s_t,i,i+1,{ri,rj})
55+
do_longest_palindrome(s_t, i+1, {ri, rj})
56+
end
57+
58+
defp do_longest_palindrome(s_t,i,j,{ri,rj}) when (i<0 or j==tuple_size(s_t)) or (elem(s_t,i) != elem(s_t,j)) do
59+
if ((j-1)-(i+1)) > rj-ri, do: {i+1,j-1}, else: {ri,rj}
60+
end
61+
defp do_longest_palindrome(s_t,i,j,{ri,rj}), do: do_longest_palindrome(s_t, i-1, j+1, {ri,rj})
62+
end
63+
```

0 commit comments

Comments
 (0)