Skip to content

Commit bfee3d3

Browse files
authored
Added tasks 6-1143
1 parent 28c15b4 commit bfee3d3

File tree

25 files changed

+1561
-1
lines changed

25 files changed

+1561
-1
lines changed

README.md

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

src/main/scala/g0001_0100/s0002_add_two_numbers/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ You may assume the two numbers do not contain any leading zero, except the numbe
4242
```scala
4343
import com_github_leetcode.ListNode
4444

45-
4645
/*
4746
* Definition for singly-linked list.
4847
* class ListNode(_x: Int = 0, _next: ListNode = null) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Scala/LeetCode-in-Scala?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Scala/LeetCode-in-Scala?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala/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+
```scala
47+
import scala.collection.mutable
48+
49+
object Solution {
50+
def convert(s: String, numRows: Int): String = {
51+
if (numRows == 1) return s
52+
val map = new mutable.HashMap[Int, String]()
53+
var string = ""
54+
var counter = 0
55+
var counterBool = true
56+
for (i <- s) {
57+
val currentValue = map.getOrElse(counter, "")
58+
map(counter) = currentValue + i.toString
59+
counterBool = if (counter == numRows - 1) false else if (counter == 0) true else counterBool
60+
counter = if (counterBool) counter + 1 else counter - 1
61+
}
62+
map.foreach {
63+
case (_, value) => string += value
64+
}
65+
string
66+
}
67+
}
68+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Scala/LeetCode-in-Scala?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Scala/LeetCode-in-Scala?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala/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+
```scala
43+
object Solution {
44+
def reverse(x: Int): Int = {
45+
var rev: Long = 0
46+
var remaining = x
47+
while (remaining != 0) {
48+
rev = (rev * 10) + (remaining % 10)
49+
remaining /= 10
50+
}
51+
if (rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE) {
52+
return 0
53+
}
54+
rev.toInt
55+
}
56+
}
57+
```
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Scala/LeetCode-in-Scala?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Scala/LeetCode-in-Scala?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala/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+
```scala
121+
object Solution {
122+
@SuppressWarnings(Array("scala:S3776"))
123+
def myAtoi(str: String): Int = {
124+
if (str == null || str.isEmpty) {
125+
return 0
126+
}
127+
128+
var i = 0
129+
var negativeSign = false
130+
val input = str.toCharArray
131+
132+
while (i < input.length && input(i) == ' ') {
133+
i += 1
134+
}
135+
136+
if (i == input.length) {
137+
return 0
138+
} else if (input(i) == '+') {
139+
i += 1
140+
} else if (input(i) == '-') {
141+
i += 1
142+
negativeSign = true
143+
}
144+
145+
var num = 0
146+
while (i < input.length && input(i) <= '9' && input(i) >= '0') {
147+
// current char
148+
val temp = input(i) - '0'
149+
val tempValue = if (negativeSign) -temp else temp
150+
151+
// avoid invalid numbers like 038
152+
if (num == 0 && tempValue == 0) {
153+
i += 1
154+
} else if (num == Integer.MIN_VALUE / 10 && tempValue <= -8 || num < Integer.MIN_VALUE / 10) {
155+
return Integer.MIN_VALUE
156+
} else if (num == Integer.MAX_VALUE / 10 && tempValue >= 7 || num > Integer.MAX_VALUE / 10) {
157+
return Integer.MAX_VALUE
158+
} else {
159+
num = num * 10 + tempValue
160+
i += 1
161+
}
162+
}
163+
164+
num
165+
}
166+
}
167+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Scala/LeetCode-in-Scala?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Scala/LeetCode-in-Scala?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Scala/LeetCode-in-Scala/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+
```scala
49+
object Solution {
50+
def isPalindrome(x: Int): Boolean = {
51+
if (x < 0) {
52+
false
53+
} else {
54+
var rev = 0
55+
var localX = x
56+
while (localX > 0) {
57+
rev *= 10
58+
rev += localX % 10
59+
localX /= 10
60+
}
61+
rev == x
62+
}
63+
}
64+
}
65+
```

src/main/scala/g0101_0200/s0148_sort_list/readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ Given the `head` of a linked list, return _the list after sorting it in **ascend
4141
```scala
4242
import com_github_leetcode.ListNode
4343

44+
/*
45+
* Definition for singly-linked list.
46+
* class ListNode(_x: Int = 0, _next: ListNode = null) {
47+
* var next: ListNode = _next
48+
* var x: Int = _x
49+
* }
50+
*/
4451
object Solution {
4552
def sortList(head: ListNode): ListNode = {
4653
if (head == null || head.next == null) {

0 commit comments

Comments
 (0)