Skip to content

Commit 94aba67

Browse files
douhuatonghappyxiaodou
douhuatong
authored andcommitted
init
1 parent 6492aae commit 94aba67

File tree

4 files changed

+201
-26
lines changed

4 files changed

+201
-26
lines changed

.gitignore

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
1-
# ---> macOS
2-
.DS_Store
3-
.AppleDouble
4-
.LSOverride
5-
6-
# Icon must end with two \r
7-
Icon
8-
9-
10-
# Thumbnails
11-
._*
12-
13-
# Files that might appear in the root of a volume
14-
.DocumentRevisions-V100
15-
.fseventsd
16-
.Spotlight-V100
17-
.TemporaryItems
18-
.Trashes
19-
.VolumeIcon.icns
20-
21-
# Directories potentially created on remote AFP share
22-
.AppleDB
23-
.AppleDesktop
24-
Network Trash Folder
25-
Temporary Items
26-
.apdisk
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Example user template template
3+
### Example user template
274

5+
# IntelliJ project files
6+
.idea
7+
*.iml
8+
out
9+
gen

pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.leetcode</groupId>
8+
<artifactId>leetcode</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
15+
<java.version>1.8</java.version>
16+
</properties>
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.apache.commons</groupId>
20+
<artifactId>commons-lang3</artifactId>
21+
<version>3.9</version>
22+
</dependency>
23+
</dependencies>
24+
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-compiler-plugin</artifactId>
31+
<configuration>
32+
<source>${java.version}</source>
33+
<target>${java.version}</target>
34+
<encoding>${project.build.sourceEncoding}</encoding>
35+
<showWarnings>true</showWarnings>
36+
</configuration>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
</project>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.leetcode;
2+
3+
import java.util.Collections;
4+
import java.util.LinkedList;
5+
import java.util.Stack;
6+
7+
/**
8+
* 给定一个经过编码的字符串,返回它解码后的字符串。
9+
* <p>
10+
* 编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。
11+
* <p>
12+
* 你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。
13+
* <p>
14+
* 此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。
15+
* <p>
16+
* 示例:
17+
* <p>
18+
* s = "3[a]2[bc]", 返回 "aaabcbc".
19+
* s = "3[a2[c]]", 返回 "accaccacc".
20+
* s = "2[abc]3[cd]ef", 返回 "abcabccdcdcdef".
21+
* <p>
22+
* 来源:力扣(LeetCode)
23+
* 链接:https://leetcode-cn.com/problems/decode-string
24+
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
25+
*/
26+
public class DecodeString {
27+
28+
int ptr;
29+
30+
public String decodeString(String s) {
31+
Stack<String> stk = new Stack<>();
32+
ptr = 0;
33+
while (ptr < s.length()) {
34+
char cur = s.charAt(ptr);
35+
if (Character.isDigit(cur)) {//是否为数字
36+
// 获取一个数字并进栈
37+
String digits = getDigits(s);
38+
stk.add(digits);
39+
} else if (Character.isLetter(cur) || cur == '[') {
40+
stk.add(String.valueOf(s.charAt(ptr++)));
41+
} else {
42+
++ptr;
43+
Stack<String> sub = new Stack<>();
44+
while (!"[".equals(stk.peek())) {
45+
sub.add(stk.pop());
46+
}
47+
Collections.reverse(sub);
48+
// 左括号出栈
49+
stk.pop();
50+
// 此时栈顶为当前 sub 对应的字符串应该出现的次数
51+
int repTime = Integer.parseInt(stk.pop());
52+
StringBuffer t = new StringBuffer();
53+
String o = getString(sub);
54+
// 构造字符串
55+
while (repTime-- > 0) {
56+
t.append(o);
57+
}
58+
// 将构造好的字符串入栈
59+
stk.add(t.toString());
60+
}
61+
}
62+
return getString(stk);
63+
}
64+
65+
public String getString(Stack<String> v) {
66+
StringBuffer ret = new StringBuffer();
67+
for (String s : v) {
68+
ret.append(s);
69+
}
70+
return ret.toString();
71+
}
72+
73+
74+
public String getDigits(String s) {
75+
StringBuffer ret = new StringBuffer();
76+
while (Character.isDigit(s.charAt(ptr))) {
77+
ret.append(s.charAt(ptr++));
78+
}
79+
return ret.toString();
80+
}
81+
82+
83+
public static void main(String[] args) {
84+
DecodeString decodeString = new DecodeString();
85+
System.err.println(decodeString.decodeString("3[a]2[bc]"));
86+
}
87+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.leetcode;
2+
3+
4+
import java.util.Arrays;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* 给定一个整数数组 A,返回其中元素之和可被 K 整除的(连续、非空)子数组的数目。
10+
* <p>
11+
* 示例:
12+
* <p>
13+
* 输入:A = [4,5,0,-2,-3,1], K = 5
14+
* 输出:7
15+
* 解释:
16+
* 有 7 个子数组满足其元素之和可被 K = 5 整除:
17+
* [4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
18+
* <p>
19+
* 来源:力扣(LeetCode)
20+
* 链接:https://leetcode-cn.com/problems/subarray-sums-divisible-by-k
21+
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
22+
*/
23+
public class SubarraysDivByK {
24+
25+
public static int subarraysDivByK(int[] A, int K) {
26+
int length = A.length;
27+
int r = 0;
28+
for (int i = 0; i <= length; i++) {
29+
for (int j = 0; j <= length; j++) {
30+
if (j < i) {
31+
int newArray[] = Arrays.copyOfRange(A, j, i);
32+
if (newArray.length > 0) {
33+
int sum = 0;
34+
for (int item : newArray) {
35+
sum = sum + item;
36+
}
37+
if (sum % K == 0) {
38+
r++;
39+
}
40+
}
41+
}
42+
}
43+
}
44+
return r;
45+
}
46+
47+
48+
public static int subarraysDivByK2(int[] A, int K) {
49+
Map<Integer, Integer> record = new HashMap<>();
50+
record.put(0, 1);
51+
int sum = 0, ans = 0;
52+
for (int elem : A) {
53+
sum += elem;
54+
int modulus = (sum % K + K) % K;
55+
int same = record.getOrDefault(modulus, 0);
56+
ans = ans + same;
57+
record.put(modulus, same + 1);
58+
}
59+
return ans;
60+
}
61+
62+
public static void main(String[] args) {
63+
int[] a = {4, 5, 0, -2, -3, 1};
64+
System.err.println(subarraysDivByK2(a, 5));
65+
}
66+
}

0 commit comments

Comments
 (0)