-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57fd47c
commit 00cd2a7
Showing
4 changed files
with
242 additions
and
2 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
.../java/com/jacobs/basic/leetcode/editor/cn/LongestSubstringWithoutRepeatingCharacters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。 | ||
// | ||
// | ||
// | ||
// 示例 1: | ||
// | ||
// | ||
//输入: s = "abcabcbb" | ||
//输出: 3 | ||
//解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。 | ||
// | ||
// | ||
// 示例 2: | ||
// | ||
// | ||
//输入: s = "bbbbb" | ||
//输出: 1 | ||
//解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。 | ||
// | ||
// | ||
// 示例 3: | ||
// | ||
// | ||
//输入: s = "pwwkew" | ||
//输出: 3 | ||
//解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。 | ||
// 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。 | ||
// | ||
// | ||
// 示例 4: | ||
// | ||
// | ||
//输入: s = "" | ||
//输出: 0 | ||
// | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// 0 <= s.length <= 5 * 104 | ||
// s 由英文字母、数字、符号和空格组成 | ||
// | ||
// Related Topics 哈希表 字符串 滑动窗口 | ||
// 👍 5680 👎 0 | ||
|
||
|
||
package com.jacobs.basic.leetcode.editor.cn; | ||
|
||
public class LongestSubstringWithoutRepeatingCharacters { | ||
public static void main(String[] args) { | ||
Solution solution = new LongestSubstringWithoutRepeatingCharacters().new Solution(); | ||
System.out.println(solution.lengthOfLongestSubstring("")); | ||
} | ||
|
||
//leetcode submit region begin(Prohibit modification and deletion) | ||
class Solution { | ||
int[] charMap = new int[256]; | ||
|
||
public int lengthOfLongestSubstring(String s) { | ||
if (s == null || s.equals("")) { | ||
return 0; | ||
} | ||
// 记录最长无重复字串长度 | ||
int maxLength = 0; | ||
int startI = 0; | ||
int endJ = 0; | ||
char[] charArr = s.toCharArray(); | ||
int charLen = charArr.length; | ||
while (endJ < charLen) { | ||
int index = charArr[endJ]; | ||
if (charMap[index] != 0) { | ||
while (startI < endJ) { | ||
int backIndex = charArr[startI]; | ||
//回退保存的记录 | ||
charMap[backIndex] = 0; | ||
if (charArr[endJ] == charArr[startI++]) { | ||
break; | ||
} | ||
} | ||
} | ||
// 更新一下maxLength | ||
maxLength = Math.max(endJ - startI + 1, maxLength); | ||
// 最后标记为出现过 | ||
charMap[index] = 1; | ||
endJ++; | ||
} | ||
|
||
return maxLength; | ||
} | ||
} | ||
//leetcode submit region end(Prohibit modification and deletion) | ||
|
||
} |
145 changes: 145 additions & 0 deletions
145
basic-learning/src/main/java/com/jacobs/basic/leetcode/editor/cn/OpenTheLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
//你有一个带有四个圆形拨轮的转盘锁。每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' | ||
// 。每个拨轮可以自由旋转:例如把 '9' 变为 '0','0' 变为 '9' 。每次旋转都只能旋转一个拨轮的一位数字。 | ||
// | ||
// 锁的初始数字为 '0000' ,一个代表四个拨轮的数字的字符串。 | ||
// | ||
// 列表 deadends 包含了一组死亡数字,一旦拨轮的数字和列表里的任何一个元素相同,这个锁将会被永久锁定,无法再被旋转。 | ||
// | ||
// 字符串 target 代表可以解锁的数字,你需要给出解锁需要的最小旋转次数,如果无论如何不能解锁,返回 -1 。 | ||
// | ||
// | ||
// | ||
// 示例 1: | ||
// | ||
// | ||
//输入:deadends = ["0201","0101","0102","1212","2002"], target = "0202" | ||
//输出:6 | ||
//解释: | ||
//可能的移动序列为 "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202"。 | ||
//注意 "0000" -> "0001" -> "0002" -> "0102" -> "0202" 这样的序列是不能解锁的, | ||
//因为当拨动到 "0102" 时这个锁就会被锁定。 | ||
// | ||
// | ||
// 示例 2: | ||
// | ||
// | ||
//输入: deadends = ["8888"], target = "0009" | ||
//输出:1 | ||
//解释: | ||
//把最后一位反向旋转一次即可 "0000" -> "0009"。 | ||
// | ||
// | ||
// 示例 3: | ||
// | ||
// | ||
//输入: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], targ | ||
//et = "8888" | ||
//输出:-1 | ||
//解释: | ||
//无法旋转到目标数字且不被锁定。 | ||
// | ||
// | ||
// 示例 4: | ||
// | ||
// | ||
//输入: deadends = ["0000"], target = "8888" | ||
//输出:-1 | ||
// | ||
// | ||
// | ||
// | ||
// 提示: | ||
// | ||
// | ||
// 1 <= deadends.length <= 500 | ||
// deadends[i].length == 4 | ||
// target.length == 4 | ||
// target 不在 deadends 之中 | ||
// target 和 deadends[i] 仅由若干位数字组成 | ||
// | ||
// Related Topics 广度优先搜索 数组 哈希表 字符串 | ||
// 👍 360 👎 0 | ||
|
||
|
||
package com.jacobs.basic.leetcode.editor.cn; | ||
|
||
import java.util.HashSet; | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
import java.util.Set; | ||
|
||
public class OpenTheLock { | ||
public static void main(String[] args) { | ||
Solution solution = new OpenTheLock().new Solution(); | ||
System.out.println(solution.openLock(new String[]{"0201", "0101", "0102", "1212", "2002"}, "0202")); | ||
} | ||
|
||
//leetcode submit region begin(Prohibit modification and deletion) | ||
class Solution { | ||
public int openLock(String[] deadends, String target) { | ||
//使用bfs枚举所有可能性 | ||
Queue<String> queue = new LinkedList<>(); | ||
Set<String> deadSet = new HashSet<>(); | ||
for (int i = 0; i < deadends.length; i++) { | ||
deadSet.add(deadends[i]); | ||
} | ||
Set<String> visited = new HashSet<>(); | ||
queue.add("0000"); | ||
int step = 0; | ||
while (!queue.isEmpty()) { | ||
int queueSize = queue.size(); | ||
for (int i = 0; i < queueSize; i++) { | ||
String str = queue.poll(); | ||
// 需要记录当前的层,也即是最少的步数 | ||
if (deadSet.contains(str)) { | ||
continue; | ||
} | ||
if (str.equals(target)) { | ||
return step; | ||
} | ||
|
||
// 同一层总共有四种变法 | ||
for (int j = 0; j < 4; j++) { | ||
String plusStr = plusOne(str, j); | ||
if (!visited.contains(plusStr)) { | ||
visited.add(plusStr); | ||
queue.add(plusStr); | ||
} | ||
String minusStr = minusOne(str, j); | ||
if (!visited.contains(minusStr)) { | ||
visited.add(minusStr); | ||
queue.add(minusStr); | ||
} | ||
} | ||
} | ||
step++; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
private String plusOne(String str, int i) { | ||
char[] chaArr = str.toCharArray(); | ||
if (chaArr[i] == '9') { | ||
chaArr[i] = '0'; | ||
} else { | ||
chaArr[i] += 1; | ||
} | ||
|
||
return new String(chaArr); | ||
} | ||
|
||
private String minusOne(String str, int i) { | ||
char[] chaArr = str.toCharArray(); | ||
if (chaArr[i] == '0') { | ||
chaArr[i] = '9'; | ||
} else { | ||
chaArr[i] -= 1; | ||
} | ||
|
||
return new String(chaArr); | ||
} | ||
} | ||
//leetcode submit region end(Prohibit modification and deletion) | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
basic-learning/src/main/java/com/jacobs/basic/leetcode/editor/cn/all.json
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
basic-learning/src/main/java/com/jacobs/basic/leetcode/editor/cn/translation.json
Large diffs are not rendered by default.
Oops, something went wrong.