LIXI.FUN
0%

1
2
3
4
5
6
7
8
9
10
11
12
mvn install:install-file  \
-Dfile=<path-to-file> \
-DgroupId=<group-id> \
-DartifactId=<artifact-id> \
-Dversion=<version> \
-Dpackaging=jar

# 如果有 pom.xml 文件的话,可以使用
mvn install:install-file -Dfile=<path-to-file> -DpomFile=<path-to-pomfile>

# 对于 2.5 以上版本的 maven-install-plugin
mvn install:install-file -Dfile=<path-to-file>

https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

题目链接

20. 有效的括号

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {

// Map.of 是 Java 9 及以后有的 api
private static final Map<Character, Character> pairs = Map.of('(', ')', '{', '}', '[', ']');

public boolean isValid(String s) {

// 栈推荐使用 Deque 而不是 Stack
Deque<Character> stack = new ArrayDeque<>();

for (char c : s.toCharArray()) {

if (pairs.containsKey(c)) {
// 如果是 左边括号,就把 右边括号放进去
stack.push(pairs.get(c));
} else if (stack.isEmpty() || stack.pop() != c){
// 如果是 右边括号,就 pop 出来看是不是一样
return false;
}
}

return stack.isEmpty();
}
}

复杂度分析

  • 时间复杂度: O(N)
  • 空间复杂度: O(n+∣Σ∣)

题目链接

3. 无重复字符的最长子串

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public int lengthOfLongestSubstring(String s) {

int n = s.length();

// 字母上次出现的下标
int[] charLastOccIndex = new int[128];
Arrays.fill(charLastOccIndex, -1);

int left = -1;
int max = 0;

for (int i = 0; i < n; i++) {

char c = s.charAt(i);

left = Math.max(left, charLastOccIndex[c] + 1);
max = Math.max(max, i - left + 1);

charLastOccIndex[c] = i;
}

return max;
}
}

复杂度分析

  • 时间复杂度: O(N)
  • 空间复杂度: O(count) count 为字符集数量