Merge remote-tracking branch 'origin/master'
# Conflicts: # src/main/java/leetcode/editor/cn/doc/all.json
This commit is contained in:
commit
7c122c490d
94
src/main/java/com/code/leet/week/Solution260.java
Normal file
94
src/main/java/com/code/leet/week/Solution260.java
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
package com.code.leet.week;
|
||||||
|
|
||||||
|
|
||||||
|
import com.code.leet.entiy.TwoArray;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description:
|
||||||
|
* @author: Administrator
|
||||||
|
* @date: 2021/8/22-10:29
|
||||||
|
*/
|
||||||
|
public class Solution260 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution260 solution = new Solution260();
|
||||||
|
TwoArray twoArray = new TwoArray("[[1,3,1,15],[1,3,3,1]]", true);
|
||||||
|
solution.gridGame(twoArray.getArr());
|
||||||
|
}
|
||||||
|
|
||||||
|
public int maximumDifference(int[] nums) {
|
||||||
|
int max = 0;
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
for (int j = i + 1; j < nums.length; j++) {
|
||||||
|
max = Math.max(max, nums[j] - nums[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max > 0 ? max : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long gridGame(int[][] grid) {
|
||||||
|
int size = grid[0].length;
|
||||||
|
long[][] sums = new long[2][size + 1];
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
sums[0][i + 1] = sums[0][i] + grid[0][i];
|
||||||
|
sums[1][i + 1] = sums[1][i] + grid[1][i];
|
||||||
|
}
|
||||||
|
long min = Long.MAX_VALUE;
|
||||||
|
for (int i = 1; i < size + 1; i++) {
|
||||||
|
min = Math.min(min, Math.max(sums[0][size] - sums[0][i], sums[1][0] - sums[1][i - 1]));
|
||||||
|
}
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean placeWordInCrossword(char[][] board, String word) {
|
||||||
|
int size = word.length();
|
||||||
|
for (int i = 0; i < board.length; i++) {
|
||||||
|
int blank = 0;
|
||||||
|
boolean add = false;
|
||||||
|
for (int j = 0; j < board[0].length; j++) {
|
||||||
|
if (size - blank > board[0].length - j) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (board[i][j] == ' ' && add) {
|
||||||
|
blank++;
|
||||||
|
} else if (board[i][j] == ' ' && !add && ((j > 1 && board[i][j - 1] == '#') || j == 0)) {
|
||||||
|
if (blank == size) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
add = true;
|
||||||
|
blank = 0;
|
||||||
|
} else if (board[i][j] != ' ' && board[i][j] != '#') {
|
||||||
|
if(word.contains(""+board[i][j])){
|
||||||
|
String temp = word;
|
||||||
|
int index = temp.indexOf(""+board[i][j]);
|
||||||
|
if(blank==index){
|
||||||
|
blank++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
add = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < board[0].length; i++) {
|
||||||
|
int blank = 0;
|
||||||
|
boolean add = false;
|
||||||
|
for (int j = 0; j < board.length; j++) {
|
||||||
|
if (size - blank > board.length - j) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (board[j][i] == ' ' && add) {
|
||||||
|
blank++;
|
||||||
|
} else if (board[j][i] == ' ' && !add && ((i > 1 && board[j][i - 1] == '#') || i == 0)) {
|
||||||
|
if (blank == size) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
add = true;
|
||||||
|
blank = 0;
|
||||||
|
} else {
|
||||||
|
add = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
126
src/main/java/leetcode/editor/cn/DecodeWaysIi.java
Normal file
126
src/main/java/leetcode/editor/cn/DecodeWaysIi.java
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
//一条包含字母 A-Z 的消息通过以下的方式进行了编码:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//'A' -> 1
|
||||||
|
//'B' -> 2
|
||||||
|
//...
|
||||||
|
//'Z' -> 26
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 要 解码 一条已编码的消息,所有的数字都必须分组,然后按原来的编码方案反向映射回字母(可能存在多种方式)。例如,"11106" 可以映射为:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// "AAJF" 对应分组 (1 1 10 6)
|
||||||
|
// "KJF" 对应分组 (11 10 6)
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 注意,像 (1 11 06) 这样的分组是无效的,因为 "06" 不可以映射为 'F' ,因为 "6" 与 "06" 不同。
|
||||||
|
//
|
||||||
|
// 除了 上面描述的数字字母映射方案,编码消息中可能包含 '*' 字符,可以表示从 '1' 到 '9' 的任一数字(不包括 '0')。例如,编码字符串 "1*
|
||||||
|
//" 可以表示 "11"、"12"、"13"、"14"、"15"、"16"、"17"、"18" 或 "19" 中的任意一条消息。对 "1*" 进行解码,相当于解码
|
||||||
|
//该字符串可以表示的任何编码消息。
|
||||||
|
//
|
||||||
|
// 给你一个字符串 s ,由数字和 '*' 字符组成,返回 解码 该字符串的方法 数目 。
|
||||||
|
//
|
||||||
|
// 由于答案数目可能非常大,返回对 10⁹ + 7 取余 的结果。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:s = "*"
|
||||||
|
//输出:9
|
||||||
|
//解释:这一条编码消息可以表示 "1"、"2"、"3"、"4"、"5"、"6"、"7"、"8" 或 "9" 中的任意一条。
|
||||||
|
//可以分别解码成字符串 "A"、"B"、"C"、"D"、"E"、"F"、"G"、"H" 和 "I" 。
|
||||||
|
//因此,"*" 总共有 9 种解码方法。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:s = "1*"
|
||||||
|
//输出:18
|
||||||
|
//解释:这一条编码消息可以表示 "11"、"12"、"13"、"14"、"15"、"16"、"17"、"18" 或 "19" 中的任意一条。
|
||||||
|
//每种消息都可以由 2 种方法解码(例如,"11" 可以解码成 "AA" 或 "K")。
|
||||||
|
//因此,"1*" 共有 9 * 2 = 18 种解码方法。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:s = "2*"
|
||||||
|
//输出:15
|
||||||
|
//解释:这一条编码消息可以表示 "21"、"22"、"23"、"24"、"25"、"26"、"27"、"28" 或 "29" 中的任意一条。
|
||||||
|
//"21"、"22"、"23"、"24"、"25" 和 "26" 由 2 种解码方法,但 "27"、"28" 和 "29" 仅有 1 种解码方法。
|
||||||
|
//因此,"2*" 共有 (6 * 2) + (3 * 1) = 12 + 3 = 15 种解码方法。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= s.length <= 10⁵
|
||||||
|
// s[i] 是 0 - 9 中的一位数字或字符 '*'
|
||||||
|
//
|
||||||
|
// Related Topics 字符串 动态规划 👍 137 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//639:解码方法 II
|
||||||
|
class DecodeWaysIi {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new DecodeWaysIi().new Solution();
|
||||||
|
solution.numDecodings("*1*1*0");
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
static final int MOD = 1000000007;
|
||||||
|
|
||||||
|
public int numDecodings(String s) {
|
||||||
|
int n = s.length();
|
||||||
|
long a = 0, b = 1, c = 0;
|
||||||
|
for (int i = 1; i <= n; ++i) {
|
||||||
|
c = b * check1digit(s.charAt(i - 1)) % MOD;
|
||||||
|
if (i > 1) {
|
||||||
|
c = (c + a * check2digits(s.charAt(i - 2), s.charAt(i - 1))) % MOD;
|
||||||
|
}
|
||||||
|
a = b;
|
||||||
|
b = c;
|
||||||
|
}
|
||||||
|
return (int) c;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int check1digit(char ch) {
|
||||||
|
if (ch == '0') {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return ch == '*' ? 9 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int check2digits(char c0, char c1) {
|
||||||
|
if (c0 == '*' && c1 == '*') {
|
||||||
|
return 15;
|
||||||
|
}
|
||||||
|
if (c0 == '*') {
|
||||||
|
return c1 <= '6' ? 2 : 1;
|
||||||
|
}
|
||||||
|
if (c1 == '*') {
|
||||||
|
if (c0 == '1') {
|
||||||
|
return 9;
|
||||||
|
}
|
||||||
|
if (c0 == '2') {
|
||||||
|
return 6;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return (c0 != '0' && (c0 - '0') * 10 + (c1 - '0') <= 26) ? 1 : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
70
src/main/java/leetcode/editor/cn/PowerOfThree.java
Normal file
70
src/main/java/leetcode/editor/cn/PowerOfThree.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
//给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 true ;否则,返回 false 。
|
||||||
|
//
|
||||||
|
// 整数 n 是 3 的幂次方需满足:存在整数 x 使得 n == 3ˣ
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 27
|
||||||
|
//输出:true
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 0
|
||||||
|
//输出:false
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 9
|
||||||
|
//输出:true
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 4:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:n = 45
|
||||||
|
//输出:false
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// -2³¹ <= n <= 2³¹ - 1
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 进阶:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 你能不使用循环或者递归来完成本题吗?
|
||||||
|
//
|
||||||
|
// Related Topics 递归 数学 👍 184 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//326:3的幂
|
||||||
|
class PowerOfThree {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new PowerOfThree().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public boolean isPowerOfThree(int n) {
|
||||||
|
return n > 0 && 1162261467 % n == 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
65
src/main/java/leetcode/editor/cn/doc/content/DecodeWaysIi.md
Normal file
65
src/main/java/leetcode/editor/cn/doc/content/DecodeWaysIi.md
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<p>一条包含字母 <code>A-Z</code> 的消息通过以下的方式进行了编码:</p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
'A' -> 1
|
||||||
|
'B' -> 2
|
||||||
|
...
|
||||||
|
'Z' -> 26
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p>要 <strong>解码</strong> 一条已编码的消息,所有的数字都必须分组,然后按原来的编码方案反向映射回字母(可能存在多种方式)。例如,<code>"11106"</code> 可以映射为:</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>"AAJF"</code> 对应分组 <code>(1 1 10 6)</code></li>
|
||||||
|
<li><code>"KJF"</code> 对应分组 <code>(11 10 6)</code></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p>注意,像 <code>(1 11 06)</code> 这样的分组是无效的,因为 <code>"06"</code> 不可以映射为 <code>'F'</code> ,因为 <code>"6"</code> 与 <code>"06"</code> 不同。</p>
|
||||||
|
|
||||||
|
<p><strong>除了</strong> 上面描述的数字字母映射方案,编码消息中可能包含 <code>'*'</code> 字符,可以表示从 <code>'1'</code> 到 <code>'9'</code> 的任一数字(不包括 <code>'0'</code>)。例如,编码字符串 <code>"1*"</code> 可以表示 <code>"11"</code>、<code>"12"</code>、<code>"13"</code>、<code>"14"</code>、<code>"15"</code>、<code>"16"</code>、<code>"17"</code>、<code>"18"</code> 或 <code>"19"</code> 中的任意一条消息。对 <code>"1*"</code> 进行解码,相当于解码该字符串可以表示的任何编码消息。</p>
|
||||||
|
|
||||||
|
<p>给你一个字符串 <code>s</code> ,由数字和 <code>'*'</code> 字符组成,返回 <strong>解码</strong> 该字符串的方法 <strong>数目</strong> 。</p>
|
||||||
|
|
||||||
|
<p>由于答案数目可能非常大,返回对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 的结果。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = "*"
|
||||||
|
<strong>输出:</strong>9
|
||||||
|
<strong>解释:</strong>这一条编码消息可以表示 "1"、"2"、"3"、"4"、"5"、"6"、"7"、"8" 或 "9" 中的任意一条。
|
||||||
|
可以分别解码成字符串 "A"、"B"、"C"、"D"、"E"、"F"、"G"、"H" 和 "I" 。
|
||||||
|
因此,"*" 总共有 9 种解码方法。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = "1*"
|
||||||
|
<strong>输出:</strong>18
|
||||||
|
<strong>解释:</strong>这一条编码消息可以表示 "11"、"12"、"13"、"14"、"15"、"16"、"17"、"18" 或 "19" 中的任意一条。
|
||||||
|
每种消息都可以由 2 种方法解码(例如,"11" 可以解码成 "AA" 或 "K")。
|
||||||
|
因此,"1*" 共有 9 * 2 = 18 种解码方法。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = "2*"
|
||||||
|
<strong>输出:</strong>15
|
||||||
|
<strong>解释:</strong>这一条编码消息可以表示 "21"、"22"、"23"、"24"、"25"、"26"、"27"、"28" 或 "29" 中的任意一条。
|
||||||
|
"21"、"22"、"23"、"24"、"25" 和 "26" 由 2 种解码方法,但 "27"、"28" 和 "29" 仅有 1 种解码方法。
|
||||||
|
因此,"2*" 共有 (6 * 2) + (3 * 1) = 12 + 3 = 15 种解码方法。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||||
|
<li><code>s[i]</code> 是 <code>0 - 9</code> 中的一位数字或字符 <code>'*'</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>字符串</li><li>动态规划</li></div></div><br><div><li>👍 137</li><li>👎 0</li></div>
|
50
src/main/java/leetcode/editor/cn/doc/content/PowerOfThree.md
Normal file
50
src/main/java/leetcode/editor/cn/doc/content/PowerOfThree.md
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<p>给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||||
|
|
||||||
|
<p>整数 <code>n</code> 是 3 的幂次方需满足:存在整数 <code>x</code> 使得 <code>n == 3<sup>x</sup></code></p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 27
|
||||||
|
<strong>输出:</strong>true
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 0
|
||||||
|
<strong>输出:</strong>false
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 9
|
||||||
|
<strong>输出:</strong>true
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 4:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 45
|
||||||
|
<strong>输出:</strong>false
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>进阶:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>你能不使用循环或者递归来完成本题吗?</li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>递归</li><li>数学</li></div></div><br><div><li>👍 184</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user