-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
14 changed files
with
738 additions
and
23 deletions.
There are no files selected for viewing
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
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
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,57 @@ | ||
package bfs; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* @BelongsProject: LeetCode | ||
* @BelongsPackage: bfs | ||
* @Author: elvis | ||
* @CreateTime: 2020-08-28 19:57 | ||
* @Description: 单词接龙 | ||
*/ | ||
public class Leetcode126 { | ||
|
||
public static int minLength(String beginWord, String endWord, List<String> wordList) { | ||
Set<String> beginSet = new HashSet<>(); | ||
Set<String> endSet = new HashSet<>(); | ||
beginSet.add(beginWord); | ||
endSet.add(endWord); | ||
Set<String> dict = new HashSet<>(wordList); | ||
if (!dict.contains(endWord)) return 0; | ||
return search(beginSet, endSet, dict, 1); | ||
} | ||
|
||
private static int search(Set<String> beginSet, Set<String> endSet, Set<String> dict, int cnt) { | ||
if (beginSet.isEmpty() || endSet.isEmpty()) return 0; | ||
cnt++; | ||
dict.removeAll(beginSet); | ||
Set<String> nextSet = new HashSet<>(); | ||
for (String str : beginSet) { | ||
char[] chs = str.toCharArray(); | ||
for (int i = 0; i < chs.length; i++) { | ||
char c = chs[i]; | ||
for (char j = 'a'; j <= 'z'; j++) { | ||
chs[i] = j; | ||
String tmp = new String(chs); | ||
if (!dict.contains(tmp)) continue; | ||
if (endSet.contains(tmp)) return cnt; | ||
nextSet.add(tmp); | ||
} | ||
chs[i] = c; | ||
} | ||
} | ||
return nextSet.size() > endSet.size() ? search(endSet, nextSet, dict, cnt) : search(nextSet, endSet, dict, cnt); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
String begin = sc.nextLine(); | ||
String end = sc.nextLine(); | ||
String para = sc.nextLine(); | ||
String[] dict = para.split(" "); | ||
List<String> wordList = new ArrayList<>(); | ||
for (String s : dict) wordList.add(s); | ||
int ret = minLength(begin, end, wordList); | ||
System.out.println(ret); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -50,7 +50,6 @@ public int hammingWeight(int n) { | |
res += n & 1; | ||
n >>>= 1; | ||
} | ||
|
||
return res; | ||
} | ||
|
||
|
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
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,97 @@ | ||
package contest.weekly200; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @BelongsProject: LeetCode | ||
* @BelongsPackage: contest.weekly200 | ||
* @Author: elvis | ||
* @CreateTime: 2020-08-23 21:58 | ||
* @Description: 统计好三元组 | ||
* 给你一个整数数组 arr ,以及 a、b 、c 三个整数。请你统计其中好三元组的数量。 | ||
* | ||
* 如果三元组 (arr[i], arr[j], arr[k]) 满足下列全部条件,则认为它是一个 好三元组 。 | ||
* | ||
* 0 <= i < j < k < arr.length | ||
* |arr[i] - arr[j]| <= a | ||
* |arr[j] - arr[k]| <= b | ||
* |arr[i] - arr[k]| <= c | ||
* 其中 |x| 表示 x 的绝对值。 | ||
* | ||
* 返回 好三元组的数量 。 | ||
* | ||
* | ||
* | ||
* 示例 1: | ||
* | ||
* 输入:arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3 | ||
* 输出:4 | ||
* 解释:一共有 4 个好三元组:[(3,0,1), (3,0,1), (3,1,1), (0,1,1)] 。 | ||
* 示例 2: | ||
* | ||
* 输入:arr = [1,1,2,2,3], a = 0, b = 0, c = 1 | ||
* 输出:0 | ||
* 解释:不存在满足所有条件的三元组。 | ||
* | ||
* | ||
* 提示: | ||
* | ||
* 3 <= arr.length <= 100 | ||
* 0 <= arr[i] <= 1000 | ||
* 0 <= a, b, c <= 1000 | ||
*/ | ||
public class Leetcode1534 { | ||
|
||
public int countGoodTriplets(int[] arr, int a, int b, int c) { | ||
int n = arr.length; | ||
int ct = 0; | ||
for(int i = 0;i < n;i++){ | ||
for(int j = i+1;j < n;j++){ | ||
for(int k = j+1;k < n;k++){ | ||
if( | ||
Math.abs(arr[j] - arr[i]) <= a && | ||
Math.abs(arr[k] - arr[j]) <= b && | ||
Math.abs(arr[k] - arr[i]) <= c | ||
){ | ||
ct++; | ||
} | ||
|
||
} | ||
} | ||
} | ||
return ct; | ||
} | ||
|
||
List<List<String>> ret=new ArrayList<>(); | ||
public List<List<String>> partition(String s) { | ||
List<String> list=new ArrayList<>(); | ||
back(s,list,0); | ||
return ret; | ||
} | ||
|
||
private void back(String s, List<String> list, int idx) { | ||
if(idx==s.length()){ | ||
ret.add(new ArrayList<>(list)); | ||
return; | ||
} | ||
for (int i = idx; i < s.length(); i++) { | ||
String str=s.substring(idx,i+1); | ||
if(isPalindrome(str)){ | ||
list.add(str); | ||
back(s,list,i+1); | ||
list.remove(list.size()-1); | ||
} | ||
} | ||
} | ||
|
||
private boolean isPalindrome(String str) { | ||
int l=0,r=str.length()-1; | ||
while(l<r){ | ||
if(str.charAt(l++)!=str.charAt(r--)) return false; | ||
} | ||
return true; | ||
} | ||
|
||
|
||
} |
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,94 @@ | ||
package contest.weekly200; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.Deque; | ||
import java.util.stream.IntStream; | ||
|
||
/** | ||
* @BelongsProject: LeetCode | ||
* @BelongsPackage: contest.weekly200 | ||
* @Author: elvis | ||
* @CreateTime: 2020-08-23 21:57 | ||
* @Description: 找出数组游戏的赢家 | ||
* 给你一个由 不同 整数组成的整数数组 arr 和一个整数 k 。 | ||
* | ||
* 每回合游戏都在数组的前两个元素(即 arr[0] 和 arr[1] )之间进行。比较 arr[0] 与 arr[1] 的大小,较大的整数将会取得这一回合的胜利并保留在位置 0 ,较小的整数移至数组的末尾。当一个整数赢得 k 个连续回合时,游戏结束,该整数就是比赛的 赢家 。 | ||
* | ||
* 返回赢得比赛的整数。 | ||
* | ||
* 题目数据 保证 游戏存在赢家。 | ||
* | ||
* 示例 1: | ||
* | ||
* 输入:arr = [2,1,3,5,4,6,7], k = 2 | ||
* 输出:5 | ||
* 解释:一起看一下本场游戏每回合的情况: | ||
* | ||
* 因此将进行 4 回合比赛,其中 5 是赢家,因为它连胜 2 回合。 | ||
* 示例 2: | ||
* | ||
* 输入:arr = [3,2,1], k = 10 | ||
* 输出:3 | ||
* 解释:3 将会在前 10 个回合中连续获胜。 | ||
* 示例 3: | ||
* | ||
* 输入:arr = [1,9,8,2,3,7,6,4,5], k = 7 | ||
* 输出:9 | ||
* 示例 4: | ||
* | ||
* 输入:arr = [1,11,22,33,44,55,66,77,88,99], k = 1000000000 | ||
* 输出:99 | ||
* | ||
* | ||
* 提示: | ||
* | ||
* 2 <= arr.length <= 10^5 | ||
* 1 <= arr[i] <= 10^6 | ||
* arr 所含的整数 各不相同 。 | ||
* 1 <= k <= 10^9 | ||
*/ | ||
public class Leetcode1535 { | ||
|
||
public int getWinner(int[] arr, int k) { | ||
if (k >= arr.length || arr.length == 1) return IntStream.of(arr).max().getAsInt(); | ||
Deque<Integer> deque=new ArrayDeque<>(); | ||
for(int i=0;i<arr.length;i++) deque.addLast(arr[i]); | ||
int win=0; | ||
while(true){ | ||
int x=deque.pollFirst(); | ||
int y=deque.pollFirst(); | ||
if(x>y){ | ||
win++; | ||
if(win>=k) return x; | ||
deque.addFirst(x); | ||
deque.addLast(y); | ||
}else{ | ||
win=1; | ||
if(win>=k) return y; | ||
deque.addFirst(y); | ||
deque.addLast(x); | ||
} | ||
} | ||
|
||
} | ||
|
||
public int getWinner2(int[] arr, int k) { | ||
int n = arr.length; | ||
k = Math.min(k, n); | ||
int A = arr[0]; | ||
int rep = 0; | ||
for(int i = 1;;i++){ | ||
int B = arr[i%n]; | ||
if(A >= B){ | ||
rep++; | ||
}else{ | ||
A = B; | ||
rep = 1; | ||
} | ||
if(rep == k){ | ||
return A; | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.