Skip to content

Commit 4aae924

Browse files
committed
Added tasks 6-1143
1 parent 7def8fa commit 4aae924

File tree

79 files changed

+5514
-79
lines changed

Some content is hidden

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

79 files changed

+5514
-79
lines changed

Diff for: README.md

+317-79
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Elixir/LeetCode-in-Elixir?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Elixir/LeetCode-in-Elixir)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Elixir/LeetCode-in-Elixir?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Elixir/LeetCode-in-Elixir/fork)
3+
4+
## 6\. Zigzag Conversion
5+
6+
Medium
7+
8+
The string `"PAYPALISHIRING"` is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
9+
10+
P A H N A P L S I I G Y I R
11+
12+
And then read line by line: `"PAHNAPLSIIGYIR"`
13+
14+
Write the code that will take a string and make this conversion given a number of rows:
15+
16+
string convert(string s, int numRows);
17+
18+
**Example 1:**
19+
20+
**Input:** s = "PAYPALISHIRING", numRows = 3
21+
22+
**Output:** "PAHNAPLSIIGYIR"
23+
24+
**Example 2:**
25+
26+
**Input:** s = "PAYPALISHIRING", numRows = 4
27+
28+
**Output:** "PINALSIGYAHRPI"
29+
30+
**Explanation:** P I N A L S I G Y A H R P I
31+
32+
**Example 3:**
33+
34+
**Input:** s = "A", numRows = 1
35+
36+
**Output:** "A"
37+
38+
**Constraints:**
39+
40+
* `1 <= s.length <= 1000`
41+
* `s` consists of English letters (lower-case and upper-case), `','` and `'.'`.
42+
* `1 <= numRows <= 1000`
43+
44+
## Solution
45+
46+
```elixir
47+
defmodule Solution do
48+
@spec convert(s :: String.t, num_rows :: integer) :: String.t
49+
def convert(s, 1), do: s
50+
def convert(s, num_rows) do
51+
String.codepoints(s)
52+
|> read(%{}, 0, 1, num_rows)
53+
|> then(fn map ->
54+
Enum.flat_map(0..num_rows - 1, fn i ->
55+
Map.get(map, i, [])
56+
|> Enum.reverse()
57+
end)
58+
end)
59+
|> to_string()
60+
end
61+
62+
defp read([], map, _, _, _), do: map
63+
defp read(s, map, -1, _, num_rows) do
64+
read(s, map, 1, 1, num_rows)
65+
end
66+
defp read(s, map, num_rows, _, num_rows) do
67+
read(s, map, num_rows - 2, -1, num_rows)
68+
end
69+
defp read([ch | s], map, i, add, num_rows) do
70+
map = Map.update(map, i, [ch], &([ch | &1]))
71+
read(s, map, i + add, add, num_rows)
72+
end
73+
end
74+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Elixir/LeetCode-in-Elixir?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Elixir/LeetCode-in-Elixir)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Elixir/LeetCode-in-Elixir?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Elixir/LeetCode-in-Elixir/fork)
3+
4+
## 7\. Reverse Integer
5+
6+
Medium
7+
8+
Given a signed 32-bit integer `x`, return `x` _with its digits reversed_. If reversing `x` causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return `0`.
9+
10+
**Assume the environment does not allow you to store 64-bit integers (signed or unsigned).**
11+
12+
**Example 1:**
13+
14+
**Input:** x = 123
15+
16+
**Output:** 321
17+
18+
**Example 2:**
19+
20+
**Input:** x = -123
21+
22+
**Output:** -321
23+
24+
**Example 3:**
25+
26+
**Input:** x = 120
27+
28+
**Output:** 21
29+
30+
**Example 4:**
31+
32+
**Input:** x = 0
33+
34+
**Output:** 0
35+
36+
**Constraints:**
37+
38+
* <code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code>
39+
40+
## Solution
41+
42+
```elixir
43+
defmodule Solution do
44+
@spec reverse(x :: integer) :: integer
45+
def reverse(x) do
46+
rev = reverse_digits(x, 0)
47+
if rev > :math.pow(2, 31) - 1 or rev < -(:math.pow(2, 31)) do
48+
0
49+
else
50+
rev
51+
end
52+
end
53+
54+
defp reverse_digits(0, acc), do: acc
55+
56+
defp reverse_digits(x, acc) do
57+
new_acc = (acc * 10) + rem(x, 10)
58+
reverse_digits(div(x, 10), new_acc)
59+
end
60+
end
61+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Elixir/LeetCode-in-Elixir?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Elixir/LeetCode-in-Elixir)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Elixir/LeetCode-in-Elixir?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Elixir/LeetCode-in-Elixir/fork)
3+
4+
## 8\. String to Integer (atoi)
5+
6+
Medium
7+
8+
Implement the `myAtoi(string s)` function, which converts a string to a 32-bit signed integer (similar to C/C++'s `atoi` function).
9+
10+
The algorithm for `myAtoi(string s)` is as follows:
11+
12+
1. Read in and ignore any leading whitespace.
13+
2. Check if the next character (if not already at the end of the string) is `'-'` or `'+'`. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.
14+
3. Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.
15+
4. Convert these digits into an integer (i.e. `"123" -> 123`, `"0032" -> 32`). If no digits were read, then the integer is `0`. Change the sign as necessary (from step 2).
16+
5. If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then clamp the integer so that it remains in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be clamped to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be clamped to <code>2<sup>31</sup> - 1</code>.
17+
6. Return the integer as the final result.
18+
19+
**Note:**
20+
21+
* Only the space character `' '` is considered a whitespace character.
22+
* **Do not ignore** any characters other than the leading whitespace or the rest of the string after the digits.
23+
24+
**Example 1:**
25+
26+
**Input:** s = "42"
27+
28+
**Output:** 42
29+
30+
**Explanation:** The underlined characters are what is read in, the caret is the current reader position.
31+
32+
Step 1: "42" (no characters read because there is no leading whitespace)
33+
^
34+
Step 2: "42" (no characters read because there is neither a '-' nor '+')
35+
^
36+
Step 3: "42" ("42" is read in)
37+
^
38+
39+
The parsed integer is 42. Since 42 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 42.
40+
41+
**Example 2:**
42+
43+
**Input:** s = " -42"
44+
45+
**Output:** -42
46+
47+
**Explanation:**
48+
49+
Step 1: " -42" (leading whitespace is read and ignored)
50+
^
51+
Step 2: " -42" ('-' is read, so the result should be negative)
52+
^
53+
Step 3: " -42" ("42" is read in)
54+
^
55+
The parsed integer is -42.
56+
57+
Since -42 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is -42.
58+
59+
**Example 3:**
60+
61+
**Input:** s = "4193 with words"
62+
63+
**Output:** 4193
64+
65+
**Explanation:**
66+
67+
Step 1: "4193 with words" (no characters read because there is no leading whitespace)
68+
^
69+
Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+')
70+
^
71+
Step 3: "4193 with words" ("4193" is read in; reading stops because the next character is a non-digit)
72+
^
73+
The parsed integer is 4193.
74+
75+
Since 4193 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 4193.
76+
77+
**Example 4:**
78+
79+
**Input:** s = "words and 987"
80+
81+
**Output:** 0
82+
83+
**Explanation:**
84+
85+
Step 1: "words and 987" (no characters read because there is no leading whitespace)
86+
^
87+
Step 2: "words and 987" (no characters read because there is neither a '-' nor '+')
88+
^
89+
Step 3: "words and 987" (reading stops immediately because there is a non-digit 'w')
90+
^
91+
The parsed integer is 0 because no digits were read.
92+
93+
Since 0 is in the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is 0.
94+
95+
**Example 5:**
96+
97+
**Input:** s = "-91283472332"
98+
99+
**Output:** -2147483648
100+
101+
**Explanation:**
102+
103+
Step 1: "-91283472332" (no characters read because there is no leading whitespace)
104+
^
105+
Step 2: "-91283472332" ('-' is read, so the result should be negative)
106+
^
107+
Step 3: "-91283472332" ("91283472332" is read in)
108+
^
109+
The parsed integer is -91283472332.
110+
111+
Since -91283472332 is less than the lower bound of the range [-2<sup>31</sup>, 2<sup>31</sup> - 1], the final result is clamped to -2<sup>31</sup> = -2147483648.
112+
113+
**Constraints:**
114+
115+
* `0 <= s.length <= 200`
116+
* `s` consists of English letters (lower-case and upper-case), digits (`0-9`), `' '`, `'+'`, `'-'`, and `'.'`.
117+
118+
## Solution
119+
120+
```elixir
121+
defmodule Solution do
122+
@spec my_atoi(s :: String.t) :: integer
123+
def my_atoi(s) do
124+
result = s
125+
|> String.trim
126+
|> String.split
127+
|> List.first
128+
129+
if result != nil and Integer.parse(result) != :error do
130+
{num,_} = Integer.parse(result)
131+
cond do
132+
num < -2 ** 31 -> -2 ** 31
133+
num > 2 ** 31 - 1 -> 2 ** 31 - 1
134+
true -> num
135+
end
136+
else
137+
0
138+
end
139+
end
140+
141+
end
142+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Elixir/LeetCode-in-Elixir?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Elixir/LeetCode-in-Elixir)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Elixir/LeetCode-in-Elixir?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Elixir/LeetCode-in-Elixir/fork)
3+
4+
## 9\. Palindrome Number
5+
6+
Easy
7+
8+
Given an integer `x`, return `true` if `x` is palindrome integer.
9+
10+
An integer is a **palindrome** when it reads the same backward as forward. For example, `121` is palindrome while `123` is not.
11+
12+
**Example 1:**
13+
14+
**Input:** x = 121
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
**Input:** x = -121
21+
22+
**Output:** false
23+
24+
**Explanation:** From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
25+
26+
**Example 3:**
27+
28+
**Input:** x = 10
29+
30+
**Output:** false
31+
32+
**Explanation:** Reads 01 from right to left. Therefore it is not a palindrome.
33+
34+
**Example 4:**
35+
36+
**Input:** x = -101
37+
38+
**Output:** false
39+
40+
**Constraints:**
41+
42+
* <code>-2<sup>31</sup> <= x <= 2<sup>31</sup> - 1</code>
43+
44+
**Follow up:** Could you solve it without converting the integer to a string?
45+
46+
## Solution
47+
48+
```elixir
49+
defmodule Solution do
50+
@spec is_palindrome(x :: integer) :: boolean
51+
def is_palindrome(x) when x < 0, do: false
52+
def is_palindrome(x) when x < 10, do: true
53+
def is_palindrome(x) do
54+
reversed = x
55+
|> Integer.digits
56+
|> Enum.reverse
57+
|> Integer.undigits
58+
59+
x == reversed
60+
end
61+
end
62+
```

0 commit comments

Comments
 (0)