Skip to content

Commit 0d064e2

Browse files
committed
Fixed warnings, formatted codes.
1 parent 4dd9e21 commit 0d064e2

File tree

10 files changed

+81
-79
lines changed

10 files changed

+81
-79
lines changed

src/main/kotlin/s0001_two_sum/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Solution {
1010
map[nums[i]] = i
1111
continue
1212
}
13-
if (map[value] !== i) {
13+
if (map[value] != i) {
1414
res[1] = i
1515
res[0] = map[value]!!
1616
break

src/main/kotlin/s0002_add_two_numbers/Solution.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,26 @@ class Solution {
1717
var curr = dummyHead
1818
var carry = 0
1919
while (p != null || q != null) {
20-
val x = if ((p != null)) p.`val` else 0
21-
val y = if ((q != null)) q.`val` else 0
20+
val x = if ((p != null)) {
21+
p.`val`
22+
} else {
23+
0
24+
}
25+
val y = if ((q != null)) {
26+
q.`val`
27+
} else {
28+
0
29+
}
2230
val sum = carry + x + y
2331
carry = sum / 10
2432
curr.next = ListNode(sum % 10)
2533
curr = curr.next!!
26-
if (p != null) p = p.next
27-
if (q != null) q = q.next
34+
if (p != null) {
35+
p = p.next
36+
}
37+
if (q != null) {
38+
q = q.next
39+
}
2840
}
2941
if (carry > 0) {
3042
curr.next = ListNode(carry)

src/main/kotlin/s0003_longest_substring_without_repeating_characters/Solution.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ class Solution {
66
var f = 0
77
val charArray = IntArray(128)
88
for (i in 0 until s.length) {
9-
if (charArray[s[i].toInt()] == 0) {
9+
if (charArray[s[i].code] == 0) {
1010
longest = if (longest < (i + 1 - f)) (i + 1 - f) else longest
11-
charArray[s[i].toInt()] += 1
11+
charArray[s[i].code] += 1
1212
} else {
1313
while (s[f] != s[i]) {
14-
charArray[s[f].toInt()] = 0
14+
charArray[s[f].code] = 0
1515
f++
1616
}
1717
f++

src/main/kotlin/s0004_median_of_two_sorted_arrays/Solution.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class Solution {
1414
q++
1515
}
1616
}
17-
if (array.size % 2 == 0) {
18-
return (array[array.size / 2] + array[(array.size / 2) - 1]).toDouble() / 2
17+
return if (array.size % 2 == 0) {
18+
(array[array.size / 2] + array[(array.size / 2) - 1]).toDouble() / 2
1919
} else {
20-
return array[array.size / 2].toDouble()
20+
array[array.size / 2].toDouble()
2121
}
2222
}
2323
}

src/main/kotlin/s0005_longest_palindromic_substring/Solution.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ class Solution {
55
if (s.isEmpty()) {
66
return s
77
}
8-
val dp = Array(s.length, { BooleanArray(s.length) })
8+
val dp = Array(s.length) { BooleanArray(s.length) }
99
var longestPalindromeStart = 0
1010
var longestPalindromeLength = 1
1111
for (i in s.length - 1 downTo 0) {
1212
// single character is a palindrome so making it true
1313
dp[i][i] = true
1414
for (j in i + 1 until s.length) {
15-
// checking for two edge cases 1.if characters are equal check for diagonal lower left one if true make it true
15+
// checking for two edge cases 1.if characters are equal check for diagonal lower
16+
// left one if true make it true
1617
// 2. or if both equal and its the first elelment in the loop make it as true
1718
if ((s[i] == s[j] && dp[i + 1][j - 1]) || (i + 1 == j && s[i] == s[j])) {
1819
dp[i][j] = true

src/main/kotlin/s0006_zigzag_conversion/Solution.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ class Solution {
1010
var j = 0
1111
while (j + i < n) {
1212
ret.append(s[j + i])
13-
if (i != 0 && i != numRows - 1 && j + cycleLen - i < n)
13+
if (i != 0 && i != numRows - 1 && j + cycleLen - i < n) {
1414
ret.append(s[j + cycleLen - i])
15+
}
1516
j += cycleLen
1617
}
1718
}

src/main/kotlin/s0007_reverse_integer/Solution.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package s0007_reverse_integer
22

33
class Solution {
44
fun reverse(x: Int): Int {
5-
var rev:Long = 0
6-
var localX = x
7-
while (localX != 0)
8-
{
9-
rev = (rev * 10) + (localX % 10)
10-
localX /= 10
11-
}
12-
if (rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE) return 0
13-
return rev.toInt()
5+
var rev: Long = 0
6+
var localX = x
7+
while (localX != 0) {
8+
rev = (rev * 10) + (localX % 10)
9+
localX /= 10
10+
}
11+
if (rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE) {
12+
return 0
13+
}
14+
return rev.toInt()
1415
}
1516
}

src/main/kotlin/s0008_string_to_integer_atoi/Solution.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package s0008_string_to_integer_atoi
22

33
class Solution {
44
fun myAtoi(str: String): Int {
5-
if (str.trim({ it <= ' ' }).isEmpty())
5+
if (str.trim { it <= ' ' }.isEmpty())
66
return 0
7-
var localStr = str.trim({ it <= ' ' })
8-
var i = 0
7+
var localStr = str.trim { it <= ' ' }
8+
var i = 0
99
var ans = 0
1010
var sign = 1
1111
val len = localStr.length
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
package s0010_regular_expression_matching
22

33
class Solution {
4-
fun isMatch(text:String, pattern:String): Boolean {
5-
val dp = Array<BooleanArray>(text.length + 1, {BooleanArray(pattern.length + 1)})
6-
dp[text.length][pattern.length] = true
7-
for (i in text.length downTo 0)
8-
{
9-
for (j in pattern.length - 1 downTo 0)
10-
{
11-
val first_match = ((i < text.length && ((pattern.get(j) == text.get(i) || pattern.get(j) == '.'))))
12-
if (j + 1 < pattern.length && pattern.get(j + 1) == '*')
13-
{
14-
dp[i][j] = dp[i][j + 2] || first_match && dp[i + 1][j]
4+
fun isMatch(text: String, pattern: String): Boolean {
5+
val dp = Array(text.length + 1) { BooleanArray(pattern.length + 1) }
6+
dp[text.length][pattern.length] = true
7+
for (i in text.length downTo 0) {
8+
for (j in pattern.length - 1 downTo 0) {
9+
val firstMatch = ((i < text.length && ((pattern[j] == text[i] || pattern[j] == '.'))))
10+
if (j + 1 < pattern.length && pattern[j + 1] == '*') {
11+
dp[i][j] = dp[i][j + 2] || firstMatch && dp[i + 1][j]
12+
} else {
13+
dp[i][j] = firstMatch && dp[i + 1][j + 1]
14+
}
15+
}
1516
}
16-
else
17-
{
18-
dp[i][j] = first_match && dp[i + 1][j + 1]
19-
}
20-
}
21-
}
22-
return dp[0][0]
23-
17+
return dp[0][0]
2418
}
2519
}
Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,33 @@
11
package s0011_container_with_most_water
22

33
class Solution {
4-
fun maxArea(height:IntArray):Int {
5-
var max = 0
6-
if (height != null && height.size != 0)
7-
{
8-
var left = 0
9-
var right = height.size - 1
10-
while (left < right)
11-
{
12-
// next two if conditions are to make sure there are two non-zero containers to
13-
// hold water. If not move next to find one
14-
if (height[left] == 0)
15-
{
16-
left++
17-
continue
4+
fun maxArea(height: IntArray): Int {
5+
var max = 0
6+
if (height.isNotEmpty()) {
7+
var left = 0
8+
var right = height.size - 1
9+
while (left < right) {
10+
// next two if conditions are to make sure there are two non-zero containers to
11+
// hold water. If not move next to find one
12+
if (height[left] == 0) {
13+
left++
14+
continue
15+
}
16+
if (height[right] == 0) {
17+
right--
18+
continue
19+
}
20+
// if control came here that means left and right containers are non zero and
21+
// can hold water. Get the count of containers and multiple by lower container
22+
if (height[left] < height[right]) {
23+
max = Math.max(max, (right - left) * height[left])
24+
left++
25+
} else {
26+
max = Math.max(max, (right - left) * height[right])
27+
right--
28+
}
29+
}
1830
}
19-
if (height[right] == 0)
20-
{
21-
right--
22-
continue
23-
}
24-
// if control came here that means left and right containers are non zero and
25-
// can hold water. Get the count of containers and multiple by lower container
26-
if (height[left] < height[right])
27-
{
28-
max = Math.max(max, (right - left) * height[left])
29-
left++
30-
}
31-
else
32-
{
33-
max = Math.max(max, (right - left) * height[right])
34-
right--
35-
}
36-
}
31+
return max
3732
}
38-
return max
39-
}
4033
}

0 commit comments

Comments
 (0)