0%

代码解析

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.demo.s141;

/**
* 环形链表
* 给你一个链表的头节点 head ,判断链表中是否有环。
*
* 如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。
*
* 如果链表中存在环 ,则返回 true 。 否则,返回 false 。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode.cn/problems/linked-list-cycle
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class Solution {
public boolean hasCycle(ListNode head) {
//快指针
ListNode slow = head;
//慢指针
ListNode fast = head;
//下一步不为null
while(fast != null && fast.next != null && fast.next.next != null) {
//快指针走两步
fast = fast.next.next;
//慢指针走一步
slow = slow.next;
//快慢相遇则出现环
if(fast == slow) {
return true;
}
}
return false;
}
}

代码解析

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.demo.s140;

import java.util.*;

/**
* 单词拆分 II
* 给定一个字符串 s 和一个字符串字典 wordDict ,在字符串 s 中增加空格来构建一个句子,使得句子中所有的单词都在词典中。以任意顺序 返回所有这些可能的句子。
*
* 注意:词典中的同一个单词可能在分段中被重复使用多次。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode.cn/problems/word-break-ii
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*
*/
public class Solution {
public List<String> wordBreak(String s, List<String> wordDict) {
Map<Integer, List<List<String>>> map = new HashMap<Integer, List<List<String>>>();
List<List<String>> wordBreaks = backtrack(s, s.length(), new HashSet<String>(wordDict), 0, map);
List<String> breakList = new LinkedList<String>();
for (List<String> wordBreak : wordBreaks) {
breakList.add(String.join(" ", wordBreak));
}
return breakList;
}

public List<List<String>> backtrack(String s, int length, Set<String> wordSet, int index, Map<Integer, List<List<String>>> map) {
if (!map.containsKey(index)) {
List<List<String>> wordBreaks = new LinkedList<List<String>>();
if (index == length) {
wordBreaks.add(new LinkedList<String>());
}
for (int i = index + 1; i <= length; i++) {
String word = s.substring(index, i);
if (wordSet.contains(word)) {
List<List<String>> nextWordBreaks = backtrack(s, length, wordSet, i, map);
for (List<String> nextWordBreak : nextWordBreaks) {
LinkedList<String> wordBreak = new LinkedList<String>(nextWordBreak);
wordBreak.offerFirst(word);
wordBreaks.add(wordBreak);
}
}
}
map.put(index, wordBreaks);
}
return map.get(index);
}
}

代码解析

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
26
27
28
29
30
31
32
33
package com.demo.s14;

/**
* 最长公共前缀
* 编写一个函数来查找字符串数组中的最长公共前缀。
*
* 如果不存在公共前缀,返回空字符串 ""。
*/
public class Solution {
public String longestCommonPrefix(String[] strs) {
//特殊情况判断
if (strs == null || strs.length == 0) {
return "";
}
//取第一个数组做对比
int length = strs[0].length();
//字符串个数
int count = strs.length;
//遍历字符串字符
for (int i = 0; i < length; i++) {
char c = strs[0].charAt(i);
//对比其他字符串
for (int j = 1; j < count; j++) {
//达到其他字符串最大长度 或者 字符匹配不上,则终止
if (i == strs[j].length() || strs[j].charAt(i) != c) {
return strs[0].substring(0, i);
}
}
}
return strs[0];

}
}

代码解析

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.demo.s139;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* 单词拆分
* 给你一个字符串 s 和一个字符串列表 wordDict 作为字典。请你判断是否可以利用字典中出现的单词拼接出 s 。
*
* 注意:不要求字典中出现的单词全部都使用,并且字典中的单词可以重复使用。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode.cn/problems/word-break
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public class Solution {

public boolean wordBreak(String s, List<String> wordDict) {
if(wordDict.size() == 0) return false;
Set<Integer> wordLength = new HashSet<>();
for (String word : wordDict) {
wordLength.add(word.length());
}
//单词去重
Set<String> dict = new HashSet<>(wordDict);
Set<String> wrongSet = new HashSet<>();
return matchWords(s, dict, wordLength, wrongSet);
}

public boolean matchWords(String s, Set<String> dict, Set<Integer> wordLength, Set<String> wrongSet) {
//特殊值判断
if("".equals(s) || dict.contains(s)) return true;
//遍历单词所有字符
for(Integer j : wordLength) {
if(j > s.length()) return false;
String sub = s.substring(0, j);
//截取字符串,找到单词表中存在的子串
if(dict.contains(sub)) {
String subEnd = s.substring(j);
if(!wrongSet.contains(subEnd) && matchWords(subEnd, dict, wordLength, wrongSet)) {
return true;
} else {
wrongSet.add(subEnd);
}
}
}
return false;
}
}

代码解析

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.demo.s138;

import java.util.HashMap;
import java.util.Map;

/**
* 复制带随机指针的链表
* 给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。
*
* 构造这个链表的 深拷贝。 深拷贝应该正好由 n 个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。
*
* 例如,如果原链表中有 X 和 Y 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 x 和 y ,同样有 x.random --> y 。
*
* 返回复制链表的头节点。
*
* 用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:
*
* val:一个表示 Node.val 的整数。
* random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为  null 。
* 你的代码 只 接受原链表的头节点 head 作为传入参数。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode.cn/problems/copy-list-with-random-pointer
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
class Node {
int val;
Node next;
Node random;

public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
public class Solution {
//节点缓存到map
Map<Node, Node> cachedNode = new HashMap<Node, Node>();

public Node copyRandomList(Node head) {
//特殊情况判断
if (head == null) {
return null;
}
//如果已经缓存说明节点已存在,则不用复制
if (!cachedNode.containsKey(head)) {
//复制节点值
Node headNew = new Node(head.val);
//缓存新旧节点
cachedNode.put(head, headNew);
//新节点next指向下一个新节点
headNew.next = copyRandomList(head.next);
//新节点random指向下一个新节点
headNew.random = copyRandomList(head.random);
}
return cachedNode.get(head);
}
}

代码解析

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
26
package com.demo.s137;

/**
* 只出现一次的数字 II
* 给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。
*
*/
public class Solution {
public int singleNumber(int[] nums) {
//最终的结果值
int res = 0;
//int类型有32位,统计每一位1的个数
for (int i = 0; i < 32; i++) {
//统计第i位中1的个数
int oneCount = 0;
for (int j = 0; j < nums.length; j++) {
oneCount += (nums[j] >>> i) & 1;
}
//如果1的个数不是3的倍数,说明那个只出现一次的数字
//的二进制位中在这一位是1
if (oneCount % 3 == 1)
res |= 1 << i;
}
return res;
}
}

代码解析

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
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.demo.s136;

import java.util.HashMap;

/**
* 只出现一次的数字
* 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
*
* 说明:
*
* 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode.cn/problems/single-number
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public class Solution {
public int singleNumber(int[] nums) {
//特殊情况
if (nums.length == 1) {
return nums[0];
}
//HashMap
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
//将其存入哈希表中,含义为,若该元素不存在则存入表中,并计数为1,若已经存在获取次数并加1.
for (int x : nums) {
map.put(x , map.getOrDefault(x,0) + 1);
}
//遍历出出现次数为1的情况
for (int y : map.keySet()) {
if(map.get(y) == 1){
return y;
}
}
return 0;

}
}

代码解析

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.demo.s135;

/**
* 分发糖果
* n 个孩子站成一排。给你一个整数数组 ratings 表示每个孩子的评分。
*
* 你需要按照以下要求,给这些孩子分发糖果:
*
* 每个孩子至少分配到 1 个糖果。
* 相邻两个孩子评分更高的孩子会获得更多的糖果。
* 请你给每个孩子分发糖果,计算并返回需要准备的 最少糖果数目 。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode.cn/problems/candy
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public class Solution {
public int candy(int[] ratings) {
int n = ratings.length;
int[] left = new int[n];
//从左到右发,最少给1个 分高就比右边多给一个
for (int i = 0; i < n; i++) {
if (i > 0 && ratings[i] > ratings[i - 1]) {
left[i] = left[i - 1] + 1;
} else {
left[i] = 1;
}
}
//从右向左,保证左侧分高的能多分一个糖
int right = 0, ret = 0;
for (int i = n - 1; i >= 0; i--) {
if (i < n - 1 && ratings[i] > ratings[i + 1]) {
right++;
} else {
right = 1;
}
ret += Math.max(left[i], right);
}
return ret;
}
}

代码解析

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.demo.s134;

/**
* 加油站
* 在一条环路上有 n 个加油站,其中第 i 个加油站有汽油 gas[i] 升。
*
* 你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。
*
* 给定两个整数数组 gas 和 cost ,如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1 。如果存在解,则 保证 它是 唯一 的。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode.cn/problems/gas-station
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public class Solution {
public int canCompleteCircuit(int[] gas, int[] cost) {
int n = gas.length;
int i = 0;
while (i < n) {
int sumOfGas = 0, sumOfCost = 0;
int cnt = 0;
while (cnt < n) {
int j = (i + cnt) % n;
sumOfGas += gas[j];
sumOfCost += cost[j];
if (sumOfCost > sumOfGas) {
break;
}
cnt++;
}
if (cnt == n) {
return i;
} else {
i = i + cnt + 1;
}
}
return -1;
}
}

代码解析

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.demo.s133;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* 克隆图
* 给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆)。
*
* 图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node])。
*
* class Node {
* public int val;
* public List<Node> neighbors;
* }
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode.cn/problems/clone-graph
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/

class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}

public class Solution {
private HashMap<Node, Node> visited = new HashMap <> ();
public Node cloneGraph(Node node) {
if (node == null) {
return node;
}

// 如果该节点已经被访问过了,则直接从哈希表中取出对应的克隆节点返回
if (visited.containsKey(node)) {
return visited.get(node);
}

// 克隆节点,注意到为了深拷贝我们不会克隆它的邻居的列表
Node cloneNode = new Node(node.val, new ArrayList());
// 哈希表存储
visited.put(node, cloneNode);

// 遍历该节点的邻居并更新克隆节点的邻居列表
for (Node neighbor: node.neighbors) {
cloneNode.neighbors.add(cloneGraph(neighbor));
}
return cloneNode;
}

}