Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/main/java/leetcode/editor/cn/all.json
#	src/main/java/leetcode/editor/cn/translation.json
This commit is contained in:
huangge1199@hotmail.com 2021-05-30 21:18:51 +08:00
commit 5062a8a76f
18 changed files with 939 additions and 2 deletions

View File

@ -0,0 +1,58 @@
//黑板上写着一个非负整数数组 nums[i] Alice Bob 轮流从黑板上擦掉一个数字Alice 先手如果擦除一个数字后剩余的所有数字按位异或
//运算得出的结果等于 0 的话当前玩家游戏失败 (另外如果只剩一个数字按位异或运算得到它本身如果无数字剩余按位异或运算结果为 0
//
// 换种说法就是轮到某个玩家时如果当前黑板上所有数字按位异或运算结果等于 0这个玩家获胜
//
// 假设两个玩家每步都使用最优解当且仅当 Alice 获胜时返回 true
//
//
//
// 示例
//
//
//输入: nums = [1, 1, 2]
//输出: false
//解释:
//Alice 有两个选择: 擦掉数字 1 2
//如果擦掉 1, 数组变成 [1, 2]剩余数字按位异或得到 1 XOR 2 = 3那么 Bob 可以擦掉任意数字因为 Alice 会成为擦掉最后一个数
//字的人她总是会输
//如果 Alice 擦掉 2那么数组变成[1, 1]剩余数字按位异或得到 1 XOR 1 = 0Alice 仍然会输掉游戏
//
//
//
//
// 提示
//
//
// 1 <= N <= 1000
// 0 <= nums[i] <= 2^16
//
// Related Topics 数学
// 👍 44 👎 0
package leetcode.editor.cn;
//810:黑板异或游戏
public class ChalkboardXorGame {
public static void main(String[] args) {
//测试代码
Solution solution = new ChalkboardXorGame().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean xorGame(int[] nums) {
if (nums.length % 2 == 0) {
return true;
}
int xor = 0;
for (int num : nums) {
xor ^= num;
}
return xor == 0;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,28 @@
<p>黑板上写着一个非负整数数组 <code>nums[i]</code> 。Alice 和 Bob 轮流从黑板上擦掉一个数字Alice 先手。如果擦除一个数字后,剩余的所有数字按位异或运算得出的结果等于 0 的话,当前玩家游戏失败。 (另外如果只剩一个数字按位异或运算得到它本身如果无数字剩余按位异或运算结果为 0。</p>
<p>换种说法就是,轮到某个玩家时,如果当前黑板上所有数字按位异或运算结果等于 0这个玩家获胜。</p>
<p>假设两个玩家每步都使用最优解,当且仅当 Alice 获胜时返回 <code>true</code></p>
<p> </p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入:</strong> nums = [1, 1, 2]
<strong>输出:</strong> false
<strong>解释:</strong>
Alice 有两个选择: 擦掉数字 1 或 2。
如果擦掉 1, 数组变成 [1, 2]。剩余数字按位异或得到 1 XOR 2 = 3。那么 Bob 可以擦掉任意数字,因为 Alice 会成为擦掉最后一个数字的人,她总是会输。
如果 Alice 擦掉 2那么数组变成[1, 1]。剩余数字按位异或得到 1 XOR 1 = 0。Alice 仍然会输掉游戏。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= N <= 1000</code></li>
<li><code>0 <= nums[i] <= 2^16</code></li>
</ul>
<div><div>Related Topics</div><div><li>数学</li></div></div>\n<div><li>👍 44</li><li>👎 0</li></div>

View File

@ -0,0 +1,111 @@
//请你设计一个支持下述操作的栈
//
// 实现自定义栈类 CustomStack
//
//
// CustomStack(int maxSize) maxSize 初始化对象maxSize 是栈中最多能容纳的元素数量栈在增长到 maxSize
//之后则不支持 push 操作
// void push(int x)如果栈还未增长到 maxSize 就将 x 添加到栈顶
// int pop()弹出栈顶元素并返回栈顶的值或栈为空时返回 -1
// void inc(int k, int val)栈底的 k 个元素的值都增加 val 如果栈中元素总数小于 k 则栈中的所有元素都增加 val
//
//
//
//
//
// 示例
//
// 输入
//["CustomStack","push","push","pop","push","push","push","increment","increment
//","pop","pop","pop","pop"]
//[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
//输出
//[null,null,null,2,null,null,null,null,null,103,202,201,-1]
//解释
//CustomStack customStack = new CustomStack(3); // 栈是空的 []
//customStack.push(1); // 栈变为 [1]
//customStack.push(2); // 栈变为 [1, 2]
//customStack.pop(); // 返回 2 --> 返回栈顶值 2栈变为 [1]
//customStack.push(2); // 栈变为 [1, 2]
//customStack.push(3); // 栈变为 [1, 2, 3]
//customStack.push(4); // 栈仍然是 [1, 2, 3]不能添加其他元素使栈大小变为
// 4
//customStack.increment(5, 100); // 栈变为 [101, 102, 103]
//customStack.increment(2, 100); // 栈变为 [201, 202, 103]
//customStack.pop(); // 返回 103 --> 返回栈顶值 103栈变为 [201
//, 202]
//customStack.pop(); // 返回 202 --> 返回栈顶值 202栈变为 [201
//]
//customStack.pop(); // 返回 201 --> 返回栈顶值 201栈变为 []
//customStack.pop(); // 返回 -1 --> 栈为空返回 -1
//
//
//
//
// 提示
//
//
// 1 <= maxSize <= 1000
// 1 <= x <= 1000
// 1 <= k <= 1000
// 0 <= val <= 100
// 每种方法 incrementpush 以及 pop 分别最多调用 1000
//
// Related Topics 设计
// 👍 47 👎 0
package leetcode.editor.cn;
//1381:设计一个支持增量操作的栈
public class DesignAStackWithIncrementOperation {
public static void main(String[] args) {
//测试代码
// Solution solution = new DesignAStackWithIncrementOperation().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class CustomStack {
int[] stack;
int index;
public CustomStack(int maxSize) {
stack = new int[maxSize];
index = 0;
}
public void push(int x) {
if (index < stack.length) {
stack[index] = x;
index++;
}
}
public int pop() {
if(index==0){
return -1;
}
index--;
int num = stack[index];
stack[index] = 0;
return num;
}
public void increment(int k, int val) {
int size = Math.min(index, k);
for (int i = 0; i < size; i++) {
stack[i] += val;
}
}
}
/**
* Your CustomStack object will be instantiated and called as such:
* CustomStack obj = new CustomStack(maxSize);
* obj.push(x);
* int param_2 = obj.pop();
* obj.increment(k,val);
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,48 @@
<p>请你设计一个支持下述操作的栈。</p>
<p>实现自定义栈类 <code>CustomStack</code> </p>
<ul>
<li><code>CustomStack(int maxSize)</code>:用 <code>maxSize</code> 初始化对象,<code>maxSize</code> 是栈中最多能容纳的元素数量,栈在增长到 <code>maxSize</code> 之后则不支持 <code>push</code> 操作。</li>
<li><code>void push(int x)</code>:如果栈还未增长到 <code>maxSize</code> ,就将 <code>x</code> 添加到栈顶。</li>
<li><code>int pop()</code>:弹出栈顶元素,并返回栈顶的值,或栈为空时返回 <strong>-1</strong></li>
<li><code>void inc(int k, int val)</code>:栈底的 <code>k</code> 个元素的值都增加 <code>val</code> 。如果栈中元素总数小于 <code>k</code> ,则栈中的所有元素都增加 <code>val</code></li>
</ul>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>
[&quot;CustomStack&quot;,&quot;push&quot;,&quot;push&quot;,&quot;pop&quot;,&quot;push&quot;,&quot;push&quot;,&quot;push&quot;,&quot;increment&quot;,&quot;increment&quot;,&quot;pop&quot;,&quot;pop&quot;,&quot;pop&quot;,&quot;pop&quot;]
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
<strong>输出:</strong>
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
<strong>解释:</strong>
CustomStack customStack = new CustomStack(3); // 栈是空的 []
customStack.push(1); // 栈变为 [1]
customStack.push(2); // 栈变为 [1, 2]
customStack.pop(); // 返回 2 --&gt; 返回栈顶值 2栈变为 [1]
customStack.push(2); // 栈变为 [1, 2]
customStack.push(3); // 栈变为 [1, 2, 3]
customStack.push(4); // 栈仍然是 [1, 2, 3],不能添加其他元素使栈大小变为 4
customStack.increment(5, 100); // 栈变为 [101, 102, 103]
customStack.increment(2, 100); // 栈变为 [201, 202, 103]
customStack.pop(); // 返回 103 --&gt; 返回栈顶值 103栈变为 [201, 202]
customStack.pop(); // 返回 202 --&gt; 返回栈顶值 202栈变为 [201]
customStack.pop(); // 返回 201 --&gt; 返回栈顶值 201栈变为 []
customStack.pop(); // 返回 -1 --&gt; 栈为空,返回 -1
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= maxSize &lt;= 1000</code></li>
<li><code>1 &lt;= x &lt;= 1000</code></li>
<li><code>1 &lt;= k &lt;= 1000</code></li>
<li><code>0 &lt;= val &lt;= 100</code></li>
<li>每种方法 <code>increment</code><code>push</code> 以及 <code>pop</code> 分别最多调用 <code>1000</code></li>
</ul>
<div><div>Related Topics</div><div><li></li><li>设计</li></div></div>\n<div><li>👍 47</li><li>👎 0</li></div>

View File

@ -0,0 +1,43 @@
//两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目
//
// 给出两个整数 x y计算它们之间的汉明距离
//
// 注意
//0 x, y < 231.
//
// 示例:
//
//
//输入: x = 1, y = 4
//
//输出: 2
//
//解释:
//1 (0 0 0 1)
//4 (0 1 0 0)
//
//
//上面的箭头指出了对应二进制位不同的位置
//
// Related Topics 位运算
// 👍 426 👎 0
package leetcode.editor.cn;
//461:汉明距离
public class HammingDistance {
public static void main(String[] args) {
//测试代码
Solution solution = new HammingDistance().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int hammingDistance(int x, int y) {
return Integer.bitCount(x ^ y);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,22 @@
<p>两个整数之间的<a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E8%B7%9D%E7%A6%BB">汉明距离</a>指的是这两个数字对应二进制位不同的位置的数目。</p>
<p>给出两个整数 <code>x</code><code>y</code>,计算它们之间的汉明距离。</p>
<p><strong>注意:</strong><br />
0 &le; <code>x</code>, <code>y</code> &lt; 2<sup>31</sup>.</p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入:</strong> x = 1, y = 4
<strong>输出:</strong> 2
<strong>解释:</strong>
1 (0 0 0 1)
4 (0 1 0 0)
&uarr; &uarr;
上面的箭头指出了对应二进制位不同的位置。
</pre>
<div><div>Related Topics</div><div><li>位运算</li></div></div>\n<div><li>👍 426</li><li>👎 0</li></div>

View File

@ -0,0 +1,90 @@
//HTML 实体解析器 是一种特殊的解析器它将 HTML 代码作为输入并用字符本身替换掉所有这些特殊的字符实体
//
// HTML 里这些特殊字符和它们对应的字符实体包括
//
//
// 双引号字符实体为 &quot; 对应的字符是 "
// 单引号字符实体为 &apos; 对应的字符是 '
// 与符号字符实体为 &amp; 对应对的字符是 &
// 大于号字符实体为 &gt; 对应的字符是 >
// 小于号字符实体为 &lt; 对应的字符是 <
// 斜线号字符实体为 &frasl; 对应的字符是 /
//
//
// 给你输入字符串 text 请你实现一个 HTML 实体解析器返回解析器解析后的结果
//
//
//
// 示例 1
//
//
//输入text = "&amp; is an HTML entity but &ambassador; is not."
//输出"& is an HTML entity but &ambassador; is not."
//解释解析器把字符实体 &amp; & 替换
//
//
// 示例 2
//
//
//输入text = "and I quote: &quot;...&quot;"
//输出"and I quote: \"...\""
//
//
// 示例 3
//
//
//输入text = "Stay home! Practice on Leetcode :)"
//输出"Stay home! Practice on Leetcode :)"
//
//
// 示例 4
//
//
//输入text = "x &gt; y &amp;&amp; x &lt; y is always false"
//输出"x > y && x < y is always false"
//
//
// 示例 5
//
//
//输入text = "leetcode.com&frasl;problemset&frasl;all"
//输出"leetcode.com/problemset/all"
//
//
//
//
// 提示
//
//
// 1 <= text.length <= 10^5
// 字符串可能包含 256 个ASCII 字符中的任意字符
//
// Related Topics 字符串
// 👍 12 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//1410:HTML 实体解析器
public class HtmlEntityParser{
public static void main(String[] args) {
//测试代码
Solution solution = new HtmlEntityParser().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String entityParser(String text) {
return text.
replace("&quot;", "\"").
replace("&apos;", "'").
replace("&gt;", ">").
replace("&lt;", "<").
replace("&frasl;", "/").
replace("&amp;", "&");
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,62 @@
<p>「HTML&nbsp;实体解析器」 是一种特殊的解析器,它将 HTML 代码作为输入,并用字符本身替换掉所有这些特殊的字符实体。</p>
<p>HTML 里这些特殊字符和它们对应的字符实体包括:</p>
<ul>
<li><strong>双引号:</strong>字符实体为&nbsp;<code>&amp;quot;</code>&nbsp;,对应的字符是&nbsp;<code>&quot;</code>&nbsp;</li>
<li><strong>单引号:</strong>字符实体为&nbsp;<code>&amp;apos;</code>&nbsp;,对应的字符是&nbsp;<code>&#39;</code>&nbsp;</li>
<li><strong>与符号:</strong>字符实体为&nbsp;<code>&amp;amp;</code>&nbsp;,对应对的字符是&nbsp;<code>&amp;</code>&nbsp;</li>
<li><strong>大于号:</strong>字符实体为&nbsp;<code>&amp;gt;</code>&nbsp;,对应的字符是&nbsp;<code>&gt;</code>&nbsp;</li>
<li><strong>小于号:</strong>字符实体为&nbsp;<code>&amp;lt;</code>&nbsp;,对应的字符是&nbsp;<code>&lt;</code>&nbsp;</li>
<li><strong>斜线号:</strong>字符实体为&nbsp;<code>&amp;frasl;</code>&nbsp;,对应的字符是&nbsp;<code>/</code>&nbsp;</li>
</ul>
<p>给你输入字符串&nbsp;<code>text</code>&nbsp;,请你实现一个 HTML&nbsp;实体解析器,返回解析器解析后的结果。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>text = &quot;&amp;amp; is an HTML entity but &amp;ambassador; is not.&quot;
<strong>输出:</strong>&quot;&amp; is an HTML entity but &amp;ambassador; is not.&quot;
<strong>解释:</strong>解析器把字符实体 &amp;amp; 用 &amp; 替换
</pre>
<p><strong>示例&nbsp;2</strong></p>
<pre>
<strong>输入:</strong>text = &quot;and I quote: &amp;quot;...&amp;quot;&quot;
<strong>输出:</strong>&quot;and I quote: \&quot;...\&quot;&quot;
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>text = &quot;Stay home! Practice on Leetcode :)&quot;
<strong>输出:</strong>&quot;Stay home! Practice on Leetcode :)&quot;
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>text = &quot;x &amp;gt; y &amp;amp;&amp;amp; x &amp;lt; y is always false&quot;
<strong>输出:</strong>&quot;x &gt; y &amp;&amp; x &lt; y is always false&quot;
</pre>
<p><strong>示例 5</strong></p>
<pre>
<strong>输入:</strong>text = &quot;leetcode.com&amp;frasl;problemset&amp;frasl;all&quot;
<strong>输出:</strong>&quot;leetcode.com/problemset/all&quot;
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= text.length &lt;= 10^5</code></li>
<li>字符串可能包含 256 个ASCII 字符中的任意字符。</li>
</ul>
<div><div>Related Topics</div><div><li></li><li>字符串</li></div></div>\n<div><li>👍 12</li><li>👎 0</li></div>

View File

@ -0,0 +1,101 @@
//给你一个整数数组 nums 和一个整数 k 区间 [left, right]left <= right 异或结果 是对下标位于 left rig
//ht包括 left right 之间所有元素进行 XOR 运算的结果nums[left] XOR nums[left+1] XOR ... XOR n
//ums[right]
//
// 返回数组中 要更改的最小元素数 以使所有长度为 k 的区间异或结果等于零
//
//
//
// 示例 1
//
//
//输入nums = [1,2,0,3,0], k = 1
//输出3
//解释将数组 [1,2,0,3,0] 修改为 [0,0,0,0,0]
//
//
// 示例 2
//
//
//输入nums = [3,4,5,2,1,7,3,4,7], k = 3
//输出3
//解释将数组 [3,4,5,2,1,7,3,4,7] 修改为 [3,4,7,3,4,7,3,4,7]
//
//
// 示例 3
//
//
//输入nums = [1,2,4,1,2,5,1,2,6], k = 3
//输出3
//解释将数组[1,2,4,1,2,5,1,2,6] 修改为 [1,2,3,1,2,3,1,2,3]
//
//
//
// 提示
//
//
// 1 <= k <= nums.length <= 2000
// 0 <= nums[i] < 210
//
// Related Topics 动态规划
// 👍 57 👎 0
package leetcode.editor.cn;
import java.util.*;
//1787:使所有区间的异或结果为零
public class MakeTheXorOfAllSegmentsEqualToZero {
public static void main(String[] args) {
//测试代码
Solution solution = new MakeTheXorOfAllSegmentsEqualToZero().new Solution();
//3
System.out.println(solution.minChanges(new int[]{1,2,0,3,0},1));
//3
System.out.println(solution.minChanges(new int[]{3,4,5,2,1,7,3,4,7},3));
//3
System.out.println(solution.minChanges(new int[]{1, 2, 4, 1, 2, 5, 1, 2, 6}, 3));
//11
System.out.println(solution.minChanges(new int[]{26,19,19,28,13,14,6,25,28,19,0,15,25,11}, 3));
//11
System.out.println(solution.minChanges(new int[]{11,20,3,18,26,30,20,7,3,0,25,9,19,21,3,23}, 5));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minChanges(int[] nums, int k) {
int numCount = 1 << 10;
List<Map<Integer, Integer>> list = new ArrayList<>();
for (int i = 0; i < k; i++) {
list.add(new HashMap<>());
}
for (int i = 0; i < nums.length; i++) {
int index = i % k;
Map<Integer, Integer> map = list.get(index);
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
list.set(index, map);
}
int[] flag = new int[numCount];
Arrays.fill(flag, Integer.MAX_VALUE / 2);
flag[0] = 0;
int length = nums.length % k == 0 ? nums.length / k : nums.length / k + 1;
for (int i = 0; i < k; i++) {
Map<Integer, Integer> map = list.get(i);
int min = Arrays.stream(flag).min().getAsInt();
int[] temp = new int[numCount];
Arrays.fill(temp, min);
for (int j = 0; j < numCount; j++) {
for (int key : map.keySet()) {
temp[j] = Math.min(temp[j], flag[j ^ key] - map.get(key));
}
temp[j] += i < nums.length % k || nums.length % k == 0 ? length : length - 1;
}
flag = temp;
}
return flag[0];
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,38 @@
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> 。区间 <code>[left, right]</code><code>left <= right</code>)的 <strong>异或结果</strong> 是对下标位于 <code>left</code><code>right</code>(包括 <code>left</code><code>right</code> )之间所有元素进行 <code>XOR</code> 运算的结果:<code>nums[left] XOR nums[left+1] XOR ... XOR nums[right]</code></p>
<p>返回数组中 <strong>要更改的最小元素数</strong> ,以使所有长度为 <code>k</code> 的区间异或结果等于零。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,0,3,0], k = 1
<strong>输出:</strong>3
<strong>解释:</strong>将数组 [<strong>1</strong>,<strong>2</strong>,0,<strong>3</strong>,0] 修改为 [<strong>0</strong>,<strong>0</strong>,0,<strong>0</strong>,0]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [3,4,5,2,1,7,3,4,7], k = 3
<strong>输出:</strong>3
<strong>解释:</strong>将数组 [3,4,<strong>5</strong>,<strong>2</strong>,<strong>1</strong>,7,3,4,7] 修改为 [3,4,<strong>7</strong>,<strong>3</strong>,<strong>4</strong>,7,3,4,7]
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,4,1,2,5,1,2,6], k = 3
<strong>输出:</strong>3
<strong>解释:</strong>将数组[1,2,<strong>4,</strong>1,2,<strong>5</strong>,1,2,<strong>6</strong>] 修改为 [1,2,<strong>3</strong>,1,2,<strong>3</strong>,1,2,<strong>3</strong>]</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= k <= nums.length <= 2000</code></li>
<li><code>0 <= nums[i] < 2<sup>10</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>动态规划</li></div></div>\n<div><li>👍 57</li><li>👎 0</li></div>

View File

@ -0,0 +1,96 @@
//给你一个由 '('')' 和小写字母组成的字符串 s
//
// 你需要从字符串中删除最少数目的 '(' 或者 ')' 可以删除任意位置的括号)使得剩下的括号字符串有效
//
// 请返回任意一个合法字符串
//
// 有效括号字符串应当符合以下 任意一条 要求
//
//
// 空字符串或只包含小写字母的字符串
// 可以被写作 ABA 连接 B的字符串其中 A B 都是有效括号字符串
// 可以被写作 (A) 的字符串其中 A 是一个有效的括号字符串
//
//
//
//
// 示例 1
//
// 输入s = "lee(t(c)o)de)"
//输出"lee(t(c)o)de"
//解释"lee(t(co)de)" , "lee(t(c)ode)" 也是一个可行答案
//
//
// 示例 2
//
// 输入s = "a)b(c)d"
//输出"ab(c)d"
//
//
// 示例 3
//
// 输入s = "))(("
//输出""
//解释空字符串也是有效的
//
//
// 示例 4
//
// 输入s = "(a(b(c)d)"
//输出"a(b(c)d)"
//
//
//
//
// 提示
//
//
// 1 <= s.length <= 10^5
// s[i] 可能是 '('')' 或英文小写字母
//
// Related Topics 字符串
// 👍 112 👎 0
package leetcode.editor.cn;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
//1249:移除无效的括号
public class MinimumRemoveToMakeValidParentheses {
public static void main(String[] args) {
//测试代码
Solution solution = new MinimumRemoveToMakeValidParentheses().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String minRemoveToMakeValid(String s) {
Stack<Integer> stack = new Stack<>();
Set<Integer> remove = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
if(s.charAt(i)=='('){
stack.push(i);
}else if(s.charAt(i)==')'&&stack.isEmpty()){
remove.add(i);
}else if(s.charAt(i)==')'&&!stack.isEmpty()){
stack.pop();
}
}
while (!stack.isEmpty()){
remove.add(stack.pop());
}
StringBuilder str = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if(!remove.contains(i)){
str.append(s.charAt(i));
}
}
return str.toString();
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,51 @@
<p>给你一个由 <code>&#39;(&#39;</code><code>&#39;)&#39;</code> 和小写字母组成的字符串 <code>s</code></p>
<p>你需要从字符串中删除最少数目的 <code>&#39;(&#39;</code> 或者 <code>&#39;)&#39;</code>&nbsp;(可以删除任意位置的括号),使得剩下的「括号字符串」有效。</p>
<p>请返回任意一个合法字符串。</p>
<p>有效「括号字符串」应当符合以下&nbsp;<strong>任意一条&nbsp;</strong>要求:</p>
<ul>
<li>空字符串或只包含小写字母的字符串</li>
<li>可以被写作&nbsp;<code>AB</code><code>A</code>&nbsp;连接&nbsp;<code>B</code>)的字符串,其中&nbsp;<code>A</code>&nbsp;&nbsp;<code>B</code>&nbsp;都是有效「括号字符串」</li>
<li>可以被写作&nbsp;<code>(A)</code>&nbsp;的字符串,其中&nbsp;<code>A</code>&nbsp;是一个有效的「括号字符串」</li>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>s = &quot;lee(t(c)o)de)&quot;
<strong>输出:</strong>&quot;lee(t(c)o)de&quot;
<strong>解释:</strong>&quot;lee(t(co)de)&quot; , &quot;lee(t(c)ode)&quot; 也是一个可行答案。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>s = &quot;a)b(c)d&quot;
<strong>输出:</strong>&quot;ab(c)d&quot;
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>s = &quot;))((&quot;
<strong>输出:</strong>&quot;&quot;
<strong>解释:</strong>空字符串也是有效的
</pre>
<p><strong>示例 4</strong></p>
<pre><strong>输入:</strong>s = &quot;(a(b(c)d)&quot;
<strong>输出:</strong>&quot;a(b(c)d)&quot;
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 10^5</code></li>
<li><code>s[i]</code>&nbsp;可能是&nbsp;<code>&#39;(&#39;</code><code>&#39;)&#39;</code>&nbsp;或英文小写字母</li>
</ul>
<div><div>Related Topics</div><div><li></li><li>字符串</li></div></div>\n<div><li>👍 112</li><li>👎 0</li></div>

View File

@ -0,0 +1,75 @@
//有台奇怪的打印机有以下两个特殊要求
//
//
// 打印机每次只能打印由 同一个字符 组成的序列
// 每次可以在任意起始和结束位置打印新字符并且会覆盖掉原来已有的字符
//
//
// 给你一个字符串 s 你的任务是计算这个打印机打印它需要的最少打印次数
//
//
// 示例 1
//
//
//输入s = "aaabbb"
//输出2
//解释首先打印 "aaa" 然后打印 "bbb"
//
//
// 示例 2
//
//
//输入s = "aba"
//输出2
//解释首先打印 "aaa" 然后在第二个位置打印 "b" 覆盖掉原来的字符 'a'
//
//
//
//
// 提示
//
//
// 1 <= s.length <= 100
// s 由小写英文字母组成
//
// Related Topics 深度优先搜索 动态规划
// 👍 162 👎 0
package leetcode.editor.cn;
//664:奇怪的打印机
public class StrangePrinter {
public static void main(String[] args) {
//测试代码
Solution solution = new StrangePrinter().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int strangePrinter(String s) {
int length = s.length();
int[][] dp = new int[length][length];
// for (int i = 0; i < length; i++) {
// dp[i][i] = 1;
// }
for (int i = length - 1; i >= 0; i--) {
dp[i][i] = 1;
for (int j = i + 1; j < length; j++) {
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i][j - 1];
} else {
int min = Integer.MAX_VALUE;
for (int k = i; k <= j - 1; k++) {
min = Math.min(min, dp[i][k] + dp[k + 1][j]);
}
dp[i][j] = min;
}
}
}
return dp[0][length - 1];
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,35 @@
<p>有台奇怪的打印机有以下两个特殊要求:</p>
<ul>
<li>打印机每次只能打印由 <strong>同一个字符</strong> 组成的序列。</li>
<li>每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符。</li>
</ul>
<p>给你一个字符串 <code>s</code> ,你的任务是计算这个打印机打印它需要的最少打印次数。</p>
 
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>s = "aaabbb"
<strong>输出:</strong>2
<strong>解释:</strong>首先打印 "aaa" 然后打印 "bbb"。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>s = "aba"
<strong>输出:</strong>2
<strong>解释:</strong>首先打印 "aaa" 然后在第二个位置打印 "b" 覆盖掉原来的字符 'a'。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> 由小写英文字母组成</li>
</ul>
<div><div>Related Topics</div><div><li>深度优先搜索</li><li>动态规划</li></div></div>\n<div><li>👍 161</li><li>👎 0</li></div>

View File

@ -0,0 +1,54 @@
//两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量
//
// 计算一个数组中任意两个数之间汉明距离的总和
//
// 示例:
//
//
//输入: 4, 14, 2
//
//输出: 6
//
//解释: 在二进制表示中4表示为010014表示为11102表示为0010这样表示是为了体现后四位之间关系
//所以答案为
//HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 +
//2 + 2 = 6.
//
//
// 注意:
//
//
// 数组中元素的范围为从 0到 10^9
// 数组的长度不超过 10^4
//
// Related Topics 位运算
// 👍 154 👎 0
package leetcode.editor.cn;
//477:汉明距离总和
public class TotalHammingDistance{
public static void main(String[] args) {
//测试代码
Solution solution = new TotalHammingDistance().new Solution();
System.out.println(solution.totalHammingDistance(new int[]{4, 14, 2}));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int totalHammingDistance(int[] nums) {
int length = nums.length;
int result = 0;
int size = 30;
for (int i = 0; i < size; ++i) {
int count = 0;
for (int num : nums) {
count += (num >> i) & 1;
}
result += count * (length - count);
}
return result;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,23 @@
<p>两个整数的&nbsp;<a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E8%B7%9D%E7%A6%BB/475174?fr=aladdin">汉明距离</a> 指的是这两个数字的二进制数对应位不同的数量。</p>
<p>计算一个数组中,任意两个数之间汉明距离的总和。</p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入:</strong> 4, 14, 2
<strong>输出:</strong> 6
<strong>解释:</strong> 在二进制表示中4表示为010014表示为11102表示为0010。这样表示是为了体现后四位之间关系
所以答案为:
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.
</pre>
<p><strong>注意:</strong></p>
<ol>
<li>数组中元素的范围为从&nbsp;<code>0</code>&nbsp;<code>10^9</code></li>
<li>数组的长度不超过&nbsp;<code>10^4</code></li>
</ol>
<div><div>Related Topics</div><div><li>位运算</li></div></div>\n<div><li>👍 154</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long