Skip to content

Commit eade80a

Browse files
committedAug 5, 2024·
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
 
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+
```
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+
```

0 commit comments

Comments
 (0)
Please sign in to comment.