Skip to content

Commit ae8a511

Browse files
committed
2 parents 958f824 + 991648b commit ae8a511

File tree

5 files changed

+166
-28
lines changed

5 files changed

+166
-28
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
| [069][069-question] | [Sqrt(x)][069-tips] | [][069-java] | | [][069-kotlin] |
4747
| [070][070-question] | [Climbing Stairs][070-tips] | [][070-java] | | [][070-kotlin] |
4848
| [083][083-question] | [Remove Duplicates from Sorted List][083-tips] | [][083-java] | | [][083-kotlin] |
49-
| [088][088-question] | [Merge Sorted Array][088-tips] | [][088-java] | | |
49+
| [088][088-question] | [Merge Sorted Array][088-tips] | [][088-java] | | [][088-kotlin] |
5050
| [100][100-question] | [Same Tree][100-tips] | [][100-java] | | |
5151
| [101][101-question] | [Symmetric Tree][101-tips] | [][101-java] | | |
5252
| [104][104-question] | [Maximum Depth of Binary Tree][104-tips] | [][104-java] | | |
@@ -73,7 +73,7 @@
7373
| [674][674-question] | [Longest Continuous Increasing Subsequence][674-tips] | [][674-java] | [][674-js] | |
7474
| [680][680-question] | [Valid Palindrome II][680-tips] | [][680-java] | [][680-js] | |
7575
| [682][682-question] | [Baseball Game][682-tips] | [][682-java] | [][682-js] | |
76-
| [686][686-question] | [Repeated String Match][686-tips] | | [][686-js] | |
76+
| [686][686-question] | [Repeated String Match][686-tips] | [][686-java] | [][686-js] | |
7777
| [687][687-question] | [Longest Univalue Path][687-tips] | | [][687-js] | |
7878
| [693][693-question] | [Binary Number with Alternating Bits][693-tips] | | [][693-js] | |
7979
| [695][695-question] | [Max Area of Island][695-tips] | | [][695-js] | |
@@ -469,6 +469,7 @@
469469
[671-java]: ./src/_671/Solution.java
470470
[674-java]: ./src/_674/Solution.java
471471
[680-java]: ./src/_680/Solution.java
472+
[686-java]: ./src/_686/Solution.java
472473
[682-java]: ./src/_682/Solution.java
473474
[728-java]: ./src/_728/Solution.java
474475
[771-java]: ./src/_771/Solution.java
@@ -494,4 +495,5 @@
494495
[069-kotlin]: ./src/_069/kotlin/Solution.kt
495496
[070-kotlin]: ./src/_070/kotlin/Solution.kt
496497
[083-kotlin]: ./src/_083/kotlin/Solution.kt
498+
[088-kotlin]: ./src/_088/kotlin/Solution.kt
497499
[771-kotlin]: ./src/_771/kotlin/Solution.kt

src/_088/kotlin/Solution.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package _088.kotlin
2+
3+
import java.util.*
4+
5+
/**
6+
* @author relish
7+
* @since 2018/04/24
8+
*/
9+
class Solution {
10+
fun merge(nums1: IntArray, m: Int, nums2: IntArray, n: Int): Unit {
11+
var index = n + m - 1
12+
var i = m - 1
13+
var j = n - 1
14+
15+
while ((i >= 0 || j >= 0) && index >= 0) {
16+
if (j < 0 && i in 0..(m - 1)) {
17+
nums1[index--] = nums1[i--]
18+
continue
19+
}
20+
if (i < 0 && j in 0..(n - 1)) {
21+
nums1[index--] = nums2[j--]
22+
continue
23+
}
24+
if (nums1[i] > nums2[j]) {
25+
nums1[index--] = nums1[i--]
26+
} else {
27+
nums1[index--] = nums2[j--]
28+
}
29+
}
30+
}
31+
}
32+
33+
fun main(args: Array<String>) {
34+
val arr1 = intArrayOf(1, 2, 3, 0, 0, 0)
35+
val arr2 = intArrayOf(2, 5, 6)
36+
Solution().merge(arr1, 3, arr2, 3)
37+
println(Arrays.toString(arr1))
38+
}

src/_686/Solution.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package _686;
2+
3+
public class Solution {
4+
public int repeatedStringMatch(String A, String B) {
5+
if (A.length() > B.length()) {
6+
return A.contains(B) ? 1 : (A + A).contains(B) ? 2 : -1;
7+
}
8+
9+
int aInB = B.indexOf(A);
10+
11+
if (aInB >= A.length()) {
12+
return -1;
13+
}
14+
if (aInB == -1) {
15+
return (A + A).contains(B) ? 2 : -1;
16+
}
17+
18+
int repeatCount = aInB == 0 ? 0 : 1;
19+
int iA = A.length() - aInB;
20+
int iB = 0;
21+
22+
while (iB < B.length()) {
23+
if (iA == A.length()) {
24+
iA = 0;
25+
repeatCount++;
26+
}
27+
28+
if (A.charAt(iA) != B.charAt(iB)) {
29+
return -1;
30+
}
31+
32+
iA++;
33+
iB++;
34+
}
35+
36+
return repeatCount;
37+
}
38+
39+
public static void main(String[] args) {
40+
Solution solution = new Solution();
41+
System.out.println(solution.repeatedStringMatch("abcd", "cdabcdab"));
42+
System.out.println(solution.repeatedStringMatch("abcd", "ab"));
43+
System.out.println(solution.repeatedStringMatch("abcd", "da"));
44+
System.out.println(solution.repeatedStringMatch("a", "aa"));
45+
}
46+
}

tips/088/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ You may assume that *nums1* has enough space (size that is greater or equal to *
1515

1616
题意是给两个已排序的数组 `nums1``nums2`,合并 `nums2``nums1` 中,两数组元素个数分别为 `m``n`,而且 `nums1` 数组的长度足够容纳 `m + n` 个元素,如果我们按顺序排下去,那肯定要开辟一个新数组来保存元素,如果我们选择逆序,这样利用 `nums1` 自身空间足矣,不会出现覆盖的情况,依次把大的元素插入到 `nums1` 的末尾,确保 `nums2` 中的元素全部插入到 `nums1` 即可。
1717

18+
Java:
1819
```java
1920
class Solution {
2021
public void merge(int[] nums1, int m, int[] nums2, int n) {
@@ -27,6 +28,32 @@ class Solution {
2728
}
2829
```
2930

31+
kotlin(208ms/100.00%)
32+
```kotlin
33+
class Solution {
34+
fun merge(nums1: IntArray, m: Int, nums2: IntArray, n: Int): Unit {
35+
var index = n + m - 1
36+
var i = m - 1
37+
var j = n - 1
38+
39+
while ((i >= 0 || j >= 0) && index >= 0) {
40+
if (j < 0 && i in 0..(m - 1)) {
41+
nums1[index--] = nums1[i--]
42+
continue
43+
}
44+
if (i < 0 && j in 0..(n - 1)) {
45+
nums1[index--] = nums2[j--]
46+
continue
47+
}
48+
if (nums1[i] > nums2[j]) {
49+
nums1[index--] = nums1[i--]
50+
} else {
51+
nums1[index--] = nums2[j--]
52+
}
53+
}
54+
}
55+
}
56+
```
3057

3158
## 结语
3259

tips/686/README.md

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,71 @@
1-
[xxxx][title]
1+
[Repeated String Match][title]
22

33
## Description
4-
// 抄题目
54

5+
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.
66

7-
**Example:**
7+
For example, with A = "abcd" and B = "cdabcdab".
88

9-
```
10-
// 抄Example
11-
```
9+
Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times ("abcdabcd").
1210

1311
**Note:**
14-
// Note
1512

16-
**Tags:** // tags
13+
The length of `A` and `B` will be between 1 and 10000.
1714

15+
**Tags:** [String](https://leetcode.com/tag/string/)
1816

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
17+
## 思路
2318

24-
```
25-
```javascript
19+
判断重复的A字符串能否使B字符串成为子字符串,若能,计算出至少需要重复几次。使用A字符串对B字符串重复进行匹配即可。但在匹配之前可通过一些已知条件减少一些计算:
2620

27-
```
21+
- 如果A的长度大于B,假如重复A字符串能使B成为子字符串,那么至多只需重复两次。因为如果重复三次才行,那么B的长度肯定大于A,就矛盾了。
22+
- 如果A的长度小于B,用`IndexOf()`方法计算字符串A在B中首次匹配的位置,记作`aInB`
23+
- 如果`aInB`>=`A.length()`,则说明在B的前面一段(比A长或相等)字符串中,没有和A匹配的子字符串,这跟连续的A字符串这个要求不符,因此可以排除。
24+
- 如果`aInB`==`-1`,则B字符串只有可能是两个连续A字符串的子字符串。
25+
- 接着循环用A字符串对B字符串进行匹配。
2826

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
32-
33-
```
34-
35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
27+
**Java:**
3828

29+
```java
30+
public int repeatedStringMatch(String A, String B) {
31+
if (A.length() > B.length()) {
32+
return A.contains(B) ? 1 : (A + A).contains(B) ? 2 : -1;
33+
}
34+
35+
int aInB = B.indexOf(A);
36+
37+
if (aInB >= A.length()) {
38+
return -1;
39+
}
40+
if (aInB == -1) {
41+
return (A + A).contains(B) ? 2 : -1;
42+
}
43+
44+
int repeatCount = aInB == 0 ? 0 : 1;
45+
int iA = A.length() - aInB;
46+
int iB = 0;
47+
48+
while (iB < B.length()) {
49+
if (iA == A.length()) {
50+
iA = 0;
51+
repeatCount++;
52+
}
53+
54+
if (A.charAt(iA) != B.charAt(iB)) {
55+
return -1;
56+
}
57+
58+
iA++;
59+
iB++;
60+
}
61+
62+
return repeatCount;
63+
}
3964
```
4065

4166
## 结语
42-
67+
4368
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4469

45-
[title]: https://leetcode.com/problems/xxxx
70+
[title]: https://leetcode.com/problems/repeated-string-match/description/
4671
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)