Skip to content

Commit 40777a3

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 0933ca5 + 4329c4e commit 40777a3

File tree

7 files changed

+215
-4
lines changed

7 files changed

+215
-4
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
| [021][021-question] | [Merge Two Sorted Lists][021-tips] | [][021-java] | | [][021-kotlin] |
3737
| [026][026-question] | [Remove Duplicates from Sorted Array][026-tips] | [][026-java] | | [][026-kotlin] |
3838
| [027][027-question] | [Remove Element][027-tips] | [][027-java] | | [][027-kotlin] |
39-
| [028][028-question] | [Implement strStr()][028-tips] | [][028-java] | | |
40-
| [035][035-question] | [Search Insert Position][035-tips] | [][035-java] | | |
41-
| [038][038-question] | [Count and Say][038-tips] | [][038-java] | | |
39+
| [028][028-question] | [Implement strStr()][028-tips] | [][028-java] | | [][028-kotlin] |
40+
| [035][035-question] | [Search Insert Position][035-tips] | [][035-java] | | [][035-kotlin] |
41+
| [038][038-question] | [Count and Say][038-tips] | [][038-java] | | [][038-kotlin] |
4242
| [053][053-question] | [Maximum Subarray][053-tips] | [][053-java] | | |
4343
| [058][058-question] | [Length of Last Word][058-tips] | [][058-java] | | |
4444
| [066][066-question] | [Plus One][066-tips] | [][066-java] | | |
@@ -471,4 +471,7 @@
471471
[021-kotlin]: ./src/_021/kotlin/Solution.kt
472472
[026-kotlin]: ./src/_026/kotlin/Solution.kt
473473
[027-kotlin]: ./src/_027/kotlin/Solution.kt
474+
[028-kotlin]: ./src/_028/kotlin/Solution.kt
475+
[035-kotlin]: ./src/_035/kotlin/Solution.kt
476+
[038-kotlin]: ./src/_038/kotlin/Solution.kt
474477
[771-kotlin]: ./src/_771/kotlin/Solution.kt

src/_028/kotlin/Solution.kt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package _028.kotlin
2+
3+
/**
4+
* @author relish
5+
* @since 2018/04/15
6+
*/
7+
class Solution {
8+
fun strStr(haystack: String, needle: String): Int {
9+
10+
if (haystack.isEmpty()) {
11+
return if (needle.isEmpty()) haystack.length else -1
12+
}
13+
if (needle.isEmpty()) {
14+
return 0
15+
}
16+
17+
val first = needle[0]
18+
val max = 0 + (haystack.length - needle.length)
19+
20+
var i = 0 + 0
21+
while (i <= max) {
22+
/* Look for first character. */
23+
if (haystack[i] != first) {
24+
while (++i <= max && haystack[i] != first);
25+
}
26+
27+
/* Found first character, now look at the rest of v2 */
28+
if (i <= max) {
29+
var j = i + 1
30+
val end = j + needle.length - 1
31+
var k = 0 + 1
32+
while (j < end && haystack[j] == needle[k]) {
33+
j++
34+
k++
35+
}
36+
37+
if (j == end) {
38+
/* Found whole string. */
39+
return i - 0
40+
}
41+
}
42+
i++
43+
}
44+
return -1
45+
}
46+
}
47+
48+
fun main(args: Array<String>) {
49+
println(Solution().strStr("hello",""))
50+
}

src/_035/kotlin/Solution.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package _035.kotlin
2+
3+
/**
4+
* @author relish
5+
* @since 2018/04/15
6+
*/
7+
class Solution {
8+
fun searchInsert(nums: IntArray, target: Int): Int {
9+
var left = 0
10+
var right = nums.size - 1
11+
while (left <= right) {
12+
val mid = (left + right) / 2
13+
when {
14+
nums[mid] == target -> return mid
15+
nums[mid] < target -> left = mid + 1
16+
else -> right = mid - 1
17+
}
18+
}
19+
return left
20+
}
21+
}
22+
23+
fun main(args: Array<String>) {
24+
println(Solution().searchInsert(intArrayOf(1,3,5,6), 5))
25+
}

src/_038/kotlin/Solution.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package _038.kotlin
2+
3+
/**
4+
* @author relish
5+
* @since 2018/04/16
6+
*/
7+
class Solution {
8+
fun countAndSay(n: Int): String {
9+
var say = "1"
10+
for (count in 1 until n) {
11+
say = say(say)
12+
}
13+
return say
14+
}
15+
16+
private fun say(say: String): String {
17+
val newSay = StringBuilder()
18+
var prev = say[0]
19+
var tCount = 0
20+
for (i in say.indices) {
21+
if (say[i] == prev) {
22+
tCount++
23+
continue
24+
}
25+
newSay.append(tCount).append(prev)
26+
prev = say[i]
27+
tCount = 1
28+
}
29+
newSay.append(tCount).append(prev)
30+
return newSay.toString()
31+
}
32+
}
33+
34+
fun main(args: Array<String>) {
35+
for (i in 1 until 10) {
36+
println(Solution().countAndSay(i))
37+
}
38+
}

tips/028/README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Tags:** Two Pointers, String
2626
## 思路
2727

2828
题意是从主串中找到子串的索引,如果找不到则返回-1,当子串长度大于主串,直接返回-1,然后我们只需要遍历比较即可。
29-
29+
Java:
3030
```java
3131
class Solution {
3232
public int strStr(String haystack, String needle) {
@@ -42,6 +42,49 @@ class Solution {
4242
}
4343
}
4444
```
45+
kotlin(200ms/87.50%):
46+
```kotlin
47+
class Solution {
48+
fun strStr(haystack: String, needle: String): Int {
49+
50+
if (haystack.isEmpty()) {
51+
return if (needle.isEmpty()) haystack.length else -1
52+
}
53+
if (needle.isEmpty()) {
54+
return 0
55+
}
56+
57+
val first = needle[0]
58+
val max = 0 + (haystack.length - needle.length)
59+
60+
var i = 0 + 0
61+
while (i <= max) {
62+
/* Look for first character. */
63+
if (haystack[i] != first) {
64+
while (++i <= max && haystack[i] != first);
65+
}
66+
67+
/* Found first character, now look at the rest of v2 */
68+
if (i <= max) {
69+
var j = i + 1
70+
val end = j + needle.length - 1
71+
var k = 0 + 1
72+
while (j < end && haystack[j] == needle[k]) {
73+
j++
74+
k++
75+
}
76+
77+
if (j == end) {
78+
/* Found whole string. */
79+
return i - 0
80+
}
81+
}
82+
i++
83+
}
84+
return -1
85+
}
86+
}
87+
```
4588

4689

4790
## 结语

tips/035/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Output: 0
4141

4242
题意是让你从一个没有重复元素的已排序数组中找到插入位置的索引。因为数组已排序,所以我们可以想到二分查找法,因为查找到的条件是找到第一个等于或者大于 `target` 的元素的位置,所以二分法略作变动即可。
4343

44+
Java:
4445
```java
4546
class Solution {
4647
public int searchInsert(int[] nums, int target) {
@@ -55,6 +56,25 @@ class Solution {
5556
}
5657
```
5758

59+
kotlin(216ms/90.00%):
60+
```kotlin
61+
class Solution {
62+
fun searchInsert(nums: IntArray, target: Int): Int {
63+
var left = 0
64+
var right = nums.size - 1
65+
while (left <= right) {
66+
val mid = (left + right) / 2
67+
when {
68+
nums[mid] == target -> return mid
69+
nums[mid] < target -> left = mid + 1
70+
else -> right = mid - 1
71+
}
72+
}
73+
return left
74+
}
75+
}
76+
```
77+
5878

5979
## 结语
6080

tips/038/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Output: "1211"
4343

4444
题意是数和说,根据如下序列 `1, 11, 21, 1211, 111221, ...`,求第 n 个数,规则很简单,就是数和说,数就是数这个数数字有几个,说就是说这个数,所以 `1` 就是 1 个 1:`11`,`11` 就是有 2 个 1:`21``21` 就是 1 个 2、1 个 1:`1211`,可想而知后面就是 `111221`,思路的话就是按这个逻辑模拟出来即可。
4545

46+
Java:
4647
```java
4748
class Solution {
4849
public String countAndSay(int n) {
@@ -67,6 +68,37 @@ class Solution {
6768
}
6869
```
6970

71+
kotlin(172ms/100.00%):
72+
```kotlin
73+
class Solution {
74+
fun countAndSay(n: Int): String {
75+
var say = "1"
76+
for (count in 1 until n) {
77+
say = say(say)
78+
}
79+
return say
80+
}
81+
82+
private fun say(say: String): String {
83+
val newSay = StringBuilder()
84+
var prev = say[0]
85+
var tCount = 0
86+
for (i in say.indices) {
87+
if (say[i] == prev) {
88+
tCount++
89+
continue
90+
}
91+
newSay.append(tCount).append(prev)
92+
prev = say[i]
93+
tCount = 1
94+
}
95+
newSay.append(tCount).append(prev)
96+
return newSay.toString()
97+
}
98+
}
99+
100+
```
101+
70102

71103
## 结语
72104

0 commit comments

Comments
 (0)