Skip to content

Commit 05263da

Browse files
committed
2 parents f9c0907 + ce19ec2 commit 05263da

27 files changed

+829
-141
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
*.iml
44
.idea/
55
out/
6+
lib/

MAINTENANCE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
commit前先pull, 尽量避免产生不必要的git分叉.
5151

52-
commit信息模板: ``feat: add solution of `Two Sum`(001) with Java``
52+
commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
5353

5454
## 添加新语言代码
5555
目前已使用的代码有: `Java``JavaScript``kotlin`

README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# <img src="./images/logo.gif" alt="LeetCode-Solution" width="80" height="80" align="bottom"/>LeetCode-Solution
2-
本仓库为LeetCode练习题题解记录,会持续更新各种语言的题解。同时也欢迎使用各种语言的同好♂们加入此项目,为开源贡献力量。
2+
3+
本仓库为LeetCode练习题题解记录,会持续更新各种语言的题解。同时也欢迎使用各种语言的同♂好们加入此项目,为开源贡献力量。
34

45
> [LeetCode](https://leetcode.com/) is the best platform to help you enhance your skills, expand your knowledge and prepare for technical interviews.
56
@@ -15,13 +16,15 @@
1516
> 科举为国取士,LeetCode 为 Google 筛码工,各取所需也。
1617
1718
## 目录
19+
1820
- [Easy](#Easy)
1921
- [Medium](#Medium)
2022
- [Hard](#Hard)
2123
- [库维护须知](#库维护须知)
2224
- [贡献者名单](#贡献者名单)
2325

2426
## [Easy](https://leetcode.com/problemset/all/?difficulty=Easy)
27+
2528
| 题目 | 题解 | Java | JavaScript | kotlin |
2629
| :-----------------: | ------------------------------------------------------------ | :------------: |:-----------: | :-------------: |
2730
| [001][001-question] | [Two Sum][001-tips] | [][001-java] | [][001-js] | [][001-kotlin] |
@@ -30,12 +33,12 @@
3033
| [013][013-question] | [Roman to Integer][013-tips] | [][013-java] | [][013-js] | [][013-kotlin] |
3134
| [014][014-question] | [Longest Common Prefix][014-tips] | [][014-java] | [][014-js] | [][014-kotlin] |
3235
| [020][020-question] | [Valid Parentheses][020-tips] | [][020-java] | | [][020-kotlin] |
33-
| [021][021-question] | [Merge Two Sorted Lists][021-tips] | [][021-java] | | |
34-
| [026][026-question] | [Remove Duplicates from Sorted Array][026-tips] | [][026-java] | | |
35-
| [027][027-question] | [Remove Element][027-tips] | [][027-java] | | |
36-
| [028][028-question] | [Implement strStr()][028-tips] | [][028-java] | | |
37-
| [035][035-question] | [Search Insert Position][035-tips] | [][035-java] | | |
38-
| [038][038-question] | [Count and Say][038-tips] | [][038-java] | | |
36+
| [021][021-question] | [Merge Two Sorted Lists][021-tips] | [][021-java] | | [][021-kotlin] |
37+
| [026][026-question] | [Remove Duplicates from Sorted Array][026-tips] | [][026-java] | | [][026-kotlin] |
38+
| [027][027-question] | [Remove Element][027-tips] | [][027-java] | | [][027-kotlin] |
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] |
3942
| [053][053-question] | [Maximum Subarray][053-tips] | [][053-java] | | |
4043
| [058][058-question] | [Length of Last Word][058-tips] | [][058-java] | | |
4144
| [066][066-question] | [Plus One][066-tips] | [][066-java] | | |
@@ -59,11 +62,11 @@
5962
| [226][226-question] | [Invert Binary Tree][226-tips] | [][226-java] | [][226-js] | |
6063
| [543][543-question] | [Diameter of Binary Tree][543-tips] | [][543-java] | | |
6164
| [561][561-question] | [Array Partition I][561-tips] | [][561-java] | [][561-js] | |
62-
| [643][643-question] | [Maximum Average Subarray I][643-tips] | | [][643-js] | |
63-
| [645][645-question] | [Set Mismatch][645-tips] | | [][645-js] | |
64-
| [653][653-question] | [Two Sum IV - Input is a BST][653-tips] | | [][653-js] | |
65-
| [657][657-question] | [Judge Route Circle][657-tips] | | [][657-js] | |
66-
| [661][661-question] | [Image Smoother][661-tips] | | [][661-js] | |
65+
| [643][643-question] | [Maximum Average Subarray I][643-tips] | [][643-java] | [][643-js] | |
66+
| [645][645-question] | [Set Mismatch][645-tips] | [][645-java] | [][645-js] | |
67+
| [653][653-question] | [Two Sum IV - Input is a BST][653-tips] | [][653-java] | [][653-js] | |
68+
| [657][657-question] | [Judge Route Circle][657-tips] | [][657-java] | [][657-js] | |
69+
| [661][661-question] | [Image Smoother][661-tips] | [][661-java] | [][661-js] | |
6770
| [665][665-question] | [Non-decreasing Array][665-tips] | | [][665-js] | |
6871
| [669][669-question] | [Trim a Binary Search Tree][669-tips] | | [][669-js] | |
6972
| [671][671-question] | [Second Minimum Node In a Binary Tree][671-tips] | | [][671-js] | |
@@ -95,6 +98,7 @@
9598
| [806][806-question] | [Number of Lines To Write String][806-tips] | | [][806-js] | |
9699

97100
## [Medium](https://leetcode.com/problemset/all/?difficulty=Medium)
101+
98102
| 题目 | 题解 | Java | JavaScript | kotlin |
99103
| :-----------------: | ------------------------------------------------------------ | :------------: | :----------: | :--------------: |
100104
| [002][002-question] | [Add Two Numbers][002-tips] | [][002-java] | | [][002-kotlin] |
@@ -120,6 +124,7 @@
120124
| [554][554-question] | [Brick Wall][554-tips] | [][554-java] | | |
121125

122126
## [Hard](https://leetcode.com/problemset/all/?difficulty=Hard)
127+
123128
| 题目 | 题解 | Java | JavaScript | kotlin |
124129
| :-----------------: | ------------------------------------------------------------ | :------------: | :----------: | :--------------: |
125130
| [004][004-question] | [Median of Two Sorted Arrays][004-tips] | [][004-java] | | |
@@ -132,9 +137,11 @@
132137
| [068][068-question] | [Text Justification][068-tips] | [][068-java] | | |
133138

134139
## 库维护须知
140+
135141
[库维护须知.md](MAINTENANCE_NOTES.md)
136142

137143
## 贡献者名单
144+
138145
排名不分先后
139146
- 感谢[@Mukyu](https://github.com/Mukyu), 提供**Java**题解。
140147
- 感谢[@taryn](https://github.com/taryn2016), 提供**JavaScript**题解。
@@ -447,6 +454,11 @@
447454
[543-java]: ./src/_543/Solution.java
448455
[554-java]: ./src/_554/Solution.java
449456
[561-java]: ./src/_561/Solution.java
457+
[643-java]: ./src/_643/Solution.java
458+
[645-java]: ./src/_645/Solution.java
459+
[653-java]: ./src/_653/Solution.java
460+
[657-java]: ./src/_657/Solution.java
461+
[661-java]: ./src/_661/Solution.java
450462
[728-java]: ./src/_728/Solution.java
451463
[771-java]: ./src/_771/Solution.java
452464
[804-java]: ./src/_804/Solution.java
@@ -458,4 +470,10 @@
458470
[013-kotlin]: ./src/_013/kotlin/Solution.kt
459471
[014-kotlin]: ./src/_014/kotlin/Solution.kt
460472
[020-kotlin]: ./src/_020/kotlin/Solution.kt
461-
[771-kotlin]: ./src/_771/kotlin/Solution.kt
473+
[021-kotlin]: ./src/_021/kotlin/Solution.kt
474+
[026-kotlin]: ./src/_026/kotlin/Solution.kt
475+
[027-kotlin]: ./src/_027/kotlin/Solution.kt
476+
[028-kotlin]: ./src/_028/kotlin/Solution.kt
477+
[035-kotlin]: ./src/_035/kotlin/Solution.kt
478+
[038-kotlin]: ./src/_038/kotlin/Solution.kt
479+
[771-kotlin]: ./src/_771/kotlin/Solution.kt

_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
theme: jekyll-theme-cayman

src/_021/kotlin/Solution.kt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package _021.kotlin
2+
3+
import structure.ListNode
4+
5+
/**
6+
* @author relish
7+
* @since 2018/04/11
8+
*/
9+
/**
10+
* Definition for singly-linked list.
11+
* class ListNode(var `val`: Int = 0) {
12+
* var next: ListNode? = null
13+
* }
14+
*/
15+
class Solution {
16+
fun mergeTwoLists(l1: ListNode?, l2: ListNode?): ListNode? {
17+
if (l1 == null && l2 == null) return null
18+
if (l1 == null) return l2
19+
if (l2 == null) return l1
20+
val head: ListNode
21+
var p: ListNode
22+
var n1 = l1
23+
var n2 = l2
24+
25+
if (n1.`val` > n2.`val`) {
26+
head = n2
27+
p = head
28+
n2 = n2.next
29+
} else {
30+
head = n1
31+
p = head
32+
n1 = n1.next
33+
}
34+
while (n1 != null || n2 != null) {
35+
if (n1 == null) {
36+
p.next = n2
37+
break
38+
} else if (n2 == null) {
39+
p.next = n1
40+
break
41+
}
42+
if (n1.`val` > n2.`val`) {
43+
p.next = n2
44+
p = p.next
45+
n2 = n2.next
46+
} else {
47+
p.next = n1
48+
p = p.next
49+
n1 = n1.next
50+
}
51+
}
52+
return head
53+
}
54+
}
55+
56+
fun main(args: Array<String>) {
57+
val l1 = ListNode(1)
58+
l1.next = ListNode(2)
59+
l1.next.next = ListNode(4)
60+
val l2 = ListNode(1)
61+
l2.next = ListNode(3)
62+
l2.next.next = ListNode(4)
63+
64+
var ret = Solution().mergeTwoLists(l1, l2)
65+
while (ret != null) {
66+
print(String.format("%d->", ret.`val`))
67+
ret = ret.next
68+
}
69+
70+
}

src/_026/kotlin/Solution.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package _026.kotlin
2+
3+
/**
4+
* 268ms
5+
* 96.77%
6+
* @author relish
7+
* @since 2018/04/12
8+
*/
9+
class Solution {
10+
fun removeDuplicates(nums: IntArray): Int {
11+
if (nums.size < 2) return nums.size
12+
var t: Int
13+
var j = 1
14+
var i = 1
15+
while (i < nums.size) {
16+
t = i
17+
while (i < nums.size && nums[i] == nums[t - 1]) {
18+
i++
19+
}
20+
if (i < nums.size) {
21+
nums[j] = nums[i]
22+
j++
23+
i++
24+
}
25+
}
26+
return j
27+
}
28+
}
29+
30+
fun main(args: Array<String>) {
31+
val arr = intArrayOf(1, 1, 2, 3, 3, 3, 3, 4, 4, 5, 6, 7, 7)
32+
println(Solution().removeDuplicates(arr))
33+
}

src/_027/kotlin/Solution.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package _027.kotlin
2+
3+
/**
4+
* @author relish
5+
* @since 2018/04/13
6+
*/
7+
class Solution {
8+
fun removeElement(nums: IntArray, `val`: Int): Int {
9+
var count = 0
10+
for (index in nums.indices) {
11+
val t = nums[index]
12+
if (t != `val`) {
13+
nums[count++] = t
14+
}
15+
}
16+
return count
17+
}
18+
}
19+
20+
fun main(args: Array<String>) {
21+
val arr = intArrayOf(3, 2, 3, 1, 2, 3, 2)
22+
println(Solution().removeElement(arr, 3))
23+
}

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+
}

0 commit comments

Comments
 (0)