mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-04 23:11:41 +08:00
update
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,它表示一个班级中所有学生在一次考试中的成绩。老师想选出一部分同学组成一个 <strong>非空</strong> 小组,且这个小组的 <strong>实力值</strong> 最大,如果这个小组里的学生下标为 <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, ... , <code>i<sub>k</sub></code> ,那么这个小组的实力值定义为 <code>nums[i<sub>0</sub>] * nums[i<sub>1</sub>] * nums[i<sub>2</sub>] * ... * nums[i<sub>k</sub>]</code> 。</p>
|
||||
|
||||
<p>请你返回老师创建的小组能得到的最大实力值为多少。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [3,-1,-5,2,5,-9]
|
||||
<strong>输出:</strong>1350
|
||||
<b>解释:</b>一种构成最大实力值小组的方案是选择下标为 [0,2,3,4,5] 的学生。实力值为 3 * (-5) * 2 * 5 * (-9) = 1350 ,这是可以得到的最大实力值。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [-4,-5,-4]
|
||||
<b>输出:</b>20
|
||||
<b>解释:</b>选择下标为 [0, 1] 的学生。得到的实力值为 20 。我们没法得到更大的实力值。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 13</code></li>
|
||||
<li><code>-9 <= nums[i] <= 9</code></li>
|
||||
</ul>
|
@@ -0,0 +1,119 @@
|
||||
<p>请你编写一个函数,它接收两个深度嵌套的对象或数组 <code>obj1</code> 和 <code>obj2</code> ,并返回一个新对象表示它们之间差异。</p>
|
||||
|
||||
<p>该函数应该比较这两个对象的属性,并识别任何变化。返回的对象应仅包含从 <code>obj1</code> 到 <code>obj2</code> 的值不同的键。对于每个变化的键,值应表示为一个数组 <code>[obj1 value, obj2 value]</code> 。不存在于一个对象中但存在于另一个对象中的键不应包含在返回的对象中。在比较两个数组时,数组的索引被视为它们的键。最终结果应是一个深度嵌套的对象,其中每个叶子的值都是一个差异数组。</p>
|
||||
|
||||
<p>你可以假设这两个对象都是 <code>JSON.parse</code> 的输出结果。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
obj1 = {}
|
||||
obj2 = {
|
||||
"a": 1,
|
||||
"b": 2
|
||||
}
|
||||
<b>输出:</b>{}
|
||||
<b>解释:</b>obj1没有进行任何修改。obj2中出现了新的键 "a" 和 "b" ,但添加或删除的键应该被忽略。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
obj1 = {
|
||||
"a": 1,
|
||||
"v": 3,
|
||||
"x": [],
|
||||
"z": {
|
||||
"a": null
|
||||
}
|
||||
}
|
||||
obj2 = {
|
||||
"a": 2,
|
||||
"v": 4,
|
||||
"x": [],
|
||||
"z": {
|
||||
"a": 2
|
||||
}
|
||||
}
|
||||
<b>输出:</b>
|
||||
{
|
||||
"a": [1, 2],
|
||||
"v": [3, 4],
|
||||
"z": {
|
||||
"a": [null, 2]
|
||||
}
|
||||
}
|
||||
<b>解释:</b>键 "a"、"v" 和 "z" 都有变化。"a" 从 1 变为 2,"v" 从 3 变为 4 ,"z" 的子对象 "a" 从 null 变为 2。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
obj1 = {
|
||||
"a": 5,
|
||||
"v": 6,
|
||||
"z": [1, 2, 4, [2, 5, 7]]
|
||||
}
|
||||
obj2 = {
|
||||
"a": 5,
|
||||
"v": 7,
|
||||
"z": [1, 2, 3, [1]]
|
||||
}
|
||||
<b>输出:</b>
|
||||
{
|
||||
"v": [6, 7],
|
||||
"z": {
|
||||
"2": [4, 3],
|
||||
"3": {
|
||||
"0": [2, 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
<b>解释:</b>在 obj1 和 obj2 中,键 "v" 和 "z" 的值不同。"a" 被忽略,因为值没有变化。在键 "z" 中,有一个嵌套的数组。数组被视为对象,其中索引被视为键。数组发生了两处变化:z[2] 和 z[3][0]。z[0] 和 z[1] 没有变化,因此没有包含在结果中。z[3][1] 和 z[3][2] 被删除,因此也没有包含在结果中。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
obj1 = {
|
||||
"a": {"b": 1},
|
||||
}
|
||||
obj2 = {
|
||||
"a": [5],
|
||||
}
|
||||
<b>输出:</b>
|
||||
{
|
||||
"a": [{"b": 1}, [5]]
|
||||
}
|
||||
<b>解释:</b>键 "a" 在两个对象中都存在。但由于两个相关值具有不同的类型,所以它们被放置在差异数组中。</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
obj1 = {
|
||||
"a": [1, 2, {}],
|
||||
"b": false
|
||||
}
|
||||
obj2 = {
|
||||
"b": false,
|
||||
"a": [1, 2, {}]
|
||||
}
|
||||
<b>输出:</b>
|
||||
{}
|
||||
<b>解释:</b>除了键的顺序不同之外,两个对象是相同的,因此返回一个空对象。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= JSON.stringify(obj1).length <= 10<sup>4</sup></code></li>
|
||||
<li><code>2 <= JSON.stringify(obj2).length <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
63
leetcode-cn/problem (Chinese)/事件发射器 [event-emitter].html
Normal file
63
leetcode-cn/problem (Chinese)/事件发射器 [event-emitter].html
Normal file
@@ -0,0 +1,63 @@
|
||||
<p>设计一个 <code>EventEmitter</code> 类。这个接口与 Node.js 或 DOM 的 Event Target 接口相似,但有一些差异。<code>EventEmitter</code> 应该允许订阅事件和触发事件。</p>
|
||||
|
||||
<p>你的 <code>EventEmitter</code> 类应该有以下两个方法:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>subscribe</strong> - 这个方法接收两个参数:一个作为字符串的事件名和一个回调函数。当事件被触发时,这个回调函数将被调用。 一个事件应该能够有多个监听器。当触发带有多个回调函数的事件时,应按照订阅的顺序依次调用每个回调函数。应返回一个结果数组。你可以假设传递给 <code>subscribe</code> 的回调函数都不是引用相同的。 <code>subscribe</code> 方法还应返回一个对象,其中包含一个 <code>unsubscribe</code> 方法,使用户可以取消订阅。当调用 <code>unsubscribe</code> 方法时,回调函数应该从订阅列表中删除,并返回 undefined。</li>
|
||||
<li><strong>emit</strong> - 这个方法接收两个参数:一个作为字符串的事件名和一个可选的参数数组,这些参数将传递给回调函数。如果没有订阅给定事件的回调函数,则返回一个空数组。否则,按照它们被订阅的顺序返回所有回调函数调用的结果数组。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>actions = ["EventEmitter", "emit", "subscribe", "subscribe", "emit"], values = [[], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent"]]
|
||||
<b>输出:</b>[[],["emitted",[]],["subscribed"],["subscribed"],["emitted",[5,6]]]
|
||||
<b>解释:</b>
|
||||
const emitter = new EventEmitter();
|
||||
emitter.emit("firstEvent"); // [], 还没有订阅任何回调函数
|
||||
emitter.subscribe("firstEvent", function cb1() { return 5; });
|
||||
emitter.subscribe("firstEvent", function cb2() { return 6; });
|
||||
emitter.emit("firstEvent"); // [5, 6], 返回 cb1 和 cb2 的输出
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>actions = ["EventEmitter", "subscribe", "emit", "emit"], values = [[], ["firstEvent", "function cb1(...args) { return args.join(','); }"], ["firstEvent", [1,2,3]], ["firstEvent", [3,4,6]]]
|
||||
<b>输出:</b>[[],["subscribed"],["emitted",["1,2,3"]],["emitted",["3,4,6"]]]
|
||||
<strong>解释:</strong>注意 emit 方法应该能够接受一个可选的参数数组。
|
||||
|
||||
const emitter = new EventEmitter();
|
||||
emitter.subscribe("firstEvent, function cb1(...args) { return args.join(','); });
|
||||
emitter.emit("firstEvent", [1, 2, 3]); // ["1,2,3"]
|
||||
emitter.emit("firstEvent", [3, 4, 6]); // ["3,4,6"]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>actions = ["EventEmitter", "subscribe", "emit", "unsubscribe", "emit"], values = [[], ["firstEvent", "(...args) => args.join(',')"], ["firstEvent", [1,2,3]], [0], ["firstEvent", [4,5,6]]]
|
||||
<b>输出:</b>[[],["subscribed"],["emitted",["1,2,3"]],["unsubscribed",0],["emitted",[]]]
|
||||
<b>解释:</b>
|
||||
const emitter = new EventEmitter();
|
||||
const sub = emitter.subscribe("firstEvent", (...args) => args.join(','));
|
||||
emitter.emit("firstEvent", [1, 2, 3]); // ["1,2,3"]
|
||||
sub.unsubscribe(); // undefined
|
||||
emitter.emit("firstEvent", [4, 5, 6]); // [], 没有订阅者
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= actions.length <= 10</code></li>
|
||||
<li><code>values.length === actions.length</code></li>
|
||||
<li>所有测试用例都是有效的。例如,你不需要处理取消一个不存在的订阅的情况。</li>
|
||||
<li>只有 4 种不同的操作:<code>EventEmitter</code>、<code>emit</code>、<code>subscribe</code> 和 <code>unsubscribe</code> 。 <code>EventEmitter</code> 操作没有参数。</li>
|
||||
<li><code>emit</code> 操作接收 1 或 2 个参数。第一个参数是要触发的事件名,第二个参数传递给回调函数。</li>
|
||||
<li><code>subscribe</code> 操作接收 2 个参数,第一个是事件名,第二个是回调函数。</li>
|
||||
<li><code>unsubscribe</code> 操作接收一个参数,即之前进行订阅的顺序(从 0 开始)。</li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始、长度为 <code>n</code> 的二进制字符串 <code>s</code> ,你可以对其执行两种操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>选中一个下标 <code>i</code> 并且反转从下标 <code>0</code> 到下标 <code>i</code>(包括下标 <code>0</code> 和下标 <code>i</code> )的所有字符,成本为 <code>i + 1</code> 。</li>
|
||||
<li>选中一个下标 <code>i</code> 并且反转从下标 <code>i</code> 到下标 <code>n - 1</code>(包括下标 <code>i</code> 和下标 <code>n - 1</code> )的所有字符,成本为 <code>n - i</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回使字符串内所有字符 <strong>相等</strong> 需要的 <strong>最小成本</strong> 。</p>
|
||||
|
||||
<p><strong>反转</strong> 字符意味着:如果原来的值是 '0' ,则反转后值变为 '1' ,反之亦然。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "0011"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>执行第二种操作,选中下标 <code>i = 2</code> ,可以得到 <code>s = "0000" ,成本为 2</code> 。可以证明 2 是使所有字符相等的最小成本。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "010101"
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>执行第一种操作,选中下标 i = 2 ,可以得到 s = "101101" ,成本为 3 。
|
||||
执行第一种操作,选中下标 i = 1 ,可以得到 s = "011101" ,成本为 2 。
|
||||
执行第一种操作,选中下标 i = 0 ,可以得到 s = "111101" ,成本为 1 。
|
||||
执行第二种操作,选中下标 i = 4 ,可以得到 s = "111110" ,成本为 2 。
|
||||
执行第一种操作,选中下标 i = 5 ,可以得到 s = "111111" ,成本为 1 。
|
||||
使所有字符相等的总成本等于 9 。可以证明 9 是使所有字符相等的最小成本。 </pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length == n <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> 为 <code>'0'</code> 或 <code>'1'</code></li>
|
||||
</ul>
|
@@ -0,0 +1,54 @@
|
||||
<p>增强所有函数,使其具有 <code>callPolyfill</code> 方法。该方法接受一个对象 <code>obj</code> 作为第一个参数,以及任意数量的附加参数。<code>obj</code> 成为函数的 <code>this</code> 上下文。附加参数将传递给该函数(即 <code>callPolyfill</code> 方法所属的函数)。</p>
|
||||
|
||||
<p>例如,如果有以下函数:</p>
|
||||
|
||||
<pre>
|
||||
function tax(price, taxRate) {
|
||||
const totalCost = price * (1 + taxRate);
|
||||
console.log(`The cost of ${this.item} is ${totalCost}`);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>调用 <code>tax(10, 0.1)</code> 将输出 <code>"The cost of undefined is 11"</code> 。这是因为 <code>this</code> 上下文未定义。</p>
|
||||
|
||||
<p>然而,调用 <code>tax.callPolyfill({item: "salad"}, 10, 0.1)</code> 将输出 <code>"The cost of salad is 11"</code> 。<code>this</code> 上下文被正确设置,函数输出了适当的结果。</p>
|
||||
|
||||
<p>请在不使用内置的 <code>Function.call</code> 方法的情况下解决这个问题。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
fn = function add(b) {
|
||||
return this.a + b;
|
||||
}
|
||||
args = [{"a": 5}, 7]
|
||||
<b>输出:</b>12
|
||||
<strong>解释:</strong>
|
||||
fn.callPolyfill({"a": 5}, 7); // 12
|
||||
<code>callPolyfill </code>将 "this" 上下文设置为 <code>{"a": 5} </code>,并将 7 作为参数传递。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>
|
||||
fn = function tax(price, taxRate) {
|
||||
return `The cost of the ${this.item} is ${price * taxRate}`;
|
||||
}
|
||||
args = [{"item": "burger"}, 10, 1,1]
|
||||
<b>输出:</b>"The cost of the burger is 11"
|
||||
<b>解释:</b><code>callPolyfill </code>将 "this" 上下文设置为 <code>{"item": "burger"} </code>,并将 10 和 1.1 作为附加参数传递。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul style="list-style-type:square;">
|
||||
<li><code><font face="monospace">typeof args[0] == 'object' and args[0] != null</font></code></li>
|
||||
<li><code>1 <= args.length <= 100</code></li>
|
||||
<li><code>2 <= JSON.stringify(args[0]).length <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>给你一个 <code>n</code> 个节点的 <strong>无向带权连通</strong> 图,节点编号为 <code>0</code> 到 <code>n - 1</code> ,再给你一个整数数组 <code>edges</code> ,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> 表示节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 之间有一条边权为 <code>w<sub>i</sub></code> 的边。</p>
|
||||
|
||||
<p>部分边的边权为 <code>-1</code>(<code>w<sub>i</sub> = -1</code>),其他边的边权都为 <strong>正</strong> 数(<code>w<sub>i</sub> > 0</code>)。</p>
|
||||
|
||||
<p>你需要将所有边权为 <code>-1</code> 的边都修改为范围 <code>[1, 2 * 10<sup>9</sup>]</code> 中的 <strong>正整数</strong> ,使得从节点 <code>source</code> 到节点 <code>destination</code> 的 <strong>最短距离</strong> 为整数 <code>target</code> 。如果有 <strong>多种</strong> 修改方案可以使 <code>source</code> 和 <code>destination</code> 之间的最短距离等于 <code>target</code> ,你可以返回任意一种方案。</p>
|
||||
|
||||
<p>如果存在使 <code>source</code> 到 <code>destination</code> 最短距离为 <code>target</code> 的方案,请你按任意顺序返回包含所有边的数组(包括未修改边权的边)。如果不存在这样的方案,请你返回一个 <strong>空数组</strong> 。</p>
|
||||
|
||||
<p><strong>注意:</strong>你不能修改一开始边权为正数的边。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2023/04/18/graph.png" style="width: 300px; height: 300px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 5, edges = [[4,1,-1],[2,0,-1],[0,3,-1],[4,3,-1]], source = 0, destination = 1, target = 5
|
||||
<b>输出:</b>[[4,1,1],[2,0,1],[0,3,3],[4,3,1]]
|
||||
<b>解释:</b>上图展示了一个满足题意的修改方案,从 0 到 1 的最短距离为 5 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2023/04/18/graph-2.png" style="width: 300px; height: 300px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 3, edges = [[0,1,-1],[0,2,5]], source = 0, destination = 2, target = 6
|
||||
<b>输出:</b>[]
|
||||
<b>解释:</b>上图是一开始的图。没有办法通过修改边权为 -1 的边,使得 0 到 2 的最短距离等于 6 ,所以返回一个空数组。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2023/04/19/graph-3.png" style="width: 300px; height: 300px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 4, edges = [[1,0,4],[1,2,3],[2,3,5],[0,3,-1]], source = 0, destination = 2, target = 6
|
||||
<b>输出:</b>[[1,0,4],[1,2,3],[2,3,5],[0,3,1]]
|
||||
<b>解释:</b>上图展示了一个满足题意的修改方案,从 0 到 2 的最短距离为 6 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= edges.length <= n * (n - 1) / 2</code></li>
|
||||
<li><code>edges[i].length == 3</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i </sub>< n</code></li>
|
||||
<li><code>w<sub>i</sub> = -1</code> 或者 <code>1 <= w<sub>i </sub><= 10<sup><span style="">7</span></sup></code></li>
|
||||
<li><code>a<sub>i </sub>!= b<sub>i</sub></code></li>
|
||||
<li><code>0 <= source, destination < n</code></li>
|
||||
<li><code>source != destination</code></li>
|
||||
<li><code>1 <= target <= 10<sup>9</sup></code></li>
|
||||
<li>输入的图是连通图,且没有自环和重边。</li>
|
||||
</ul>
|
@@ -0,0 +1,38 @@
|
||||
<p>给你一个仅由 <strong>大写</strong> 英文字符组成的字符串 <code>s</code> 。</p>
|
||||
|
||||
<p>你可以对此字符串执行一些操作,在每一步操作中,你可以从 <code>s</code> 中删除 <strong>任一个</strong> <code>"AB"</code> 或 <code>"CD"</code> 子字符串。</p>
|
||||
|
||||
<p>通过执行操作,删除所有 <code>"AB"</code> 和 <code>"CD"</code> 子串,返回可获得的最终字符串的 <strong>最小</strong> 可能长度。</p>
|
||||
|
||||
<p><strong>注意</strong>,删除子串后,重新连接出的字符串可能会产生新的 <code>"AB"</code> 或 <code>"CD"</code> 子串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "ABFCACDB"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>你可以执行下述操作:
|
||||
- 从 "<em><strong>AB</strong></em>FCACDB" 中删除子串 "AB",得到 s = "FCACDB" 。
|
||||
- 从 "FCA<em><strong>CD</strong></em>B" 中删除子串 "CD",得到 s = "FCAB" 。
|
||||
- 从 "FC<strong><em>AB</em></strong>" 中删除子串 "AB",得到 s = "FC" 。
|
||||
最终字符串的长度为 2 。
|
||||
可以证明 2 是可获得的最小长度。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "ACBBD"
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>无法执行操作,字符串长度不变。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> 仅由大写英文字母组成</li>
|
||||
</ul>
|
50
leetcode-cn/problem (Chinese)/包装数组 [array-wrapper].html
Normal file
50
leetcode-cn/problem (Chinese)/包装数组 [array-wrapper].html
Normal file
@@ -0,0 +1,50 @@
|
||||
<p>创建一个名为 <code>ArrayWrapper</code> 的类,它在其构造函数中接受一个整数数组作为参数。该类应具有以下两个特性:</p>
|
||||
|
||||
<ul>
|
||||
<li>当使用 <code>+</code> 运算符将两个该类的实例相加时,结果值为两个数组中所有元素的总和。</li>
|
||||
<li>当在实例上调用 <code>String()</code> 函数时,它将返回一个由逗号分隔的括在方括号中的字符串。例如,<code>[1,2,3]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [[1,2],[3,4]], operation = "Add"
|
||||
<b>输出:</b>10
|
||||
<b>解释:</b>
|
||||
const obj1 = new ArrayWrapper([1,2]);
|
||||
const obj2 = new ArrayWrapper([3,4]);
|
||||
obj1 + obj2; // 10
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [[23,98,42,70]], operation = "String"
|
||||
<b>输出:</b>"[23,98,42,70]"
|
||||
<strong>解释:</strong>
|
||||
const obj = new ArrayWrapper([23,98,42,70]);
|
||||
String(obj); // "[23,98,42,70]"
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [[],[]], operation = "Add"
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>
|
||||
const obj1 = new ArrayWrapper([]);
|
||||
const obj2 = new ArrayWrapper([]);
|
||||
obj1 + obj2; // 0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><b>提示:</b></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
<li><code>注意:nums 是传递给构造函数的数组。</code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>给你一个由 <strong>小写英文字母</strong> 组成的字符串 <code>s</code> ,你可以对其执行一些操作。在一步操作中,你可以用其他小写英文字母 <strong>替换</strong> <code>s</code> 中的一个字符。</p>
|
||||
|
||||
<p>请你执行 <strong>尽可能少的操作</strong> ,使 <code>s</code> 变成一个 <strong>回文串</strong> 。如果执行 <strong>最少</strong> 操作次数的方案不止一种,则只需选取 <strong>字典序最小</strong> 的方案。</p>
|
||||
|
||||
<p>对于两个长度相同的字符串 <code>a</code> 和 <code>b</code> ,在 <code>a</code> 和 <code>b</code> 出现不同的第一个位置,如果该位置上 <code>a</code> 中对应字母比 <code>b</code> 中对应字母在字母表中出现顺序更早,则认为 <code>a</code> 的字典序比 <code>b</code> 的字典序要小。</p>
|
||||
|
||||
<p>返回最终的回文字符串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "egcfe"
|
||||
<strong>输出:</strong>"efcfe"
|
||||
<strong>解释:</strong>将 "egcfe" 变成回文字符串的最小操作次数为 1 ,修改 1 次得到的字典序最小回文字符串是 "efcfe",只需将 'g' 改为 'f' 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abcd"
|
||||
<strong>输出:</strong>"abba"
|
||||
<strong>解释:</strong>将 "abcd" 变成回文字符串的最小操作次数为 2 ,修改 2 次得到的字典序最小回文字符串是 "abba" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "seven"
|
||||
<strong>输出:</strong>"neven"
|
||||
<strong>解释:</strong>将 "seven" 变成回文字符串的最小操作次数为 1 ,修改 1 次得到的字典序最小回文字符串是 "neven" 。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> 仅由小写英文字母组成</li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>s</code> 和一个单词字典 <code>dictionary</code> 。你需要将 <code>s</code> 分割成若干个 <strong>互不重叠</strong> 的子字符串,每个子字符串都在 <code>dictionary</code> 中出现过。<code>s</code> 中可能会有一些 <strong>额外的字符</strong> 不在任何子字符串中。</p>
|
||||
|
||||
<p>请你采取最优策略分割 <code>s</code> ,使剩下的字符 <strong>最少</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "leetscode", dictionary = ["leet","code","leetcode"]
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>将 s 分成两个子字符串:下标从 0 到 3 的 "leet" 和下标从 5 到 8 的 "code" 。只有 1 个字符没有使用(下标为 4),所以我们返回 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "sayhelloworld", dictionary = ["hello","world"]
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>将 s 分成两个子字符串:下标从 3 到 7 的 "hello" 和下标从 8 到 12 的 "world" 。下标为 0 ,1 和 2 的字符没有使用,所以我们返回 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 50</code></li>
|
||||
<li><code>1 <= dictionary.length <= 50</code></li>
|
||||
<li><code>1 <= dictionary[i].length <= 50</code></li>
|
||||
<li><code>dictionary[i]</code> 和 <code>s</code> 只包含小写英文字母。</li>
|
||||
<li><code>dictionary</code> 中的单词互不相同。</li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>给你一个下标从 <code>0</code> 开始、大小为 <code>m x n</code> 的二维矩阵 <code>grid</code> ,请你求解大小同样为 <code>m x n</code> 的答案矩阵 <code>answer</code> 。</p>
|
||||
|
||||
<p>矩阵 <code>answer</code> 中每个单元格 <code>(r, c)</code> 的值可以按下述方式进行计算:</p>
|
||||
|
||||
<ul>
|
||||
<li>令 <code>topLeft[r][c]</code> 为矩阵 <code>grid</code> 中单元格 <code>(r, c)</code> 左上角对角线上 <strong>不同值</strong> 的数量。</li>
|
||||
<li>令 <code>bottomRight[r][c]</code> 为矩阵 <code>grid</code> 中单元格 <code>(r, c)</code> 右下角对角线上 <strong>不同值</strong> 的数量。</li>
|
||||
</ul>
|
||||
|
||||
<p>然后 <code>answer[r][c] = |topLeft[r][c] - bottomRight[r][c]|</code> 。</p>
|
||||
|
||||
<p>返回矩阵 <code>answer</code> 。</p>
|
||||
|
||||
<p><strong>矩阵对角线</strong> 是从最顶行或最左列的某个单元格开始,向右下方向走到矩阵末尾的对角线。</p>
|
||||
|
||||
<p>如果单元格 <code>(r1, c1)</code> 和单元格 <code>(r, c) </code>属于同一条对角线且 <code>r1 < r</code> ,则单元格 <code>(r1, c1)</code> 属于单元格 <code>(r, c)</code> 的左上对角线。类似地,可以定义右下对角线。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/04/19/ex2.png" style="width: 786px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[1,2,3],[3,1,5],[3,2,1]]
|
||||
<strong>输出:</strong>[[1,1,0],[1,0,1],[0,1,1]]
|
||||
<strong>解释:</strong>第 1 个图表示最初的矩阵 grid 。
|
||||
第 2 个图表示对单元格 (0,0) 计算,其中蓝色单元格是位于右下对角线的单元格。
|
||||
第 3 个图表示对单元格 (1,2) 计算,其中红色单元格是位于左上对角线的单元格。
|
||||
第 4 个图表示对单元格 (1,1) 计算,其中蓝色单元格是位于右下对角线的单元格,红色单元格是位于左上对角线的单元格。
|
||||
- 单元格 (0,0) 的右下对角线包含 [1,1] ,而左上对角线包含 [] 。对应答案是 |1 - 0| = 1 。
|
||||
- 单元格 (1,2) 的右下对角线包含 [] ,而左上对角线包含 [2] 。对应答案是 |0 - 1| = 1 。
|
||||
- 单元格 (1,1) 的右下对角线包含 [1] ,而左上对角线包含 [1] 。对应答案是 |1 - 1| = 0 。
|
||||
其他单元格的对应答案也可以按照这样的流程进行计算。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>grid = [[1]]
|
||||
<strong>输出:</strong>[[0]]
|
||||
<strong>解释:</strong>- 单元格 (0,0) 的右下对角线包含 [] ,左上对角线包含 [] 。对应答案是 |0 - 0| = 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n, grid[i][j] <= 50</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,你可以在一些下标之间遍历。对于两个下标 <code>i</code> 和 <code>j</code>(<code>i != j</code>),当且仅当 <code>gcd(nums[i], nums[j]) > 1</code> 时,我们可以在两个下标之间通行,其中 <code>gcd</code> 是两个数的 <strong>最大公约数</strong> 。</p>
|
||||
|
||||
<p>你需要判断 <code>nums</code> 数组中 <strong>任意 </strong>两个满足 <code>i < j</code> 的下标 <code>i</code> 和 <code>j</code> ,是否存在若干次通行可以从 <code>i</code> 遍历到 <code>j</code> 。</p>
|
||||
|
||||
<p>如果任意满足条件的下标对都可以遍历,那么返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [2,3,6]
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>这个例子中,总共有 3 个下标对:(0, 1) ,(0, 2) 和 (1, 2) 。
|
||||
从下标 0 到下标 1 ,我们可以遍历 0 -> 2 -> 1 ,我们可以从下标 0 到 2 是因为 gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1 ,从下标 2 到 1 是因为 gcd(nums[2], nums[1]) = gcd(6, 3) = 3 > 1 。
|
||||
从下标 0 到下标 2 ,我们可以直接遍历,因为 gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1 。同理,我们也可以从下标 1 到 2 因为 gcd(nums[1], nums[2]) = gcd(3, 6) = 3 > 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,9,5]
|
||||
<b>输出:</b>false
|
||||
<b>解释:</b>我们没法从下标 0 到 2 ,所以返回 false 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [4,3,12,8]
|
||||
<b>输出:</b>true
|
||||
<b>解释:</b>总共有 6 个下标对:(0, 1) ,(0, 2) ,(0, 3) ,(1, 2) ,(1, 3) 和 (2, 3) 。所有下标对之间都存在可行的遍历,所以返回 true 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>给你一个正整数 <code>n</code> ,请你返回 <code>n</code> 的 <strong>惩罚数</strong> 。</p>
|
||||
|
||||
<p><code>n</code> 的 <strong>惩罚数</strong> 定义为所有满足以下条件 <code>i</code> 的数的平方和:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= i <= n</code></li>
|
||||
<li><code>i * i</code> 的十进制表示的字符串可以分割成若干连续子字符串,且这些子字符串对应的整数值之和等于 <code>i</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 10
|
||||
<b>输出:</b>182
|
||||
<b>解释:</b>总共有 3 个整数 i 满足要求:
|
||||
- 1 ,因为 1 * 1 = 1
|
||||
- 9 ,因为 9 * 9 = 81 ,且 81 可以分割成 8 + 1 。
|
||||
- 10 ,因为 10 * 10 = 100 ,且 100 可以分割成 10 + 0 。
|
||||
因此,10 的惩罚数为 1 + 81 + 100 = 182
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 37
|
||||
<b>输出:</b>1478
|
||||
<b>解释:</b>总共有 4 个整数 i 满足要求:
|
||||
- 1 ,因为 1 * 1 = 1
|
||||
- 9 ,因为 9 * 9 = 81 ,且 81 可以分割成 8 + 1 。
|
||||
- 10 ,因为 10 * 10 = 100 ,且 100 可以分割成 10 + 0 。
|
||||
- 36 ,因为 36 * 36 = 1296 ,且 1296 可以分割成 1 + 29 + 6 。
|
||||
因此,37 的惩罚数为 1 + 81 + 100 + 1296 = 1478
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>请你编写一个名为 <code>expect</code> 的函数,用于帮助开发人员测试他们的代码。它应该接受任何值 <code>val</code> 并返回一个包含以下两个函数的对象。</p>
|
||||
|
||||
<ul>
|
||||
<li><code>toBe(val)</code> 接受另一个值并在两个值相等( <code>===</code> )时返回 <code>true</code> 。如果它们不相等,则应抛出错误 <code>"Not Equal"</code> 。</li>
|
||||
<li><code>notToBe(val)</code> 接受另一个值并在两个值不相等( <code>!==</code> )时返回 <code>true</code> 。如果它们相等,则应抛出错误 <code>"Equal"</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>func = () => expect(5).toBe(5)
|
||||
<b>输出:</b>{"value": true}
|
||||
<b>解释:</b>5 === 5 因此该表达式返回 true。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>func = () => expect(5).toBe(null)
|
||||
<b>输出:</b>{"error": "Not Equal"}
|
||||
<b>解释:</b>5 !== null 因此抛出错误 "Not Equal".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>func = () => expect(5).notToBe(null)
|
||||
<b>输出:</b>{"value": true}
|
||||
<b>解释:</b>5 !== null 因此该表达式返回 true.
|
||||
</pre>
|
@@ -0,0 +1,50 @@
|
||||
<p>给你一个下标从 <strong>1</strong> 开始、大小为 <code>m x n</code> 的整数矩阵 <code>mat</code>,你可以选择任一单元格作为 <strong>起始单元格</strong> 。</p>
|
||||
|
||||
<p>从起始单元格出发,你可以移动到 <strong>同一行或同一列</strong> 中的任何其他单元格,但前提是目标单元格的值<strong> 严格大于 </strong>当前单元格的值。</p>
|
||||
|
||||
<p>你可以多次重复这一过程,从一个单元格移动到另一个单元格,直到无法再进行任何移动。</p>
|
||||
|
||||
<p>请你找出从某个单元开始访问矩阵所能访问的 <strong>单元格的最大数量</strong> 。</p>
|
||||
|
||||
<p>返回一个表示可访问单元格最大数量的整数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2023/04/23/diag1drawio.png" style="width: 200px; height: 176px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>mat = [[3,1],[3,4]]
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>上图展示了从第 1 行、第 2 列的单元格开始,可以访问 2 个单元格。可以证明,无论从哪个单元格开始,最多只能访问 2 个单元格,因此答案是 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2023/04/23/diag3drawio.png" style="width: 200px; height: 176px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>mat = [[1,1],[1,1]]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>由于目标单元格必须严格大于当前单元格,在本示例中只能访问 1 个单元格。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2023/04/23/diag4drawio.png" style="width: 350px; height: 250px;"></strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>mat = [[3,1,6],[-9,5,7]]
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>上图展示了从第 2 行、第 1 列的单元格开始,可以访问 4 个单元格。可以证明,无论从哪个单元格开始,最多只能访问 4 个单元格,因此答案是 4 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length </code></li>
|
||||
<li><code>n == mat[i].length </code></li>
|
||||
<li><code>1 <= m, n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>5</sup> <= mat[i][j] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,27 @@
|
||||
<p>给你一个用字符串表示的正整数 <code>num</code> ,请你以字符串形式返回不含尾随零的整数<em> </em><code>num</code><em> </em>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>num = "51230100"
|
||||
<strong>输出:</strong>"512301"
|
||||
<strong>解释:</strong>整数 "51230100" 有 2 个尾随零,移除并返回整数 "512301" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>num = "123"
|
||||
<strong>输出:</strong>"123"
|
||||
<strong>解释:</strong>整数 "123" 不含尾随零,返回整数 "123" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num.length <= 1000</code></li>
|
||||
<li><code>num</code> 仅由数字 <code>0</code> 到 <code>9</code> 组成</li>
|
||||
<li><code>num</code> 不含前导零</li>
|
||||
</ul>
|
37
leetcode-cn/problem (Chinese)/精简对象 [compact-object].html
Normal file
37
leetcode-cn/problem (Chinese)/精简对象 [compact-object].html
Normal file
@@ -0,0 +1,37 @@
|
||||
<p>现给定一个对象或数组 <code>obj</code>,返回一个 <strong>精简对象</strong> 。<strong>精简对象</strong> 与原始对象相同,只是将包含 <strong>假</strong> 值的键移除。该操作适用于对象及其嵌套对象。数组被视为索引作为键的对象。当 <code>Boolean(value)</code> 返回 <code>false</code> 时,值被视为 <strong>假</strong> 值。</p>
|
||||
|
||||
<p>你可以假设 <code>obj</code> 是 <code>JSON.parse</code> 的输出结果。换句话说,它是有效的 JSON。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>obj = [null, 0, false, 1]
|
||||
<b>输出:</b>[1]
|
||||
<b>解释:</b>数组中的所有假值已被移除。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>obj = {"a": null, "b": [false, 1]}
|
||||
<b>输出:</b>{"b": [1]}
|
||||
<b>解释:</b>obj["a"] 和 obj["b"][0] 包含假值,因此被移除。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>obj = [null, 0, 5, [0], [false, 16]]
|
||||
<b>输出:</b>[5, [], [16]]
|
||||
<b>解释:</b>obj[0], obj[1], obj[3][0], 和 obj[4][0] 包含假值,因此被移除。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>obj 是一个有效的 JSON 对象</code></li>
|
||||
<li><code>2 <= JSON.stringify(obj).length <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,31 @@
|
||||
<p>给你一个整数数组 <code>prices</code> ,它表示一个商店里若干巧克力的价格。同时给你一个整数 <code>money</code> ,表示你一开始拥有的钱数。</p>
|
||||
|
||||
<p>你必须购买 <strong>恰好 </strong>两块巧克力,而且剩余的钱数必须是 <strong>非负数</strong> 。同时你想最小化购买两块巧克力的总花费。</p>
|
||||
|
||||
<p>请你返回在购买两块巧克力后,最多能剩下多少钱。如果购买任意两块巧克力都超过了你拥有的钱,请你返回 <code>money</code> 。注意剩余钱数必须是非负数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>prices = [1,2,2], money = 3
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>分别购买价格为 1 和 2 的巧克力。你剩下 3 - 3 = 0 块钱。所以我们返回 0 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>prices = [3,2,3], money = 3
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>购买任意 2 块巧克力都会超过你拥有的钱数,所以我们返回 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= prices.length <= 50</code></li>
|
||||
<li><code>1 <= prices[i] <= 100</code></li>
|
||||
<li><code>1 <= money <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
请你编写一个函数 <code>argumentsLength</code>,返回传递给该函数的参数数量。
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>argsArr = [5]
|
||||
<b>输出:</b>1
|
||||
<strong>解释:</strong>
|
||||
argumentsLength(5); // 1
|
||||
|
||||
只传递了一个值给函数,因此它应返回 1。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>argsArr = [{}, null, "3"]
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>
|
||||
argumentsLength({}, null, "3"); // 3
|
||||
|
||||
传递了三个值给函数,因此它应返回 3。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>argsArr 是一个有效的 JSON 数组</code></li>
|
||||
<li><code>0 <= argsArr.length <= 100</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user