Skip to content

Commit ce0198f

Browse files
committed
Improved tasks 1, 3, 4, 3136, 3612
1 parent d58787e commit ce0198f

File tree

6 files changed

+94
-156
lines changed

6 files changed

+94
-156
lines changed

README.md

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

src/main/kotlin/g0001_0100/s0001_two_sum/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ You can return the answer in any order.
4545
```kotlin
4646
class Solution {
4747
fun twoSum(numbers: IntArray, target: Int): IntArray {
48-
val indexMap: MutableMap<Int, Int> = HashMap()
48+
val indexMap = HashMap<Int, Int>()
4949
for (i in numbers.indices) {
5050
val requiredNum = target - numbers[i]
5151
if (indexMap.containsKey(requiredNum)) {

src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,26 @@ Given a string `s`, find the length of the **longest substring** without repeati
4747
```kotlin
4848
class Solution {
4949
fun lengthOfLongestSubstring(s: String): Int {
50-
var i = 0
51-
var j = 0
52-
var longest = 0
53-
// 1. if string empty, return 0
54-
if (s.isEmpty()) {
55-
return 0
56-
}
57-
while (j < s.length) {
58-
// 2. if the char at index j already seen, update the longest if needs
59-
if (i != j && s.substring(i, j).indexOf(s[j]) > -1) {
60-
longest = Math.max(j - i, longest)
61-
i++
50+
val lastIndices = IntArray(256) { -1 }
51+
var maxLen = 0
52+
var curLen = 0
53+
var start = 0
54+
for (i in s.indices) {
55+
val cur = s[i]
56+
if (lastIndices[cur.code] < start) {
57+
lastIndices[cur.code] = i
58+
curLen++
6259
} else {
63-
// 3. j out of bound already, update longest
64-
if (++j == s.length) {
65-
longest = Math.max(s.length - i, longest)
66-
break
67-
}
60+
val lastIndex = lastIndices[cur.code]
61+
start = lastIndex + 1
62+
curLen = i - start + 1
63+
lastIndices[cur.code] = i
64+
}
65+
if (curLen > maxLen) {
66+
maxLen = curLen
6867
}
6968
}
70-
return longest
69+
return maxLen
7170
}
7271
}
7372
```

src/main/kotlin/g0001_0100/s0004_median_of_two_sorted_arrays/readme.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,38 @@ The overall run time complexity should be `O(log (m+n))`.
5555
## Solution
5656

5757
```kotlin
58+
import kotlin.math.max
59+
import kotlin.math.min
60+
5861
class Solution {
5962
fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double {
60-
val l: MutableList<Int> = ArrayList()
61-
val f: Double
62-
for (j in nums1) {
63-
l.add(j)
64-
}
65-
for (i in nums2) {
66-
l.add(i)
63+
if (nums2.size < nums1.size) {
64+
return findMedianSortedArrays(nums2, nums1)
6765
}
68-
l.sort()
69-
val k = l.size
70-
f = if (k % 2 == 0) {
71-
(l[k / 2 - 1] + l[k / 2]).toDouble() / 2
72-
} else {
73-
l[(k + 1) / 2 - 1].toDouble()
66+
val n1 = nums1.size
67+
val n2 = nums2.size
68+
var low = 0
69+
var high = n1
70+
while (low <= high) {
71+
val cut1 = (low + high) / 2
72+
val cut2 = ((n1 + n2 + 1) / 2) - cut1
73+
val l1 = if (cut1 == 0) Int.MIN_VALUE else nums1[cut1 - 1]
74+
val l2 = if (cut2 == 0) Int.MIN_VALUE else nums2[cut2 - 1]
75+
val r1 = if (cut1 == n1) Int.MAX_VALUE else nums1[cut1]
76+
val r2 = if (cut2 == n2) Int.MAX_VALUE else nums2[cut2]
77+
if (l1 <= r2 && l2 <= r1) {
78+
return if ((n1 + n2) % 2 == 0) {
79+
(max(l1, l2).toDouble() + min(r1, r2).toDouble()) / 2.0
80+
} else {
81+
max(l1, l2).toDouble()
82+
}
83+
} else if (l1 > r2) {
84+
high = cut1 - 1
85+
} else {
86+
low = cut1 + 1
87+
}
7488
}
75-
return f
89+
return 0.0
7690
}
7791
}
7892
```

src/main/kotlin/g3101_3200/s3136_valid_word/readme.md

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,21 @@ class Solution {
6464
if (word.length < 3) {
6565
return false
6666
}
67-
if (word.contains("@") || word.contains("#") || word.contains("$")) {
68-
return false
69-
}
70-
val vowels = charArrayOf('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U')
71-
val consonants = charArrayOf(
72-
'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v',
73-
'w', 'x', 'y', 'z', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q',
74-
'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z',
75-
)
76-
var flag1 = false
77-
var flag2 = false
78-
for (c in vowels) {
79-
if (word.indexOf(c) != -1) {
80-
flag1 = true
81-
break
82-
}
83-
}
84-
for (c in consonants) {
85-
if (word.indexOf(c) != -1) {
86-
flag2 = true
87-
break
67+
var hasVowel = false
68+
var hasConsonant = false
69+
for (c in word.toCharArray()) {
70+
if (Character.isLetter(c)) {
71+
val ch = c.lowercaseChar()
72+
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
73+
hasVowel = true
74+
} else {
75+
hasConsonant = true
76+
}
77+
} else if (!Character.isDigit(c)) {
78+
return false
8879
}
8980
}
90-
return flag1 && flag2
81+
return hasVowel && hasConsonant
9182
}
9283
}
9384
```

src/main/kotlin/g3601_3700/s3612_process_string_with_special_operations_i/readme.md

Lines changed: 12 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -24,53 +24,13 @@ Return the final string `result` after processing all characters in `s`.
2424

2525
**Explanation:**
2626

27-
`i`
28-
29-
`s[i]`
30-
31-
Operation
32-
33-
Current `result`
34-
35-
0
36-
37-
`'a'`
38-
39-
Append `'a'`
40-
41-
`"a"`
42-
43-
1
44-
45-
`'#'`
46-
47-
Duplicate `result`
48-
49-
`"aa"`
50-
51-
2
52-
53-
`'b'`
54-
55-
Append `'b'`
56-
57-
`"aab"`
58-
59-
3
60-
61-
`'%'`
62-
63-
Reverse `result`
64-
65-
`"baa"`
66-
67-
4
68-
69-
`'*'`
70-
71-
Remove the last character
72-
73-
`"ba"`
27+
| i | s[i] | Operation | Current `result` |
28+
|---|-------|----------------------------|------------------|
29+
| 0 | `'a'` | Append `'a'` | `"a"` |
30+
| 1 | `'#'` | Duplicate `result` | `"aa"` |
31+
| 2 | `'b'` | Append `'b'` | `"aab"` |
32+
| 3 | `'%'` | Reverse `result` | `"baa"` |
33+
| 4 | `'*'` | Remove the last character | `"ba"` |
7434

7535
Thus, the final `result` is `"ba"`.
7636

@@ -82,37 +42,11 @@ Thus, the final `result` is `"ba"`.
8242

8343
**Explanation:**
8444

85-
`i`
86-
87-
`s[i]`
88-
89-
Operation
90-
91-
Current `result`
92-
93-
0
94-
95-
`'z'`
96-
97-
Append `'z'`
98-
99-
`"z"`
100-
101-
1
102-
103-
`'*'`
104-
105-
Remove the last character
106-
107-
`""`
108-
109-
2
110-
111-
`'#'`
112-
113-
Duplicate the string
114-
115-
`""`
45+
| i | s[i] | Operation | Current `result` |
46+
|---|-------|---------------------------|------------------|
47+
| 0 | `'z'` | Append `'z'` | `"z"` |
48+
| 1 | `'*'` | Remove the last character | `""` |
49+
| 2 | `'#'` | Duplicate the string | `""` |
11650

11751
Thus, the final `result` is `""`.
11852

0 commit comments

Comments
 (0)