mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
update
This commit is contained in:
parent
0b115f313a
commit
96cb474414
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2023.05.15**
|
||||
> 最后更新日期: **2023.06.02**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,69 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2853",
|
||||
"questionFrontendId": "2701",
|
||||
"categoryTitle": "Database",
|
||||
"boundTopicId": 2280918,
|
||||
"title": "Consecutive Transactions with Increasing Amounts",
|
||||
"titleSlug": "consecutive-transactions-with-increasing-amounts",
|
||||
"content": null,
|
||||
"translatedTitle": "连续递增交易",
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Hard",
|
||||
"likes": 2,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": null,
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": "数据库",
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"44\", \"totalSubmission\": \"98\", \"totalAcceptedRaw\": 44, \"totalSubmissionRaw\": 98, \"acRate\": \"44.9%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\":{\"Transactions\":[\"transaction_id\",\"customer_id\",\"transaction_date\",\"amount\"]},\"rows\":{\"Transactions\":[[1,101,\"2023-05-01\",100],[2,101,\"2023-05-02\",150],[3,101,\"2023-05-03\",200],[4,102,\"2023-05-01\",50],[5,102,\"2023-05-03\",100],[6,102,\"2023-05-04\",200],[7,105,\"2023-05-01\",100],[8,105,\"2023-05-02\",150],[9,105,\"2023-05-03\",200],[10,105,\"2023-05-04\",300],[11,105,\"2023-05-12\",250],[12,105,\"2023-05-13\",260],[13,105,\"2023-05-14\",270]]}}",
|
||||
"metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Transactions (transaction_id int, customer_id int, transaction_date date, amount int)\"\n ],\n \"mssql\": [\n \"Create table Transactions (transaction_id int, customer_id int, transaction_date date, amount int)\"\n ],\n \"oraclesql\": [\n \"Create table Transactions (transaction_id int, customer_id int, transaction_date date, amount int)\",\n \"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"\n ],\n \"database\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Transactions (transaction_id int, customer_id int, transaction_date date, amount int)",
|
||||
"Truncate table Transactions",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('1', '101', '2023-05-01', '100')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('2', '101', '2023-05-02', '150')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('3', '101', '2023-05-03', '200')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('4', '102', '2023-05-01', '50')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('5', '102', '2023-05-03', '100')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('6', '102', '2023-05-04', '200')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('7', '105', '2023-05-01', '100')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('8', '105', '2023-05-02', '150')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('9', '105', '2023-05-03', '200')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('10', '105', '2023-05-04', '300')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('11', '105', '2023-05-12', '250')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('12', '105', '2023-05-13', '260')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('13', '105', '2023-05-14', '270')"
|
||||
],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"mysql\":[\"MySQL\",\"<p>\\u7248\\u672c\\uff1a<code>MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"<p>mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\"<p>Oracle Sql 11.2.<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "{\"headers\":{\"Transactions\":[\"transaction_id\",\"customer_id\",\"transaction_date\",\"amount\"]},\"rows\":{\"Transactions\":[[1,101,\"2023-05-01\",100],[2,101,\"2023-05-02\",150],[3,101,\"2023-05-03\",200],[4,102,\"2023-05-01\",50],[5,102,\"2023-05-03\",100],[6,102,\"2023-05-04\",200],[7,105,\"2023-05-01\",100],[8,105,\"2023-05-02\",150],[9,105,\"2023-05-03\",200],[10,105,\"2023-05-04\",300],[11,105,\"2023-05-12\",250],[12,105,\"2023-05-13\",260],[13,105,\"2023-05-14\",270]]}}",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
51
leetcode-cn/originData/[no content]immutability-helper.json
Normal file
51
leetcode-cn/originData/[no content]immutability-helper.json
Normal file
@ -0,0 +1,51 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2773",
|
||||
"questionFrontendId": "2691",
|
||||
"categoryTitle": "JavaScript",
|
||||
"boundTopicId": 2275512,
|
||||
"title": "Immutability Helper",
|
||||
"titleSlug": "immutability-helper",
|
||||
"content": null,
|
||||
"translatedTitle": "不可变辅助工具",
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Hard",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": null,
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"26\", \"totalSubmission\": \"86\", \"totalAcceptedRaw\": 26, \"totalSubmissionRaw\": 86, \"acRate\": \"30.2%\"}",
|
||||
"hints": [
|
||||
"Javascript has the concept of Proxy. That concept is critical to this problem.",
|
||||
"You you need to keep track of which values in the JSON were overwritten with new values.",
|
||||
"Somehow, keep a tree structure that exists parallel to the original object. This will keep track of all the edits.",
|
||||
"When the mutator function is done. Return a clone with those mutations applied. It will be inefficient to clone the entire object so only clone the minimum number of nodes."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"val\":10}\n[proxy => { proxy.val += 1; }, proxy => { proxy.val -= 1; }]",
|
||||
"metaData": "{\n \"name\": \"produce\",\n \"params\": [\n {\n \"name\": \"obj\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"mutator\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "{\"val\":10}\n[proxy => { proxy.val += 1; }, proxy => { proxy.val -= 1; }]\n{\"arr\":[1,2,3]}\n[proxy => { proxy.arr[0] = 5; proxy.newVal = proxy.arr[0] + proxy.arr[1]; }]\n{\"obj\":{\"val\":{\"x\":10,\"y\":20}}}\n[proxy => { let data = proxy.obj.val; let temp = data.x; data.x = data.y; data.y = temp; }]",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2772",
|
||||
"questionFrontendId": "2690",
|
||||
"categoryTitle": "JavaScript",
|
||||
"boundTopicId": 2275510,
|
||||
"title": "Infinite Method Object",
|
||||
"titleSlug": "infinite-method-object",
|
||||
"content": null,
|
||||
"translatedTitle": "无穷方法对象",
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Easy",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": null,
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"51\", \"totalSubmission\": \"52\", \"totalAcceptedRaw\": 51, \"totalSubmissionRaw\": 52, \"acRate\": \"98.1%\"}",
|
||||
"hints": [
|
||||
"Javascript has the concept of Proxy. That concept is critical to this problem.",
|
||||
"Override all \"get\" for the object. Return a function instead.",
|
||||
"That function should return the \"prop\", i.e. the method name."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "\"abc123\"",
|
||||
"metaData": "{\n \"name\": \"createInfiniteObject\",\n \"params\": [\n {\n \"type\": \"string\",\n \"name\": \"method\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "\"abc123\"\n\".-qw73n|^2It\"",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2776",
|
||||
"questionFrontendId": "2692",
|
||||
"categoryTitle": "JavaScript",
|
||||
"boundTopicId": 2275509,
|
||||
"title": "Make Object Immutable",
|
||||
"titleSlug": "make-object-immutable",
|
||||
"content": null,
|
||||
"translatedTitle": "使对象不可变",
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": null,
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"35\", \"totalSubmission\": \"75\", \"totalAcceptedRaw\": 35, \"totalSubmissionRaw\": 75, \"acRate\": \"46.7%\"}",
|
||||
"hints": [
|
||||
"Javascript has the concept of Proxy. That concept is critical to this problem.",
|
||||
"Recursively use proxy so that the user of the object is only able to access a proxy object.",
|
||||
"Override how set works. It should throw the correct error instead of actually setting a value."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"x\":5}\n(obj) => { obj.x = 5; return obj.x; }",
|
||||
"metaData": "{\n \"name\": \"makeImmutable\",\n \"params\": [\n {\n \"name\": \"obj\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "{\"x\":5}\n(obj) => { obj.x = 5; return obj.x; }\n[1,2,3]\n(arr) => { arr[1] = {}; return arr[2]; }\n{\"arr\":[1,2,3]}\n(obj) => { obj.arr.push(4); return 42; }\n{\"x\":2,\"y\":2}\n(obj) => { return Object.keys(obj); }",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
59
leetcode-cn/originData/array-wrapper.json
Normal file
59
leetcode-cn/originData/array-wrapper.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2805",
|
||||
"questionFrontendId": "2695",
|
||||
"categoryTitle": "JavaScript",
|
||||
"boundTopicId": 2275511,
|
||||
"title": "Array Wrapper",
|
||||
"titleSlug": "array-wrapper",
|
||||
"content": "<p>Create a class <code>ArrayWrapper</code> that accepts an array of integers in it's constructor. This class should have two features:</p>\n\n<ul>\n\t<li>When two instances of this class are added together with the <code>+</code> operator, the resulting value is the sum of all the elements in both arrays.</li>\n\t<li>When the <code>String()</code> function is called on the instance, it will return a comma separated string surrounded by brackets. For example, <code>[1,2,3]</code>.</li>\n</ul>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [[1,2],[3,4]], operation = "Add"\n<strong>Output:</strong> 10\n<strong>Explanation:</strong>\nconst obj1 = new ArrayWrapper([1,2]);\nconst obj2 = new ArrayWrapper([3,4]);\nobj1 + obj2; // 10\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [[23,98,42,70]], operation = "String"\n<strong>Output:</strong> "[23,98,42,70]"\n<strong>Explanation:</strong>\nconst obj = new ArrayWrapper([23,98,42,70]);\nString(obj); // "[23,98,42,70]"\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [[],[]], operation = "Add"\n<strong>Output:</strong> 0\n<strong>Explanation:</strong>\nconst obj1 = new ArrayWrapper([]);\nconst obj2 = new ArrayWrapper([]);\nobj1 + obj2; // 0\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= nums.length <= 1000</code></li>\n\t<li><code>0 <= nums[i] <= 1000</code></li>\n\t<li><code>Note: nums is the array passed to the constructor</code></li>\n</ul>\n",
|
||||
"translatedTitle": "包装数组",
|
||||
"translatedContent": "<p>创建一个名为 <code>ArrayWrapper</code> 的类,它在其构造函数中接受一个整数数组作为参数。该类应具有以下两个特性:</p>\n\n<ul>\n\t<li>当使用 <code>+</code> 运算符将两个该类的实例相加时,结果值为两个数组中所有元素的总和。</li>\n\t<li>当在实例上调用 <code>String()</code> 函数时,它将返回一个由逗号分隔的括在方括号中的字符串。例如,<code>[1,2,3]</code> 。</li>\n</ul>\n\n<p> </p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<b>输入:</b>nums = [[1,2],[3,4]], operation = \"Add\"\n<b>输出:</b>10\n<b>解释:</b>\nconst obj1 = new ArrayWrapper([1,2]);\nconst obj2 = new ArrayWrapper([3,4]);\nobj1 + obj2; // 10\n</pre>\n\n<p><strong class=\"example\">示例 2:</strong></p>\n\n<pre>\n<b>输入:</b>nums = [[23,98,42,70]], operation = \"String\"\n<b>输出:</b>\"[23,98,42,70]\"\n<strong>解释:</strong>\nconst obj = new ArrayWrapper([23,98,42,70]);\nString(obj); // \"[23,98,42,70]\"\n</pre>\n\n<p><strong class=\"example\">示例 3:</strong></p>\n\n<pre>\n<b>输入:</b>nums = [[],[]], operation = \"Add\"\n<b>输出:</b>0\n<strong>解释:</strong>\nconst obj1 = new ArrayWrapper([]);\nconst obj2 = new ArrayWrapper([]);\nobj1 + obj2; // 0\n</pre>\n\n<p> </p>\n\n<p><b>提示:</b></p>\n\n<ul>\n\t<li><code>0 <= nums.length <= 1000</code></li>\n\t<li><code>0 <= nums[i] <= 1000</code></li>\n\t<li><code>注意:nums 是传递给构造函数的数组。</code></li>\n</ul>\n",
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 1,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": "{\"cpp\": false, \"java\": true, \"python\": true, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {number[]} nums\n */\nvar ArrayWrapper = function(nums) {\n \n};\n\nArrayWrapper.prototype.valueOf = function() {\n \n}\n\nArrayWrapper.prototype.toString = function() {\n \n}\n\n/**\n * const obj1 = new ArrayWrapper([1,2]);\n * const obj2 = new ArrayWrapper([3,4]);\n * obj1 + obj2; // 10\n * String(obj1); // \"[1,2]\"\n * String(obj2); // \"[3,4]\"\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "class ArrayWrapper {\n\tconstructor(nums: number[]) {\n \n }\n\n\tvalueOf() {\n \n }\n\n\ttoString() {\n \n }\n};\n\n/**\n * const obj1 = new ArrayWrapper([1,2]);\n * const obj2 = new ArrayWrapper([3,4]);\n * obj1 + obj2; // 10\n * String(obj1); // \"[1,2]\"\n * String(obj2); // \"[3,4]\"\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"337\", \"totalSubmission\": \"475\", \"totalAcceptedRaw\": 337, \"totalSubmissionRaw\": 475, \"acRate\": \"70.9%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[[1,2],[3,4]]\n\"Add\"",
|
||||
"metaData": "{\n \"name\": \"ArrayWrapper\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[][]\"\n },\n {\n \"type\": \"string\",\n \"name\": \"operation\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "[[1,2],[3,4]]\n\"Add\"\n[[23,98,42,70]]\n\"String\"\n[[],[]]\n\"Add\"",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
176
leetcode-cn/originData/buy-two-chocolates.json
Normal file
176
leetcode-cn/originData/buy-two-chocolates.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2790",
|
||||
"questionFrontendId": "2693",
|
||||
"categoryTitle": "JavaScript",
|
||||
"boundTopicId": 2275502,
|
||||
"title": "Call Function with Custom Context",
|
||||
"titleSlug": "call-function-with-custom-context",
|
||||
"content": "<p>Enhance all functions to have the <code>callPolyfill</code> method. The method accepts an object <code>obj</code> as it's first parameter and any number of additional arguments. The <code>obj</code> becomes the <code>this</code> context for the function. The additional arguments are passed to the function (that the <code>callPolyfill</code> method belongs on).</p>\n\n<p>For example if you had the function:</p>\n\n<pre>\nfunction tax(price, taxRate) {\n const totalCost = price * (1 + taxRate);\n console.log(`The cost of ${this.item} is ${totalCost}`);\n}\n</pre>\n\n<p>Calling this function like <code>tax(10, 0.1)</code> will log <code>"The cost of undefined is 11"</code>. This is because the <code>this</code> context was not defined.</p>\n\n<p>However, calling the function like <code>tax.callPolyfill({item: "salad"}, 10, 0.1)</code> will log <code>"The cost of salad is 11"</code>. The <code>this</code> context was appropriately set, and the function logged an appropriate output.</p>\n\n<p>Please solve this without using the built-in <code>Function.call</code> method.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfn = function add(b) {\n return this.a + b;\n}\nargs = [{"a": 5}, 7]\n<strong>Output:</strong> 12\n<strong>Explanation:</strong>\nfn.callPolyfill({"a": 5}, 7); // 12\ncallPolyfill sets the "this" context to {"a": 5}. 7 is passed as an argument.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = function tax(price, taxRate) { \n return `The cost of the ${this.item} is ${price * taxRate}`; \n}\nargs = [{"item": "burger"}, 10, 1,1]\n<strong>Output:</strong> "The cost of the burger is 11"\n<strong>Explanation:</strong> callPolyfill sets the "this" context to {"item": "burger"}. 10 and 1.1 are passed as additional arguments.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul style=\"list-style-type:square;\">\n\t<li><code><font face=\"monospace\">typeof args[0] == 'object' and args[0] != null</font></code></li>\n\t<li><code>1 <= args.length <= 100</code></li>\n\t<li><code>2 <= JSON.stringify(args[0]).length <= 10<sup>5</sup></code></li>\n</ul>\n",
|
||||
"translatedTitle": "使用自定义上下文调用函数",
|
||||
"translatedContent": "<p>增强所有函数,使其具有 <code>callPolyfill</code> 方法。该方法接受一个对象 <code>obj</code> 作为第一个参数,以及任意数量的附加参数。<code>obj</code> 成为函数的 <code>this</code> 上下文。附加参数将传递给该函数(即 <code>callPolyfill</code> 方法所属的函数)。</p>\n\n<p>例如,如果有以下函数:</p>\n\n<pre>\nfunction tax(price, taxRate) {\n const totalCost = price * (1 + taxRate);\n console.log(`The cost of ${this.item} is ${totalCost}`);\n}\n</pre>\n\n<p>调用 <code>tax(10, 0.1)</code> 将输出 <code>\"The cost of undefined is 11\"</code> 。这是因为 <code>this</code> 上下文未定义。</p>\n\n<p>然而,调用 <code>tax.callPolyfill({item: \"salad\"}, 10, 0.1)</code> 将输出 <code>\"The cost of salad is 11\"</code> 。<code>this</code> 上下文被正确设置,函数输出了适当的结果。</p>\n\n<p>请在不使用内置的 <code>Function.call</code> 方法的情况下解决这个问题。</p>\n\n<p> </p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<strong>输入:</strong>\nfn = function add(b) {\n return this.a + b;\n}\nargs = [{\"a\": 5}, 7]\n<b>输出:</b>12\n<strong>解释:</strong>\nfn.callPolyfill({\"a\": 5}, 7); // 12\n<code>callPolyfill </code>将 \"this\" 上下文设置为 <code>{\"a\": 5} </code>,并将 7 作为参数传递。\n</pre>\n\n<p><strong class=\"example\">示例 2:</strong></p>\n\n<pre>\n<b>输入:</b>\nfn = function tax(price, taxRate) { \n return `The cost of the ${this.item} is ${price * taxRate}`; \n}\nargs = [{\"item\": \"burger\"}, 10, 1,1]\n<b>输出:</b>\"The cost of the burger is 11\"\n<b>解释:</b><code>callPolyfill </code>将 \"this\" 上下文设置为 <code>{\"item\": \"burger\"} </code>,并将 10 和 1.1 作为附加参数传递。\n</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul style=\"list-style-type:square;\">\n\t<li><code><font face=\"monospace\">typeof args[0] == 'object' and args[0] != null</font></code></li>\n\t<li><code>1 <= args.length <= 100</code></li>\n\t<li><code>2 <= JSON.stringify(args[0]).length <= 10<sup>5</sup></code></li>\n</ul>\n",
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 1,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Object} context\n * @param {any[]} args\n * @return {any}\n */\nFunction.prototype.callPolyfill = function(context, ...args) {\n\n}\n\n/**\n * function increment() { this.count++; return this.count; }\n * increment.callPolyfill({count: 1}); // 2\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "declare global { \n interface Function {\n callPolyfill(context: Record<any, any>, ...args: any[]): any;\n\t}\n}\n\nFunction.prototype.callPolyfill = function(context, ...args): any {\n\n}\n\n/**\n * function increment() { this.count++; return this.count; }\n * increment.callPolyfill({count: 1}); // 2\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"397\", \"totalSubmission\": \"510\", \"totalAcceptedRaw\": 397, \"totalSubmissionRaw\": 510, \"acRate\": \"77.8%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "function add(b) { return this.a + b; }\n[{\"a\":5},7]",
|
||||
"metaData": "{\n \"name\": \"callPolyfill\",\n \"params\": [\n {\n \"type\": \"string\",\n \"name\": \"context\"\n },\n {\n \"type\": \"string\",\n \"name\": \"args\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "function add(b) { return this.a + b; }\n[{\"a\":5},7]\nfunction tax(price, taxRate) { return `The cost of the ${this.item} is ${price * taxRate}`; }\n[{\"item\":\"burger\"},10,1.1]",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
59
leetcode-cn/originData/compact-object.json
Normal file
59
leetcode-cn/originData/compact-object.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2804",
|
||||
"questionFrontendId": "2705",
|
||||
"categoryTitle": "JavaScript",
|
||||
"boundTopicId": 2289205,
|
||||
"title": "Compact Object",
|
||||
"titleSlug": "compact-object",
|
||||
"content": "<p>Given an object or array <code>obj</code>, return a <strong>compact object</strong>. A <strong>compact object</strong> is the same as the original object, except with keys containing <strong>falsy</strong> values removed. This operation applies to the object and any nested objects. Arrays are considered objects where the indices are keys. A value is considered <strong>falsy</strong> when <code>Boolean(value)</code> returns <code>false</code>.</p>\n\n<p>You may assume the <code>obj</code> is the output of <code>JSON.parse</code>. In other words, it is valid JSON.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> obj = [null, 0, false, 1]\n<strong>Output:</strong> [1]\n<strong>Explanation:</strong> All falsy values have been removed from the array.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> obj = {"a": null, "b": [false, 1]}\n<strong>Output:</strong> {"b": [1]}\n<strong>Explanation:</strong> obj["a"] and obj["b"][0] had falsy values and were removed.</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> obj = [null, 0, 5, [0], [false, 16]]\n<strong>Output:</strong> [5, [], [16]]\n<strong>Explanation:</strong> obj[0], obj[1], obj[3][0], and obj[4][0] were falsy and removed.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>obj is a valid JSON object</code></li>\n\t<li><code>2 <= JSON.stringify(obj).length <= 10<sup>6</sup></code></li>\n</ul>\n",
|
||||
"translatedTitle": "精简对象",
|
||||
"translatedContent": "<p>现给定一个对象或数组 <code>obj</code>,返回一个 <strong>精简对象</strong> 。<strong>精简对象</strong> 与原始对象相同,只是将包含 <strong>假</strong> 值的键移除。该操作适用于对象及其嵌套对象。数组被视为索引作为键的对象。当 <code>Boolean(value)</code> 返回 <code>false</code> 时,值被视为 <strong>假</strong> 值。</p>\n\n<p>你可以假设 <code>obj</code> 是 <code>JSON.parse</code> 的输出结果。换句话说,它是有效的 JSON。</p>\n\n<p> </p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<b>输入:</b>obj = [null, 0, false, 1]\n<b>输出:</b>[1]\n<b>解释:</b>数组中的所有假值已被移除。\n</pre>\n\n<p><strong class=\"example\">示例 2:</strong></p>\n\n<pre>\n<b>输入:</b>obj = {\"a\": null, \"b\": [false, 1]}\n<b>输出:</b>{\"b\": [1]}\n<b>解释:</b>obj[\"a\"] 和 obj[\"b\"][0] 包含假值,因此被移除。</pre>\n\n<p><strong class=\"example\">示例 3:</strong></p>\n\n<pre>\n<b>输入:</b>obj = [null, 0, 5, [0], [false, 16]]\n<b>输出:</b>[5, [], [16]]\n<b>解释:</b>obj[0], obj[1], obj[3][0], 和 obj[4][0] 包含假值,因此被移除。\n</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>obj 是一个有效的 JSON 对象</code></li>\n\t<li><code>2 <= JSON.stringify(obj).length <= 10<sup>6</sup></code></li>\n</ul>\n",
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Object} obj\n * @return {Object}\n */\nvar compactObject = function(obj) {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type Obj = Record<any, any>;\n\nfunction compactObject(obj: Obj): Obj {\n\n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"89\", \"totalSubmission\": \"115\", \"totalAcceptedRaw\": 89, \"totalSubmissionRaw\": 115, \"acRate\": \"77.4%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[null, 0, false, 1]",
|
||||
"metaData": "{\n \"name\": \"compactObject\",\n \"params\": [\n {\n \"name\": \"obj\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "[null, 0, false, 1]\n{\"a\": null, \"b\": [false, 1]}\n[null, 0, 5, [0], [false, 16]]",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
63
leetcode-cn/originData/differences-between-two-objects.json
Normal file
63
leetcode-cn/originData/differences-between-two-objects.json
Normal file
@ -0,0 +1,63 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2774",
|
||||
"questionFrontendId": "2700",
|
||||
"categoryTitle": "JavaScript",
|
||||
"boundTopicId": 2278876,
|
||||
"title": "Differences Between Two Objects",
|
||||
"titleSlug": "differences-between-two-objects",
|
||||
"content": "<p>Write a function that accepts two deeply nested objects or arrays <code>obj1</code> and <code>obj2</code> and returns a new object representing their differences.</p>\n\n<p>The function should compare the properties of the two objects and identify any changes. The returned object should only contains keys where the value is different from <code>obj1</code> to <code>obj2</code>. For each changed key, the value should be represented as an array <code>[obj1 value, obj2 value]</code>. Keys that exist in one object but not in the other should not be included in the returned object. When comparing two arrays, the indices of the arrays are considered to be their keys. The end result should be a deeply nested object where each leaf value is a difference array.</p>\n\n<p>You may assume that both objects are the output of <code>JSON.parse</code>.</p>\n\n<p> </p>\n<p><strong>Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nobj1 = {}\nobj2 = {\n "a": 1, \n "b": 2\n}\n<strong>Output:</strong> {}\n<strong>Explanation:</strong> There were no modifications made to obj1. New keys "a" and "b" appear in obj2, but keys that are added or removed should be ignored.\n</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nobj1 = {\n "a": 1,\n "v": 3,\n "x": [],\n "z": {\n "a": null\n }\n}\nobj2 = {\n "a": 2,\n "v": 4,\n "x": [],\n "z": {\n "a": 2\n }\n}\n<strong>Output:</strong> \n{\n "a": [1, 2],\n "v": [3, 4],\n "z": {\n "a": [null, 2]\n }\n}\n<strong>Explanation:</strong> The keys "a", "v", and "z" all had changes applied. "a" was chnaged from 1 to 2. "v" was changed from 3 to 4. "z" had a change applied to a child object. "z.a" was changed from null to 2.\n</pre>\n\n<p><strong>Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nobj1 = {\n "a": 5, \n "v": 6, \n "z": [1, 2, 4, [2, 5, 7]]\n}\nobj2 = {\n "a": 5, \n "v": 7, \n "z": [1, 2, 3, [1]]\n}\n<strong>Output:</strong> \n{\n "v": [6, 7],\n "z": {\n "2": [4, 3],\n "3": {\n "0": [2, 1]\n }\n }\n}\n<strong>Explanation:</strong> In obj1 and obj2, the keys "v" and "z" have different assigned values. "a" is ignored because the value is unchanged. In the key "z", there is a nested array. Arrays are treated like objects where the indices are keys. There were two alterations to the the array: z[2] and z[3][0]. z[0] and z[1] were unchanged and thus not included. z[3][1] and z[3][2] were removed and thus not included.\n</pre>\n\n<p><strong>Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nobj1 = {\n "a": {"b": 1}, \n}\nobj2 = {\n "a": [5],\n}\n<strong>Output:</strong> \n{\n "a": [{"b": 1}, [5]]\n}\n<strong>Explanation:</strong> The key "a" exists in both objects. Since the two associated values have different types, they are placed in the difference array.</pre>\n\n<p><strong>Example 5:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nobj1 = {\n "a": [1, 2, {}], \n "b": false\n}\nobj2 = { \n "b": false,\n "a": [1, 2, {}]\n}\n<strong>Output:</strong> \n{}\n<strong>Explanation:</strong> Apart from a different ordering of keys, the two objects are identical so an empty object is returned.</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 <= JSON.stringify(obj1).length <= 10<sup>4</sup></code></li>\n\t<li><code>2 <= JSON.stringify(obj2).length <= 10<sup>4</sup></code></li>\n</ul>\n",
|
||||
"translatedTitle": "两个对象之间的差异",
|
||||
"translatedContent": "<p>请你编写一个函数,它接收两个深度嵌套的对象或数组 <code>obj1</code> 和 <code>obj2</code> ,并返回一个新对象表示它们之间差异。</p>\n\n<p>该函数应该比较这两个对象的属性,并识别任何变化。返回的对象应仅包含从 <code>obj1</code> 到 <code>obj2</code> 的值不同的键。对于每个变化的键,值应表示为一个数组 <code>[obj1 value, obj2 value]</code> 。不存在于一个对象中但存在于另一个对象中的键不应包含在返回的对象中。在比较两个数组时,数组的索引被视为它们的键。最终结果应是一个深度嵌套的对象,其中每个叶子的值都是一个差异数组。</p>\n\n<p>你可以假设这两个对象都是 <code>JSON.parse</code> 的输出结果。</p>\n\n<p> </p>\n\n<p><strong>示例 1:</strong></p>\n\n<pre>\n<strong>输入:</strong> \nobj1 = {}\nobj2 = {\n \"a\": 1, \n \"b\": 2\n}\n<b>输出:</b>{}\n<b>解释:</b>obj1没有进行任何修改。obj2中出现了新的键 \"a\" 和 \"b\" ,但添加或删除的键应该被忽略。\n</pre>\n\n<p><strong>示例 2:</strong></p>\n\n<pre>\n<b>输入:</b>\nobj1 = {\n \"a\": 1,\n \"v\": 3,\n \"x\": [],\n \"z\": {\n \"a\": null\n }\n}\nobj2 = {\n \"a\": 2,\n \"v\": 4,\n \"x\": [],\n \"z\": {\n \"a\": 2\n }\n}\n<b>输出:</b>\n{\n \"a\": [1, 2],\n \"v\": [3, 4],\n \"z\": {\n \"a\": [null, 2]\n }\n}\n<b>解释:</b>键 \"a\"、\"v\" 和 \"z\" 都有变化。\"a\" 从 1 变为 2,\"v\" 从 3 变为 4 ,\"z\" 的子对象 \"a\" 从 null 变为 2。\n</pre>\n\n<p><strong>示例 3:</strong></p>\n\n<pre>\n<b>输入:</b>\nobj1 = {\n \"a\": 5, \n \"v\": 6, \n \"z\": [1, 2, 4, [2, 5, 7]]\n}\nobj2 = {\n \"a\": 5, \n \"v\": 7, \n \"z\": [1, 2, 3, [1]]\n}\n<b>输出:</b>\n{\n \"v\": [6, 7],\n \"z\": {\n \"2\": [4, 3],\n \"3\": {\n \"0\": [2, 1]\n }\n }\n}\n<b>解释:</b>在 obj1 和 obj2 中,键 \"v\" 和 \"z\" 的值不同。\"a\" 被忽略,因为值没有变化。在键 \"z\" 中,有一个嵌套的数组。数组被视为对象,其中索引被视为键。数组发生了两处变化:z[2] 和 z[3][0]。z[0] 和 z[1] 没有变化,因此没有包含在结果中。z[3][1] 和 z[3][2] 被删除,因此也没有包含在结果中。\n</pre>\n\n<p><strong>示例 4:</strong></p>\n\n<pre>\n<b>输入:</b>\nobj1 = {\n \"a\": {\"b\": 1}, \n}\nobj2 = {\n \"a\": [5],\n}\n<b>输出:</b>\n{\n \"a\": [{\"b\": 1}, [5]]\n}\n<b>解释:</b>键 \"a\" 在两个对象中都存在。但由于两个相关值具有不同的类型,所以它们被放置在差异数组中。</pre>\n\n<p><strong>示例 5:</strong></p>\n\n<pre>\n<b>输入:</b>\nobj1 = {\n \"a\": [1, 2, {}], \n \"b\": false\n}\nobj2 = { \n \"b\": false,\n \"a\": [1, 2, {}]\n}\n<b>输出:</b>\n{}\n<b>解释:</b>除了键的顺序不同之外,两个对象是相同的,因此返回一个空对象。</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>2 <= JSON.stringify(obj1).length <= 10<sup>4</sup></code></li>\n\t<li><code>2 <= JSON.stringify(obj2).length <= 10<sup>4</sup></code></li>\n</ul>\n",
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 1,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {object} obj1\n * @param {object} obj2\n * @return {object}\n */\nfunction objDiff(obj1, obj2) {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "function objDiff(obj1: any, obj2: any): any {\n\n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"349\", \"totalSubmission\": \"596\", \"totalAcceptedRaw\": 349, \"totalSubmissionRaw\": 596, \"acRate\": \"58.6%\"}",
|
||||
"hints": [
|
||||
"Find the intersection of the keys/indices on the two arrays/objects.",
|
||||
"Analyze the data structure recursively.",
|
||||
"For each key in the intersection, omit if there are no differences in the leaves. Otherwise return the difference."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{}\n{\"a\": 1, \"b\": 2}",
|
||||
"metaData": "{\n \"name\": \"objDiff\",\n \"params\": [\n {\n \"name\": \"obj1\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"obj2\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "{}\n{\"a\": 1, \"b\": 2}\n{\"a\": 1, \"v\": 3, \"x\": [], \"z\": {\"a\": null}}\n{\"a\": 2, \"v\": 4, \"x\": [], \"z\": {\"a\": 2}}\n{\"a\": 5, \"v\": 6, \"z\": [1,2,4, [2,5,7]]}\n{\"a\": 5, \"v\": 7, \"z\": [1,2,3, [1]]}\n{\"a\":{\"b\":1}}\n{\"a\":[5]}\n{\"a\": [1, 2, {}],\"b\": false}\n{\"b\": false, \"a\": [1, 2, {}]}",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
59
leetcode-cn/originData/event-emitter.json
Normal file
59
leetcode-cn/originData/event-emitter.json
Normal file
File diff suppressed because one or more lines are too long
195
leetcode-cn/originData/extra-characters-in-a-string.json
Normal file
195
leetcode-cn/originData/extra-characters-in-a-string.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
191
leetcode-cn/originData/greatest-common-divisor-traversal.json
Normal file
191
leetcode-cn/originData/greatest-common-divisor-traversal.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
190
leetcode-cn/originData/maximum-strength-of-a-group.json
Normal file
190
leetcode-cn/originData/maximum-strength-of-a-group.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
186
leetcode-cn/originData/modify-graph-edge-weights.json
Normal file
186
leetcode-cn/originData/modify-graph-edge-weights.json
Normal file
File diff suppressed because one or more lines are too long
170
leetcode-cn/originData/remove-trailing-zeros-from-a-string.json
Normal file
170
leetcode-cn/originData/remove-trailing-zeros-from-a-string.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2820",
|
||||
"questionFrontendId": "2703",
|
||||
"categoryTitle": "JavaScript",
|
||||
"boundTopicId": 2285880,
|
||||
"title": "Return Length of Arguments Passed",
|
||||
"titleSlug": "return-length-of-arguments-passed",
|
||||
"content": "Write a function <code>argumentsLength</code> that returns the count of arguments passed to it.\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> argsArr = [5]\n<strong>Output:</strong> 1\n<strong>Explanation:</strong>\nargumentsLength(5); // 1\n\nOne value was passed to the function so it should return 1.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> argsArr = [{}, null, "3"]\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> \nargumentsLength({}, null, "3"); // 3\n\nThree values were passed to the function so it should return 3.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>argsArr is a valid JSON array</code></li>\n\t<li><code>0 <= argsArr.length <= 100</code></li>\n</ul>\n",
|
||||
"translatedTitle": "返回传递的参数的长度",
|
||||
"translatedContent": "请你编写一个函数 <code>argumentsLength</code>,返回传递给该函数的参数数量。\n<p> </p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<b>输入:</b>argsArr = [5]\n<b>输出:</b>1\n<strong>解释:</strong>\nargumentsLength(5); // 1\n\n只传递了一个值给函数,因此它应返回 1。\n</pre>\n\n<p><strong class=\"example\">示例 2:</strong></p>\n\n<pre>\n<b>输入:</b>argsArr = [{}, null, \"3\"]\n<b>输出:</b>3\n<b>解释:</b>\nargumentsLength({}, null, \"3\"); // 3\n\n传递了三个值给函数,因此它应返回 3。\n</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>argsArr 是一个有效的 JSON 数组</code></li>\n\t<li><code>0 <= argsArr.length <= 100</code></li>\n</ul>\n",
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @return {number}\n */\nvar argumentsLength = function(...args) {\n \n};\n\n/**\n * argumentsLength(1, 2, 3); // 3\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "function argumentsLength(...args: any[]): number {\n\n};\n\n/**\n * argumentsLength(1, 2, 3); // 3\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"248\", \"totalSubmission\": \"255\", \"totalAcceptedRaw\": 248, \"totalSubmissionRaw\": 255, \"acRate\": \"97.3%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[5]",
|
||||
"metaData": "{\n \"name\": \"argumentsLength\",\n \"params\": [\n {\n \"type\": \"string\",\n \"name\": \"argsArr\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "[5]\n[{},null,\"3\"]",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
59
leetcode-cn/originData/to-be-or-not-to-be.json
Normal file
59
leetcode-cn/originData/to-be-or-not-to-be.json
Normal file
@ -0,0 +1,59 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2813",
|
||||
"questionFrontendId": "2704",
|
||||
"categoryTitle": "JavaScript",
|
||||
"boundTopicId": 2289200,
|
||||
"title": "To Be Or Not To Be",
|
||||
"titleSlug": "to-be-or-not-to-be",
|
||||
"content": "<p>Write a function <code>expect</code> that helps developers test their code. It should take in any value <code>val</code> and return an object with the following two functions.</p>\n\n<ul>\n\t<li><code>toBe(val)</code> accepts another value and returns <code>true</code> if the two values <code>===</code> each other. If they are not equal, it should throw an error <code>"Not Equal"</code>.</li>\n\t<li><code>notToBe(val)</code> accepts another value and returns <code>true</code> if the two values <code>!==</code> each other. If they are equal, it should throw an error <code>"Equal"</code>.</li>\n</ul>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => expect(5).toBe(5)\n<strong>Output:</strong> {"value": true}\n<strong>Explanation:</strong> 5 === 5 so this expression returns true.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => expect(5).toBe(null)\n<strong>Output:</strong> {"error": "Not Equal"}\n<strong>Explanation:</strong> 5 !== null so this expression throw the error "Not Equal".\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => expect(5).notToBe(null)\n<strong>Output:</strong> {"value": true}\n<strong>Explanation:</strong> 5 !== null so this expression returns true.\n</pre>\n",
|
||||
"translatedTitle": "相等还是不相等",
|
||||
"translatedContent": "<p>请你编写一个名为 <code>expect</code> 的函数,用于帮助开发人员测试他们的代码。它应该接受任何值 <code>val</code> 并返回一个包含以下两个函数的对象。</p>\n\n<ul>\n\t<li><code>toBe(val)</code> 接受另一个值并在两个值相等( <code>===</code> )时返回 <code>true</code> 。如果它们不相等,则应抛出错误 <code>\"Not Equal\"</code> 。</li>\n\t<li><code>notToBe(val)</code> 接受另一个值并在两个值不相等( <code>!==</code> )时返回 <code>true</code> 。如果它们相等,则应抛出错误 <code>\"Equal\"</code> 。</li>\n</ul>\n\n<p> </p>\n\n<p><strong class=\"example\">示例 1:</strong></p>\n\n<pre>\n<b>输入:</b>func = () => expect(5).toBe(5)\n<b>输出:</b>{\"value\": true}\n<b>解释:</b>5 === 5 因此该表达式返回 true。\n</pre>\n\n<p><strong class=\"example\">示例 2:</strong></p>\n\n<pre>\n<b>输入:</b>func = () => expect(5).toBe(null)\n<b>输出:</b>{\"error\": \"Not Equal\"}\n<b>解释:</b>5 !== null 因此抛出错误 \"Not Equal\".\n</pre>\n\n<p><strong class=\"example\">示例 3:</strong></p>\n\n<pre>\n<b>输入:</b>func = () => expect(5).notToBe(null)\n<b>输出:</b>{\"value\": true}\n<b>解释:</b>5 !== null 因此该表达式返回 true.\n</pre>\n",
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 0,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"contributors": [],
|
||||
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {string} val\n * @return {Object}\n */\nvar expect = function(val) {\n \n};\n\n/**\n * expect(5).toBe(5); // true\n * expect(5).notToBe(5); // throws \"Equal\"\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type ToBeOrNotToBe = {\n toBe: (val: any) => boolean;\n notToBe: (val: any) => boolean;\n};\n\nfunction expect(val: any): ToBeOrNotToBe {\n\n};\n\n/**\n * expect(5).toBe(5); // true\n * expect(5).notToBe(5); // throws \"Equal\"\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"107\", \"totalSubmission\": \"162\", \"totalAcceptedRaw\": 107, \"totalSubmissionRaw\": 162, \"acRate\": \"66.0%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "() => expect(5).toBe(5)",
|
||||
"metaData": "{\n \"name\": \"expect\",\n \"params\": [\n {\n \"name\": \"func\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
|
||||
"book": null,
|
||||
"isSubscribed": false,
|
||||
"isDailyQuestion": false,
|
||||
"dailyRecordStatus": null,
|
||||
"editorType": "CKEDITOR",
|
||||
"ugcQuestionId": null,
|
||||
"style": "LEETCODE",
|
||||
"exampleTestcases": "() => expect(5).toBe(5)\n() => expect(5).toBe(null)\n() => expect(5).notToBe(null)",
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
@ -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>
|
@ -0,0 +1,28 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> representing the score of students in an exam. The teacher would like to form one <strong>non-empty</strong> group of students with maximal <strong>strength</strong>, where the strength of a group of students of indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, ... , <code>i<sub>k</sub></code> is defined as <code>nums[i<sub>0</sub>] * nums[i<sub>1</sub>] * nums[i<sub>2</sub>] * ... * nums[i<sub>k</sub>]</code>.</p>
|
||||
|
||||
<p>Return <em>the maximum strength of a group the teacher can create</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,-1,-5,2,5,-9]
|
||||
<strong>Output:</strong> 1350
|
||||
<strong>Explanation:</strong> One way to form a group of maximal strength is to group the students at indices [0,2,3,4,5]. Their strength is 3 * (-5) * 2 * 5 * (-9) = 1350, which we can show is optimal.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-4,-5,-4]
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong> Group the students at indices [0, 1] . Then, we’ll have a resulting strength of 20. We cannot achieve greater strength.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 13</code></li>
|
||||
<li><code>-9 <= nums[i] <= 9</code></li>
|
||||
</ul>
|
@ -0,0 +1,117 @@
|
||||
<p>Write a function that accepts two deeply nested objects or arrays <code>obj1</code> and <code>obj2</code> and returns a new object representing their differences.</p>
|
||||
|
||||
<p>The function should compare the properties of the two objects and identify any changes. The returned object should only contains keys where the value is different from <code>obj1</code> to <code>obj2</code>. For each changed key, the value should be represented as an array <code>[obj1 value, obj2 value]</code>. Keys that exist in one object but not in the other should not be included in the returned object. When comparing two arrays, the indices of the arrays are considered to be their keys. The end result should be a deeply nested object where each leaf value is a difference array.</p>
|
||||
|
||||
<p>You may assume that both objects are the output of <code>JSON.parse</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
obj1 = {}
|
||||
obj2 = {
|
||||
"a": 1,
|
||||
"b": 2
|
||||
}
|
||||
<strong>Output:</strong> {}
|
||||
<strong>Explanation:</strong> There were no modifications made to obj1. New keys "a" and "b" appear in obj2, but keys that are added or removed should be ignored.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
obj1 = {
|
||||
"a": 1,
|
||||
"v": 3,
|
||||
"x": [],
|
||||
"z": {
|
||||
"a": null
|
||||
}
|
||||
}
|
||||
obj2 = {
|
||||
"a": 2,
|
||||
"v": 4,
|
||||
"x": [],
|
||||
"z": {
|
||||
"a": 2
|
||||
}
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{
|
||||
"a": [1, 2],
|
||||
"v": [3, 4],
|
||||
"z": {
|
||||
"a": [null, 2]
|
||||
}
|
||||
}
|
||||
<strong>Explanation:</strong> The keys "a", "v", and "z" all had changes applied. "a" was chnaged from 1 to 2. "v" was changed from 3 to 4. "z" had a change applied to a child object. "z.a" was changed from null to 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
obj1 = {
|
||||
"a": 5,
|
||||
"v": 6,
|
||||
"z": [1, 2, 4, [2, 5, 7]]
|
||||
}
|
||||
obj2 = {
|
||||
"a": 5,
|
||||
"v": 7,
|
||||
"z": [1, 2, 3, [1]]
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{
|
||||
"v": [6, 7],
|
||||
"z": {
|
||||
"2": [4, 3],
|
||||
"3": {
|
||||
"0": [2, 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
<strong>Explanation:</strong> In obj1 and obj2, the keys "v" and "z" have different assigned values. "a" is ignored because the value is unchanged. In the key "z", there is a nested array. Arrays are treated like objects where the indices are keys. There were two alterations to the the array: z[2] and z[3][0]. z[0] and z[1] were unchanged and thus not included. z[3][1] and z[3][2] were removed and thus not included.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
obj1 = {
|
||||
"a": {"b": 1},
|
||||
}
|
||||
obj2 = {
|
||||
"a": [5],
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{
|
||||
"a": [{"b": 1}, [5]]
|
||||
}
|
||||
<strong>Explanation:</strong> The key "a" exists in both objects. Since the two associated values have different types, they are placed in the difference array.</pre>
|
||||
|
||||
<p><strong>Example 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
obj1 = {
|
||||
"a": [1, 2, {}],
|
||||
"b": false
|
||||
}
|
||||
obj2 = {
|
||||
"b": false,
|
||||
"a": [1, 2, {}]
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{}
|
||||
<strong>Explanation:</strong> Apart from a different ordering of keys, the two objects are identical so an empty object is returned.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>
|
@ -0,0 +1,64 @@
|
||||
<p>Design an <code>EventEmitter</code> class. This interface is similar (but with some differences) to the one found in Node.js or the Event Target interface of the DOM. The <code>EventEmitter</code> should allow for subscribing to events and emitting them.</p>
|
||||
|
||||
<p>Your <code>EventEmitter</code> class should have the following two methods:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>subscribe</strong> - This method takes in two arguments: the name of an event as a string and a callback function. This callback function will later be called when the event is emitted.<br />
|
||||
An event should be able to have multiple listeners for the same event. When emitting an event with multiple callbacks, each should be called in the order in which they were subscribed. An array of results should be returned. You can assume no callbacks passed to <code>subscribe</code> are referentially identical.<br />
|
||||
The <code>subscribe</code> method should also return an object with an <code>unsubscribe</code> method that enables the user to unsubscribe. When it is called, the callback should be removed from the list of subscriptions and <code>undefined</code> should be returned.</li>
|
||||
<li><strong>emit</strong> - This method takes in two arguments: the name of an event as a string and an optional array of arguments that will be passed to the callback(s). If there are no callbacks subscribed to the given event, return an empty array. Otherwise, return an array of the results of all callback calls in the order they were subscribed.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> actions = ["EventEmitter", "emit", "subscribe", "subscribe", "emit"], values = [[], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent"]]
|
||||
<strong>Output:</strong> [[],["emitted",[]],["subscribed"],["subscribed"],["emitted",[5,6]]]
|
||||
<strong>Explanation:</strong>
|
||||
const emitter = new EventEmitter();
|
||||
emitter.emit("firstEvent"); // [], no callback are subscribed yet
|
||||
emitter.subscribe("firstEvent", function cb1() { return 5; });
|
||||
emitter.subscribe("firstEvent", function cb2() { return 6; });
|
||||
emitter.emit("firstEvent"); // [5, 6], returns the output of cb1 and cb2
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> actions = ["EventEmitter", "subscribe", "emit", "emit"], values = [[], ["firstEvent", "function cb1(...args) { return args.join(','); }"], ["firstEvent", [1,2,3]], ["firstEvent", [3,4,6]]]
|
||||
<strong>Output:</strong> [[],["subscribed"],["emitted",["1,2,3"]],["emitted",["3,4,6"]]]
|
||||
<strong>Explanation: </strong>Note that the emit method should be able to accept an OPTIONAL array of arguents.
|
||||
|
||||
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 class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> actions = ["EventEmitter", "subscribe", "emit", "unsubscribe", "emit"], values = [[], ["firstEvent", "(...args) => args.join(',')"], ["firstEvent", [1,2,3]], [0], ["firstEvent", [4,5,6]]]
|
||||
<strong>Output:</strong> [[],["subscribed"],["emitted",["1,2,3"]],["unsubscribed",0],["emitted",[]]]
|
||||
<strong>Explanation:</strong>
|
||||
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]); // [], there are no subscriptions
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= actions.length <= 10</code></li>
|
||||
<li><code>values.length === actions.length</code></li>
|
||||
<li>All test cases are valid, e.g. you don't need to handle scenarios when unsubscribing from a non-existing subscription.</li>
|
||||
<li>There are only 4 different actions: <code>EventEmitter</code>, <code>emit</code>, <code>subscribe</code>, and <code>unsubscribe</code>.</li>
|
||||
<li>The <code>EventEmitter</code> action doesn't take any arguments.</li>
|
||||
<li>The <code>emit</code> action takes between either 1 or 2 arguments. The first argument is the name of the event we want to emit, and the 2nd argument is passed to the callback functions.</li>
|
||||
<li>The <code>subscribe</code> action takes 2 arguments, where the first one is the event name and the second is the callback function.</li>
|
||||
<li>The <code>unsubscribe</code> action takes one argument, which is the 0-indexed order of the subscription made before.</li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> of length <code>n</code> on which you can apply two types of operations:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose an index <code>i</code> and invert all characters from index <code>0</code> to index <code>i</code> (both inclusive), with a cost of <code>i + 1</code></li>
|
||||
<li>Choose an index <code>i</code> and invert all characters from index <code>i</code> to index <code>n - 1</code> (both inclusive), with a cost of <code>n - i</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum cost </strong>to make all characters of the string <strong>equal</strong></em>.</p>
|
||||
|
||||
<p><strong>Invert</strong> a character means if its value is '0' it becomes '1' and vice-versa.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0011"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Apply the second operation with <code>i = 2</code> to obtain <code>s = "0000" for a cost of 2</code>. It can be shown that 2 is the minimum cost to make all characters equal.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "010101"
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Apply the first operation with i = 2 to obtain s = "101101" for a cost of 3.
|
||||
Apply the first operation with i = 1 to obtain s = "011101" for a cost of 2.
|
||||
Apply the first operation with i = 0 to obtain s = "111101" for a cost of 1.
|
||||
Apply the second operation with i = 4 to obtain s = "111110" for a cost of 2.
|
||||
Apply the second operation with i = 5 to obtain s = "111111" for a cost of 1.
|
||||
The total cost to make all characters equal is 9. It can be shown that 9 is the minimum cost to make all characters equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length == n <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>Enhance all functions to have the <code>callPolyfill</code> method. The method accepts an object <code>obj</code> as it's first parameter and any number of additional arguments. The <code>obj</code> becomes the <code>this</code> context for the function. The additional arguments are passed to the function (that the <code>callPolyfill</code> method belongs on).</p>
|
||||
|
||||
<p>For example if you had the function:</p>
|
||||
|
||||
<pre>
|
||||
function tax(price, taxRate) {
|
||||
const totalCost = price * (1 + taxRate);
|
||||
console.log(`The cost of ${this.item} is ${totalCost}`);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>Calling this function like <code>tax(10, 0.1)</code> will log <code>"The cost of undefined is 11"</code>. This is because the <code>this</code> context was not defined.</p>
|
||||
|
||||
<p>However, calling the function like <code>tax.callPolyfill({item: "salad"}, 10, 0.1)</code> will log <code>"The cost of salad is 11"</code>. The <code>this</code> context was appropriately set, and the function logged an appropriate output.</p>
|
||||
|
||||
<p>Please solve this without using the built-in <code>Function.call</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = function add(b) {
|
||||
return this.a + b;
|
||||
}
|
||||
args = [{"a": 5}, 7]
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong>
|
||||
fn.callPolyfill({"a": 5}, 7); // 12
|
||||
callPolyfill sets the "this" context to {"a": 5}. 7 is passed as an argument.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = function tax(price, taxRate) {
|
||||
return `The cost of the ${this.item} is ${price * taxRate}`;
|
||||
}
|
||||
args = [{"item": "burger"}, 10, 1,1]
|
||||
<strong>Output:</strong> "The cost of the burger is 11"
|
||||
<strong>Explanation:</strong> callPolyfill sets the "this" context to {"item": "burger"}. 10 and 1.1 are passed as additional arguments.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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,56 @@
|
||||
<p>You are given an <strong>undirected weighted</strong> <strong>connected</strong> graph containing <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, and an integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>, w<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> with weight <code>w<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Some edges have a weight of <code>-1</code> (<code>w<sub>i</sub> = -1</code>), while others have a <strong>positive</strong> weight (<code>w<sub>i</sub> > 0</code>).</p>
|
||||
|
||||
<p>Your task is to modify <strong>all edges</strong> with a weight of <code>-1</code> by assigning them <strong>positive integer values </strong>in the range <code>[1, 2 * 10<sup>9</sup>]</code> so that the <strong>shortest distance</strong> between the nodes <code>source</code> and <code>destination</code> becomes equal to an integer <code>target</code>. If there are <strong>multiple</strong> <strong>modifications</strong> that make the shortest distance between <code>source</code> and <code>destination</code> equal to <code>target</code>, any of them will be considered correct.</p>
|
||||
|
||||
<p>Return <em>an array containing all edges (even unmodified ones) in any order if it is possible to make the shortest distance from </em><code>source</code><em> to </em><code>destination</code><em> equal to </em><code>target</code><em>, or an <strong>empty array</strong> if it's impossible.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> You are not allowed to modify the weights of edges with initial positive weights.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/18/graph.png" style="width: 300px; height: 300px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 5, edges = [[4,1,-1],[2,0,-1],[0,3,-1],[4,3,-1]], source = 0, destination = 1, target = 5
|
||||
<strong>Output:</strong> [[4,1,1],[2,0,1],[0,3,3],[4,3,1]]
|
||||
<strong>Explanation:</strong> The graph above shows a possible modification to the edges, making the distance from 0 to 1 equal to 5.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/18/graph-2.png" style="width: 300px; height: 300px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, edges = [[0,1,-1],[0,2,5]], source = 0, destination = 2, target = 6
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> The graph above contains the initial edges. It is not possible to make the distance from 0 to 2 equal to 6 by modifying the edge with weight -1. So, an empty array is returned.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/19/graph-3.png" style="width: 300px; height: 300px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, edges = [[1,0,4],[1,2,3],[2,3,5],[0,3,-1]], source = 0, destination = 2, target = 6
|
||||
<strong>Output:</strong> [[1,0,4],[1,2,3],[2,3,5],[0,3,1]]
|
||||
<strong>Explanation:</strong> The graph above shows a modified graph having the shortest distance from 0 to 2 as 6.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code><font face="monospace">1 <= edges.length <= n * (n - 1) / 2</font></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><font face="monospace">w<sub>i</sub> = -1 </font></code>or <code><font face="monospace">1 <= w<sub>i </sub><= 10<sup><span style="font-size: 10.8333px;">7</span></sup></font></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><font face="monospace">1 <= target <= 10<sup>9</sup></font></code></li>
|
||||
<li>The graph is connected, and there are no self-loops or repeated edges</li>
|
||||
</ul>
|
@ -0,0 +1,36 @@
|
||||
<p>You are given a string <code>s</code> consisting only of <strong>uppercase</strong> English letters.</p>
|
||||
|
||||
<p>You can apply some operations to this string where, in one operation, you can remove <strong>any</strong> occurrence of one of the substrings <code>"AB"</code> or <code>"CD"</code> from <code>s</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> possible length of the resulting string that you can obtain</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the string concatenates after removing the substring and could produce new <code>"AB"</code> or <code>"CD"</code> substrings.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "ABFCACDB"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can do the following operations:
|
||||
- Remove the substring "<u>AB</u>FCACDB", so s = "FCACDB".
|
||||
- Remove the substring "FCA<u>CD</u>B", so s = "FCAB".
|
||||
- Remove the substring "FC<u>AB</u>", so s = "FC".
|
||||
So the resulting length of the string is 2.
|
||||
It can be shown that it is the minimum length that we can obtain.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "ACBBD"
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> We cannot do any operations on the string so the length remains the same.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists only of uppercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>Create a class <code>ArrayWrapper</code> that accepts an array of integers in it's constructor. This class should have two features:</p>
|
||||
|
||||
<ul>
|
||||
<li>When two instances of this class are added together with the <code>+</code> operator, the resulting value is the sum of all the elements in both arrays.</li>
|
||||
<li>When the <code>String()</code> function is called on the instance, it will return a comma separated string surrounded by brackets. For example, <code>[1,2,3]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,2],[3,4]], operation = "Add"
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong>
|
||||
const obj1 = new ArrayWrapper([1,2]);
|
||||
const obj2 = new ArrayWrapper([3,4]);
|
||||
obj1 + obj2; // 10
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[23,98,42,70]], operation = "String"
|
||||
<strong>Output:</strong> "[23,98,42,70]"
|
||||
<strong>Explanation:</strong>
|
||||
const obj = new ArrayWrapper([23,98,42,70]);
|
||||
String(obj); // "[23,98,42,70]"
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[],[]], operation = "Add"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
const obj1 = new ArrayWrapper([]);
|
||||
const obj2 = new ArrayWrapper([]);
|
||||
obj1 + obj2; // 0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
<li><code>Note: nums is the array passed to the constructor</code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given a string <code node="[object Object]">s</code> consisting of <strong>lowercase English letters</strong>, and you are allowed to perform operations on it. In one operation, you can <strong>replace</strong> a character in <code node="[object Object]">s</code> with another lowercase English letter.</p>
|
||||
|
||||
<p>Your task is to make <code node="[object Object]">s</code> a <strong>palindrome</strong> with the <strong>minimum</strong> <strong>number</strong> <strong>of operations</strong> possible. If there are <strong>multiple palindromes</strong> that can be <meta charset="utf-8" />made using the <strong>minimum</strong> number of operations, <meta charset="utf-8" />make the <strong>lexicographically smallest</strong> one.</p>
|
||||
|
||||
<p>A string <code>a</code> is lexicographically smaller than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, string <code>a</code> has a letter that appears earlier in the alphabet than the corresponding letter in <code>b</code>.</p>
|
||||
|
||||
<p>Return <em>the resulting palindrome string.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "egcfe"
|
||||
<strong>Output:</strong> "efcfe"
|
||||
<strong>Explanation:</strong> The minimum number of operations to make "egcfe" a palindrome is 1, and the lexicographically smallest palindrome string we can get by modifying one character is "efcfe", by changing 'g'.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcd"
|
||||
<strong>Output:</strong> "abba"
|
||||
<strong>Explanation:</strong> The minimum number of operations to make "abcd" a palindrome is 2, and the lexicographically smallest palindrome string we can get by modifying two characters is "abba".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "seven"
|
||||
<strong>Output:</strong> "neven"
|
||||
<strong>Explanation:</strong> The minimum number of operations to make "seven" a palindrome is 1, and the lexicographically smallest palindrome string we can get by modifying one character is "neven".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s</code> consists of only lowercase English letters<b>.</b></li>
|
||||
</ul>
|
@ -0,0 +1,32 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>s</code> and a dictionary of words <code>dictionary</code>. You have to break <code>s</code> into one or more <strong>non-overlapping</strong> substrings such that each substring is present in <code>dictionary</code>. There may be some <strong>extra characters</strong> in <code>s</code> which are not present in any of the substrings.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of extra characters left over if you break up </em><code>s</code><em> optimally.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "leetscode", dictionary = ["leet","code","leetcode"]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can break s in two substrings: "leet" from index 0 to 3 and "code" from index 5 to 8. There is only 1 unused character (at index 4), so we return 1.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "sayhelloworld", dictionary = ["hello","world"]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can break s in two substrings: "hello" from index 3 to 7 and "world" from index 8 to 12. The characters at indices 0, 1, 2 are not used in any substring and thus are considered as extra characters. Hence, we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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> and <code>s</code> consists of only lowercase English letters</li>
|
||||
<li><code>dictionary</code> contains distinct words</li>
|
||||
</ul>
|
@ -0,0 +1,50 @@
|
||||
<p>Given a <strong>0-indexed</strong> 2D <code>grid</code> of size <code>m x n</code>, you should find the matrix <code>answer</code> of size <code>m x n</code>.</p>
|
||||
|
||||
<p>The value of each cell <code>(r, c)</code> of the matrix <code>answer</code> is calculated in the following way:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>topLeft[r][c]</code> be the number of <strong>distinct</strong> values in the top-left diagonal of the cell <code>(r, c)</code> in the matrix <code>grid</code>.</li>
|
||||
<li>Let <code>bottomRight[r][c]</code> be the number of <strong>distinct</strong> values in the bottom-right diagonal of the cell <code>(r, c)</code> in the matrix <code>grid</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Then <code>answer[r][c] = |topLeft[r][c] - bottomRight[r][c]|</code>.</p>
|
||||
|
||||
<p>Return <em>the matrix</em> <code>answer</code>.</p>
|
||||
|
||||
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end.</p>
|
||||
|
||||
<p>A cell <code>(r<sub>1</sub>, c<sub>1</sub>)</code> belongs to the top-left diagonal of the cell <code>(r, c)</code>, if both belong to the same diagonal and <code>r<sub>1</sub> < r</code>. Similarly is defined bottom-right diagonal.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/04/19/ex2.png" style="width: 786px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>
|
||||
Input:</strong> grid = [[1,2,3],[3,1,5],[3,2,1]]
|
||||
<strong>Output:</strong> [[1,1,0],[1,0,1],[0,1,1]]
|
||||
<strong>Explanation:</strong> The 1<sup>st</sup> diagram denotes the initial grid.
|
||||
The 2<sup>nd</sup> diagram denotes a grid for cell (0,0), where blue-colored cells are cells on its bottom-right diagonal.
|
||||
The 3<sup>rd</sup> diagram denotes a grid for cell (1,2), where red-colored cells are cells on its top-left diagonal.
|
||||
The 4<sup>th</sup> diagram denotes a grid for cell (1,1), where blue-colored cells are cells on its bottom-right diagonal and red-colored cells are cells on its top-left diagonal.
|
||||
- The cell (0,0) contains [1,1] on its bottom-right diagonal and [] on its top-left diagonal. The answer is |1 - 0| = 1.
|
||||
- The cell (1,2) contains [] on its bottom-right diagonal and [2] on its top-left diagonal. The answer is |0 - 1| = 1.
|
||||
- The cell (1,1) contains [1] on its bottom-right diagonal and [1] on its top-left diagonal. The answer is |1 - 1| = 0.
|
||||
The answers of other cells are similarly calculated.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1]]
|
||||
<strong>Output:</strong> [[0]]
|
||||
<strong>Explanation:</strong> - The cell (0,0) contains [] on its bottom-right diagonal and [] on its top-left diagonal. The answer is |0 - 0| = 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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,40 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, and you are allowed to <strong>traverse</strong> between its indices. You can traverse between index <code>i</code> and index <code>j</code>, <code>i != j</code>, if and only if <code>gcd(nums[i], nums[j]) > 1</code>, where <code>gcd</code> is the <strong>greatest common divisor</strong>.</p>
|
||||
|
||||
<p>Your task is to determine if for <strong>every pair</strong> of indices <code>i</code> and <code>j</code> in nums, where <code>i < j</code>, there exists a <strong>sequence of traversals</strong> that can take us from <code>i</code> to <code>j</code>.</p>
|
||||
|
||||
<p>Return <code>true</code><em> if it is possible to traverse between all such pairs of indices,</em><em> or </em><code>false</code><em> otherwise.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,6]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> In this example, there are 3 possible pairs of indices: (0, 1), (0, 2), and (1, 2).
|
||||
To go from index 0 to index 1, we can use the sequence of traversals 0 -> 2 -> 1, where we move from index 0 to index 2 because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1, and then move from index 2 to index 1 because gcd(nums[2], nums[1]) = gcd(6, 3) = 3 > 1.
|
||||
To go from index 0 to index 2, we can just go directly because gcd(nums[0], nums[2]) = gcd(2, 6) = 2 > 1. Likewise, to go from index 1 to index 2, we can just go directly because gcd(nums[1], nums[2]) = gcd(3, 6) = 3 > 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,9,5]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> No sequence of traversals can take us from index 0 to index 2 in this example. So, we return false.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,3,12,8]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> There are 6 possible pairs of indices to traverse between: (0, 1), (0, 2), (0, 3), (1, 2), (1, 3), and (2, 3). A valid sequence of traversals exists for each pair, so we return true.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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,41 @@
|
||||
<p>Given a positive integer <code>n</code>, return <em>the <strong>punishment number</strong></em> of <code>n</code>.</p>
|
||||
|
||||
<p>The <strong>punishment number</strong> of <code>n</code> is defined as the sum of the squares of all integers <code>i</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= i <= n</code></li>
|
||||
<li>The decimal representation of <code>i * i</code> can be partitioned into contiguous substrings such that the sum of the integer values of these substrings equals <code>i</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 10
|
||||
<strong>Output:</strong> 182
|
||||
<strong>Explanation:</strong> There are exactly 3 integers i that satisfy the conditions in the statement:
|
||||
- 1 since 1 * 1 = 1
|
||||
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
|
||||
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
|
||||
Hence, the punishment number of 10 is 1 + 81 + 100 = 182
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 37
|
||||
<strong>Output:</strong> 1478
|
||||
<strong>Explanation:</strong> There are exactly 4 integers i that satisfy the conditions in the statement:
|
||||
- 1 since 1 * 1 = 1.
|
||||
- 9 since 9 * 9 = 81 and 81 can be partitioned into 8 + 1.
|
||||
- 10 since 10 * 10 = 100 and 100 can be partitioned into 10 + 0.
|
||||
- 36 since 36 * 36 = 1296 and 1296 can be partitioned into 1 + 29 + 6.
|
||||
Hence, the punishment number of 37 is 1 + 81 + 100 + 1296 = 1478
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,31 @@
|
||||
<p>Write a function <code>expect</code> that helps developers test their code. It should take in any value <code>val</code> and return an object with the following two functions.</p>
|
||||
|
||||
<ul>
|
||||
<li><code>toBe(val)</code> accepts another value and returns <code>true</code> if the two values <code>===</code> each other. If they are not equal, it should throw an error <code>"Not Equal"</code>.</li>
|
||||
<li><code>notToBe(val)</code> accepts another value and returns <code>true</code> if the two values <code>!==</code> each other. If they are equal, it should throw an error <code>"Equal"</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => expect(5).toBe(5)
|
||||
<strong>Output:</strong> {"value": true}
|
||||
<strong>Explanation:</strong> 5 === 5 so this expression returns true.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => expect(5).toBe(null)
|
||||
<strong>Output:</strong> {"error": "Not Equal"}
|
||||
<strong>Explanation:</strong> 5 !== null so this expression throw the error "Not Equal".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> func = () => expect(5).notToBe(null)
|
||||
<strong>Output:</strong> {"value": true}
|
||||
<strong>Explanation:</strong> 5 !== null so this expression returns true.
|
||||
</pre>
|
@ -0,0 +1,49 @@
|
||||
<p>Given a <strong>1-indexed</strong> <code>m x n</code> integer matrix <code>mat</code>, you can select any cell in the matrix as your <strong>starting cell</strong>.</p>
|
||||
|
||||
<p>From the starting cell, you can move to any other cell <strong>in the</strong> <strong>same row or column</strong>, but only if the value of the destination cell is <strong>strictly greater</strong> than the value of the current cell. You can repeat this process as many times as possible, moving from cell to cell until you can no longer make any moves.</p>
|
||||
|
||||
<p>Your task is to find the <strong>maximum number of cells</strong> that you can visit in the matrix by starting from some cell.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the maximum number of cells that can be visited.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/23/diag1drawio.png" style="width: 200px; height: 176px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[3,1],[3,4]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The image shows how we can visit 2 cells starting from row 1, column 2. It can be shown that we cannot visit more than 2 cells no matter where we start from, so the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/23/diag3drawio.png" style="width: 200px; height: 176px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[1,1],[1,1]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Since the cells must be strictly increasing, we can only visit one cell in this example.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2023/04/23/diag4drawio.png" style="width: 350px; height: 250px;" /></strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[3,1,6],[-9,5,7]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The image above shows how we can visit 4 cells starting from row 2, column 1. It can be shown that we cannot visit more than 4 cells no matter where we start from, so the answer is 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>Given a <strong>positive</strong> integer <code>num</code> represented as a string, return <em>the integer </em><code>num</code><em> without trailing zeros as a string</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "51230100"
|
||||
<strong>Output:</strong> "512301"
|
||||
<strong>Explanation:</strong> Integer "51230100" has 2 trailing zeros, we remove them and return integer "512301".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = "123"
|
||||
<strong>Output:</strong> "123"
|
||||
<strong>Explanation:</strong> Integer "123" has no trailing zeros, we return integer "123".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num.length <= 1000</code></li>
|
||||
<li><code>num</code> consists of only digits.</li>
|
||||
<li><code>num</code> doesn't have any leading zeros.</li>
|
||||
</ul>
|
@ -0,0 +1,35 @@
|
||||
<p>Given an object or array <code>obj</code>, return a <strong>compact object</strong>. A <strong>compact object</strong> is the same as the original object, except with keys containing <strong>falsy</strong> values removed. This operation applies to the object and any nested objects. Arrays are considered objects where the indices are keys. A value is considered <strong>falsy</strong> when <code>Boolean(value)</code> returns <code>false</code>.</p>
|
||||
|
||||
<p>You may assume the <code>obj</code> is the output of <code>JSON.parse</code>. In other words, it is valid JSON.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> obj = [null, 0, false, 1]
|
||||
<strong>Output:</strong> [1]
|
||||
<strong>Explanation:</strong> All falsy values have been removed from the array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> obj = {"a": null, "b": [false, 1]}
|
||||
<strong>Output:</strong> {"b": [1]}
|
||||
<strong>Explanation:</strong> obj["a"] and obj["b"][0] had falsy values and were removed.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> obj = [null, 0, 5, [0], [false, 16]]
|
||||
<strong>Output:</strong> [5, [], [16]]
|
||||
<strong>Explanation:</strong> obj[0], obj[1], obj[3][0], and obj[4][0] were falsy and removed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>obj is a valid JSON object</code></li>
|
||||
<li><code>2 <= JSON.stringify(obj).length <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,31 @@
|
||||
<p>You are given an integer array <code>prices</code> representing the prices of various chocolates in a store. You are also given a single integer <code>money</code>, which represents your initial amount of money.</p>
|
||||
|
||||
<p>You must buy <strong>exactly</strong> two chocolates in such a way that you still have some <strong>non-negative</strong> leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.</p>
|
||||
|
||||
<p>Return <em>the amount of money you will have leftover after buying the two chocolates</em>. If there is no way for you to buy two chocolates without ending up in debt, return <code>money</code>. Note that the leftover must be non-negative.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prices = [1,2,2], money = 3
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prices = [3,2,3], money = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> You cannot buy 2 chocolates without going in debt, so we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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,31 @@
|
||||
Write a function <code>argumentsLength</code> that returns the count of arguments passed to it.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> argsArr = [5]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
argumentsLength(5); // 1
|
||||
|
||||
One value was passed to the function so it should return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> argsArr = [{}, null, "3"]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
argumentsLength({}, null, "3"); // 3
|
||||
|
||||
Three values were passed to the function so it should return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>argsArr is a valid JSON array</code></li>
|
||||
<li><code>0 <= argsArr.length <= 100</code></li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,66 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2853",
|
||||
"questionFrontendId": "2701",
|
||||
"boundTopicId": null,
|
||||
"title": "Consecutive Transactions with Increasing Amounts",
|
||||
"titleSlug": "consecutive-transactions-with-increasing-amounts",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Hard",
|
||||
"likes": 7,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": null,
|
||||
"categoryTitle": "Database",
|
||||
"contributors": [],
|
||||
"topicTags": [
|
||||
{
|
||||
"name": "Database",
|
||||
"slug": "database",
|
||||
"translatedName": null,
|
||||
"__typename": "TopicTagNode"
|
||||
}
|
||||
],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"104\", \"totalSubmission\": \"560\", \"totalAcceptedRaw\": 104, \"totalSubmissionRaw\": 560, \"acRate\": \"18.6%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"headers\":{\"Transactions\":[\"transaction_id\",\"customer_id\",\"transaction_date\",\"amount\"]},\"rows\":{\"Transactions\":[[1,101,\"2023-05-01\",100],[2,101,\"2023-05-02\",150],[3,101,\"2023-05-03\",200],[4,102,\"2023-05-01\",50],[5,102,\"2023-05-03\",100],[6,102,\"2023-05-04\",200],[7,105,\"2023-05-01\",100],[8,105,\"2023-05-02\",150],[9,105,\"2023-05-03\",200],[10,105,\"2023-05-04\",300],[11,105,\"2023-05-12\",250],[12,105,\"2023-05-13\",260],[13,105,\"2023-05-14\",270]]}}",
|
||||
"metaData": "{\n \"mysql\": [\n \"Create table If Not Exists Transactions (transaction_id int, customer_id int, transaction_date date, amount int)\"\n ],\n \"mssql\": [\n \"Create table Transactions (transaction_id int, customer_id int, transaction_date date, amount int)\"\n ],\n \"oraclesql\": [\n \"Create table Transactions (transaction_id int, customer_id int, transaction_date date, amount int)\",\n \"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"\n ],\n \"database\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [
|
||||
"Create table If Not Exists Transactions (transaction_id int, customer_id int, transaction_date date, amount int)",
|
||||
"Truncate table Transactions",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('1', '101', '2023-05-01', '100')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('2', '101', '2023-05-02', '150')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('3', '101', '2023-05-03', '200')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('4', '102', '2023-05-01', '50')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('5', '102', '2023-05-03', '100')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('6', '102', '2023-05-04', '200')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('7', '105', '2023-05-01', '100')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('8', '105', '2023-05-02', '150')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('9', '105', '2023-05-03', '200')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('10', '105', '2023-05-04', '300')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('11', '105', '2023-05-12', '250')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('12', '105', '2023-05-13', '260')",
|
||||
"insert into Transactions (transaction_id, customer_id, transaction_date, amount) values ('13', '105', '2023-05-14', '270')"
|
||||
],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"mysql\": [\"MySQL\", \"<p><code>MySQL 8.0</code>.</p>\"], \"mssql\": [\"MS SQL Server\", \"<p><code>mssql server 2019</code>.</p>\"], \"oraclesql\": [\"Oracle\", \"<p><code>Oracle Sql 11.2</code>.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
48
leetcode/originData/[no content]immutability-helper.json
Normal file
48
leetcode/originData/[no content]immutability-helper.json
Normal file
@ -0,0 +1,48 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2773",
|
||||
"questionFrontendId": "2691",
|
||||
"boundTopicId": null,
|
||||
"title": "Immutability Helper",
|
||||
"titleSlug": "immutability-helper",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Hard",
|
||||
"likes": 3,
|
||||
"dislikes": 3,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Infinite Method Object\", \"titleSlug\": \"infinite-method-object\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Make Object Immutable\", \"titleSlug\": \"make-object-immutable\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "{\"val\":10}\n[proxy => { proxy.val += 1; }, proxy => { proxy.val -= 1; }]\n{\"arr\":[1,2,3]}\n[proxy => { proxy.arr[0] = 5; proxy.newVal = proxy.arr[0] + proxy.arr[1]; }]\n{\"obj\":{\"val\":{\"x\":10,\"y\":20}}}\n[proxy => { let data = proxy.obj.val; let temp = data.x; data.x = data.y; data.y = temp; }]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"69\", \"totalSubmission\": \"96\", \"totalAcceptedRaw\": 69, \"totalSubmissionRaw\": 96, \"acRate\": \"71.9%\"}",
|
||||
"hints": [
|
||||
"Javascript has the concept of Proxy. That concept is critical to this problem.",
|
||||
"You you need to keep track of which values in the JSON were overwritten with new values.",
|
||||
"Somehow, keep a tree structure that exists parallel to the original object. This will keep track of all the edits.",
|
||||
"When the mutator function is done. Return a clone with those mutations applied. It will be inefficient to clone the entire object so only clone the minimum number of nodes."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"val\":10}\n[proxy => { proxy.val += 1; }, proxy => { proxy.val -= 1; }]",
|
||||
"metaData": "{\n \"name\": \"produce\",\n \"params\": [\n {\n \"name\": \"obj\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"mutator\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
47
leetcode/originData/[no content]infinite-method-object.json
Normal file
47
leetcode/originData/[no content]infinite-method-object.json
Normal file
@ -0,0 +1,47 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2772",
|
||||
"questionFrontendId": "2690",
|
||||
"boundTopicId": null,
|
||||
"title": "Infinite Method Object",
|
||||
"titleSlug": "infinite-method-object",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Easy",
|
||||
"likes": 11,
|
||||
"dislikes": 2,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Immutability Helper\", \"titleSlug\": \"immutability-helper\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Make Object Immutable\", \"titleSlug\": \"make-object-immutable\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "\"abc123\"\n\".-qw73n|^2It\"",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"196\", \"totalSubmission\": \"198\", \"totalAcceptedRaw\": 196, \"totalSubmissionRaw\": 198, \"acRate\": \"99.0%\"}",
|
||||
"hints": [
|
||||
"Javascript has the concept of Proxy. That concept is critical to this problem.",
|
||||
"Override all \"get\" for the object. Return a function instead.",
|
||||
"That function should return the \"prop\", i.e. the method name."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "\"abc123\"",
|
||||
"metaData": "{\n \"name\": \"createInfiniteObject\",\n \"params\": [\n {\n \"type\": \"string\",\n \"name\": \"method\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
47
leetcode/originData/[no content]make-object-immutable.json
Normal file
47
leetcode/originData/[no content]make-object-immutable.json
Normal file
@ -0,0 +1,47 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2776",
|
||||
"questionFrontendId": "2692",
|
||||
"boundTopicId": null,
|
||||
"title": "Make Object Immutable",
|
||||
"titleSlug": "make-object-immutable",
|
||||
"content": null,
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": true,
|
||||
"difficulty": "Medium",
|
||||
"likes": 7,
|
||||
"dislikes": 0,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"Infinite Method Object\", \"titleSlug\": \"infinite-method-object\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Immutability Helper\", \"titleSlug\": \"immutability-helper\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "{\"x\":5}\n(obj) => { obj.x = 5; return obj.x; }\n[1,2,3]\n(arr) => { arr[1] = {}; return arr[2]; }\n{\"arr\":[1,2,3]}\n(obj) => { obj.arr.push(4); return 42; }\n{\"x\":2,\"y\":2}\n(obj) => { return Object.keys(obj); }",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": null,
|
||||
"stats": "{\"totalAccepted\": \"91\", \"totalSubmission\": \"144\", \"totalAcceptedRaw\": 91, \"totalSubmissionRaw\": 144, \"acRate\": \"63.2%\"}",
|
||||
"hints": [
|
||||
"Javascript has the concept of Proxy. That concept is critical to this problem.",
|
||||
"Recursively use proxy so that the user of the object is only able to access a proxy object.",
|
||||
"Override how set works. It should throw the correct error instead of actually setting a value."
|
||||
],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "{\"x\":5}\n(obj) => { obj.x = 5; return obj.x; }",
|
||||
"metaData": "{\n \"name\": \"makeImmutable\",\n \"params\": [\n {\n \"name\": \"obj\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
63
leetcode/originData/array-wrapper.json
Normal file
63
leetcode/originData/array-wrapper.json
Normal file
@ -0,0 +1,63 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2805",
|
||||
"questionFrontendId": "2695",
|
||||
"boundTopicId": null,
|
||||
"title": "Array Wrapper",
|
||||
"titleSlug": "array-wrapper",
|
||||
"content": "<p>Create a class <code>ArrayWrapper</code> that accepts an array of integers in it's constructor. This class should have two features:</p>\n\n<ul>\n\t<li>When two instances of this class are added together with the <code>+</code> operator, the resulting value is the sum of all the elements in both arrays.</li>\n\t<li>When the <code>String()</code> function is called on the instance, it will return a comma separated string surrounded by brackets. For example, <code>[1,2,3]</code>.</li>\n</ul>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [[1,2],[3,4]], operation = "Add"\n<strong>Output:</strong> 10\n<strong>Explanation:</strong>\nconst obj1 = new ArrayWrapper([1,2]);\nconst obj2 = new ArrayWrapper([3,4]);\nobj1 + obj2; // 10\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [[23,98,42,70]], operation = "String"\n<strong>Output:</strong> "[23,98,42,70]"\n<strong>Explanation:</strong>\nconst obj = new ArrayWrapper([23,98,42,70]);\nString(obj); // "[23,98,42,70]"\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [[],[]], operation = "Add"\n<strong>Output:</strong> 0\n<strong>Explanation:</strong>\nconst obj1 = new ArrayWrapper([]);\nconst obj2 = new ArrayWrapper([]);\nobj1 + obj2; // 0\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 <= nums.length <= 1000</code></li>\n\t<li><code>0 <= nums[i] <= 1000</code></li>\n\t<li><code>Note: nums is the array passed to the constructor</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 79,
|
||||
"dislikes": 9,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "[[1,2],[3,4]]\n\"Add\"\n[[23,98,42,70]]\n\"String\"\n[[],[]]\n\"Add\"",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {number[]} nums\n */\nvar ArrayWrapper = function(nums) {\n \n};\n\nArrayWrapper.prototype.valueOf = function() {\n \n}\n\nArrayWrapper.prototype.toString = function() {\n \n}\n\n/**\n * const obj1 = new ArrayWrapper([1,2]);\n * const obj2 = new ArrayWrapper([3,4]);\n * obj1 + obj2; // 10\n * String(obj1); // \"[1,2]\"\n * String(obj2); // \"[3,4]\"\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "class ArrayWrapper {\n\tconstructor(nums: number[]) {\n \n }\n\n\tvalueOf() {\n \n }\n\n\ttoString() {\n \n }\n};\n\n/**\n * const obj1 = new ArrayWrapper([1,2]);\n * const obj2 = new ArrayWrapper([3,4]);\n * obj1 + obj2; // 10\n * String(obj1); // \"[1,2]\"\n * String(obj2); // \"[3,4]\"\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"4.7K\", \"totalSubmission\": \"5.1K\", \"totalAcceptedRaw\": 4672, \"totalSubmissionRaw\": 5145, \"acRate\": \"90.8%\"}",
|
||||
"hints": [],
|
||||
"solution": {
|
||||
"id": "1926",
|
||||
"canSeeDetail": true,
|
||||
"paidOnly": false,
|
||||
"hasVideoSolution": false,
|
||||
"paidOnlyVideo": true,
|
||||
"__typename": "ArticleNode"
|
||||
},
|
||||
"status": null,
|
||||
"sampleTestCase": "[[1,2],[3,4]]\n\"Add\"",
|
||||
"metaData": "{\n \"name\": \"ArrayWrapper\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[][]\"\n },\n {\n \"type\": \"string\",\n \"name\": \"operation\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
173
leetcode/originData/buy-two-chocolates.json
Normal file
173
leetcode/originData/buy-two-chocolates.json
Normal file
File diff suppressed because one or more lines are too long
63
leetcode/originData/call-function-with-custom-context.json
Normal file
63
leetcode/originData/call-function-with-custom-context.json
Normal file
@ -0,0 +1,63 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2790",
|
||||
"questionFrontendId": "2693",
|
||||
"boundTopicId": null,
|
||||
"title": "Call Function with Custom Context",
|
||||
"titleSlug": "call-function-with-custom-context",
|
||||
"content": "<p>Enhance all functions to have the <code>callPolyfill</code> method. The method accepts an object <code>obj</code> as it's first parameter and any number of additional arguments. The <code>obj</code> becomes the <code>this</code> context for the function. The additional arguments are passed to the function (that the <code>callPolyfill</code> method belongs on).</p>\n\n<p>For example if you had the function:</p>\n\n<pre>\nfunction tax(price, taxRate) {\n const totalCost = price * (1 + taxRate);\n console.log(`The cost of ${this.item} is ${totalCost}`);\n}\n</pre>\n\n<p>Calling this function like <code>tax(10, 0.1)</code> will log <code>"The cost of undefined is 11"</code>. This is because the <code>this</code> context was not defined.</p>\n\n<p>However, calling the function like <code>tax.callPolyfill({item: "salad"}, 10, 0.1)</code> will log <code>"The cost of salad is 11"</code>. The <code>this</code> context was appropriately set, and the function logged an appropriate output.</p>\n\n<p>Please solve this without using the built-in <code>Function.call</code> method.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong>\nfn = function add(b) {\n return this.a + b;\n}\nargs = [{"a": 5}, 7]\n<strong>Output:</strong> 12\n<strong>Explanation:</strong>\nfn.callPolyfill({"a": 5}, 7); // 12\ncallPolyfill sets the "this" context to {"a": 5}. 7 is passed as an argument.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nfn = function tax(price, taxRate) { \n return `The cost of the ${this.item} is ${price * taxRate}`; \n}\nargs = [{"item": "burger"}, 10, 1,1]\n<strong>Output:</strong> "The cost of the burger is 11"\n<strong>Explanation:</strong> callPolyfill sets the "this" context to {"item": "burger"}. 10 and 1.1 are passed as additional arguments.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul style=\"list-style-type:square;\">\n\t<li><code><font face=\"monospace\">typeof args[0] == 'object' and args[0] != null</font></code></li>\n\t<li><code>1 <= args.length <= 100</code></li>\n\t<li><code>2 <= JSON.stringify(args[0]).length <= 10<sup>5</sup></code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 87,
|
||||
"dislikes": 9,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "function add(b) { return this.a + b; }\n[{\"a\":5},7]\nfunction tax(price, taxRate) { return `The cost of the ${this.item} is ${price * taxRate}`; }\n[{\"item\":\"burger\"},10,1.1]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Object} context\n * @param {any[]} args\n * @return {any}\n */\nFunction.prototype.callPolyfill = function(context, ...args) {\n\n}\n\n/**\n * function increment() { this.count++; return this.count; }\n * increment.callPolyfill({count: 1}); // 2\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "declare global { \n interface Function {\n callPolyfill(context: Record<any, any>, ...args: any[]): any;\n\t}\n}\n\nFunction.prototype.callPolyfill = function(context, ...args): any {\n\n}\n\n/**\n * function increment() { this.count++; return this.count; }\n * increment.callPolyfill({count: 1}); // 2\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"4.7K\", \"totalSubmission\": \"5.4K\", \"totalAcceptedRaw\": 4676, \"totalSubmissionRaw\": 5396, \"acRate\": \"86.7%\"}",
|
||||
"hints": [],
|
||||
"solution": {
|
||||
"id": "1924",
|
||||
"canSeeDetail": true,
|
||||
"paidOnly": false,
|
||||
"hasVideoSolution": false,
|
||||
"paidOnlyVideo": true,
|
||||
"__typename": "ArticleNode"
|
||||
},
|
||||
"status": null,
|
||||
"sampleTestCase": "function add(b) { return this.a + b; }\n[{\"a\":5},7]",
|
||||
"metaData": "{\n \"name\": \"callPolyfill\",\n \"params\": [\n {\n \"type\": \"string\",\n \"name\": \"context\"\n },\n {\n \"type\": \"string\",\n \"name\": \"args\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
56
leetcode/originData/compact-object.json
Normal file
56
leetcode/originData/compact-object.json
Normal file
@ -0,0 +1,56 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2804",
|
||||
"questionFrontendId": "2705",
|
||||
"boundTopicId": null,
|
||||
"title": "Compact Object",
|
||||
"titleSlug": "compact-object",
|
||||
"content": "<p>Given an object or array <code>obj</code>, return a <strong>compact object</strong>. A <strong>compact object</strong> is the same as the original object, except with keys containing <strong>falsy</strong> values removed. This operation applies to the object and any nested objects. Arrays are considered objects where the indices are keys. A value is considered <strong>falsy</strong> when <code>Boolean(value)</code> returns <code>false</code>.</p>\n\n<p>You may assume the <code>obj</code> is the output of <code>JSON.parse</code>. In other words, it is valid JSON.</p>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> obj = [null, 0, false, 1]\n<strong>Output:</strong> [1]\n<strong>Explanation:</strong> All falsy values have been removed from the array.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> obj = {"a": null, "b": [false, 1]}\n<strong>Output:</strong> {"b": [1]}\n<strong>Explanation:</strong> obj["a"] and obj["b"][0] had falsy values and were removed.</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> obj = [null, 0, 5, [0], [false, 16]]\n<strong>Output:</strong> [5, [], [16]]\n<strong>Explanation:</strong> obj[0], obj[1], obj[3][0], and obj[4][0] were falsy and removed.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>obj is a valid JSON object</code></li>\n\t<li><code>2 <= JSON.stringify(obj).length <= 10<sup>6</sup></code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 16,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "[null, 0, false, 1]\n{\"a\": null, \"b\": [false, 1]}\n[null, 0, 5, [0], [false, 16]]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {Object} obj\n * @return {Object}\n */\nvar compactObject = function(obj) {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type Obj = Record<any, any>;\n\nfunction compactObject(obj: Obj): Obj {\n\n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"442\", \"totalSubmission\": \"603\", \"totalAcceptedRaw\": 442, \"totalSubmissionRaw\": 603, \"acRate\": \"73.3%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[null, 0, false, 1]",
|
||||
"metaData": "{\n \"name\": \"compactObject\",\n \"params\": [\n {\n \"name\": \"obj\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
67
leetcode/originData/differences-between-two-objects.json
Normal file
67
leetcode/originData/differences-between-two-objects.json
Normal file
@ -0,0 +1,67 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2774",
|
||||
"questionFrontendId": "2700",
|
||||
"boundTopicId": null,
|
||||
"title": "Differences Between Two Objects",
|
||||
"titleSlug": "differences-between-two-objects",
|
||||
"content": "<p>Write a function that accepts two deeply nested objects or arrays <code>obj1</code> and <code>obj2</code> and returns a new object representing their differences.</p>\n\n<p>The function should compare the properties of the two objects and identify any changes. The returned object should only contains keys where the value is different from <code>obj1</code> to <code>obj2</code>. For each changed key, the value should be represented as an array <code>[obj1 value, obj2 value]</code>. Keys that exist in one object but not in the other should not be included in the returned object. When comparing two arrays, the indices of the arrays are considered to be their keys. The end result should be a deeply nested object where each leaf value is a difference array.</p>\n\n<p>You may assume that both objects are the output of <code>JSON.parse</code>.</p>\n\n<p> </p>\n<p><strong>Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nobj1 = {}\nobj2 = {\n "a": 1, \n "b": 2\n}\n<strong>Output:</strong> {}\n<strong>Explanation:</strong> There were no modifications made to obj1. New keys "a" and "b" appear in obj2, but keys that are added or removed should be ignored.\n</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nobj1 = {\n "a": 1,\n "v": 3,\n "x": [],\n "z": {\n "a": null\n }\n}\nobj2 = {\n "a": 2,\n "v": 4,\n "x": [],\n "z": {\n "a": 2\n }\n}\n<strong>Output:</strong> \n{\n "a": [1, 2],\n "v": [3, 4],\n "z": {\n "a": [null, 2]\n }\n}\n<strong>Explanation:</strong> The keys "a", "v", and "z" all had changes applied. "a" was chnaged from 1 to 2. "v" was changed from 3 to 4. "z" had a change applied to a child object. "z.a" was changed from null to 2.\n</pre>\n\n<p><strong>Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nobj1 = {\n "a": 5, \n "v": 6, \n "z": [1, 2, 4, [2, 5, 7]]\n}\nobj2 = {\n "a": 5, \n "v": 7, \n "z": [1, 2, 3, [1]]\n}\n<strong>Output:</strong> \n{\n "v": [6, 7],\n "z": {\n "2": [4, 3],\n "3": {\n "0": [2, 1]\n }\n }\n}\n<strong>Explanation:</strong> In obj1 and obj2, the keys "v" and "z" have different assigned values. "a" is ignored because the value is unchanged. In the key "z", there is a nested array. Arrays are treated like objects where the indices are keys. There were two alterations to the the array: z[2] and z[3][0]. z[0] and z[1] were unchanged and thus not included. z[3][1] and z[3][2] were removed and thus not included.\n</pre>\n\n<p><strong>Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nobj1 = {\n "a": {"b": 1}, \n}\nobj2 = {\n "a": [5],\n}\n<strong>Output:</strong> \n{\n "a": [{"b": 1}, [5]]\n}\n<strong>Explanation:</strong> The key "a" exists in both objects. Since the two associated values have different types, they are placed in the difference array.</pre>\n\n<p><strong>Example 5:</strong></p>\n\n<pre>\n<strong>Input:</strong> \nobj1 = {\n "a": [1, 2, {}], \n "b": false\n}\nobj2 = { \n "b": false,\n "a": [1, 2, {}]\n}\n<strong>Output:</strong> \n{}\n<strong>Explanation:</strong> Apart from a different ordering of keys, the two objects are identical so an empty object is returned.</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 <= JSON.stringify(obj1).length <= 10<sup>4</sup></code></li>\n\t<li><code>2 <= JSON.stringify(obj2).length <= 10<sup>4</sup></code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Medium",
|
||||
"likes": 110,
|
||||
"dislikes": 17,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[{\"title\": \"JSON Deep Equal\", \"titleSlug\": \"json-deep-equal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Convert Object to JSON String\", \"titleSlug\": \"convert-object-to-json-string\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
|
||||
"exampleTestcases": "{}\n{\"a\": 1, \"b\": 2}\n{\"a\": 1, \"v\": 3, \"x\": [], \"z\": {\"a\": null}}\n{\"a\": 2, \"v\": 4, \"x\": [], \"z\": {\"a\": 2}}\n{\"a\": 5, \"v\": 6, \"z\": [1,2,4, [2,5,7]]}\n{\"a\": 5, \"v\": 7, \"z\": [1,2,3, [1]]}\n{\"a\":{\"b\":1}}\n{\"a\":[5]}\n{\"a\": [1, 2, {}],\"b\": false}\n{\"b\": false, \"a\": [1, 2, {}]}",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {object} obj1\n * @param {object} obj2\n * @return {object}\n */\nfunction objDiff(obj1, obj2) {\n \n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "function objDiff(obj1: any, obj2: any): any {\n\n};",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"4.6K\", \"totalSubmission\": \"5.5K\", \"totalAcceptedRaw\": 4567, \"totalSubmissionRaw\": 5469, \"acRate\": \"83.5%\"}",
|
||||
"hints": [
|
||||
"Find the intersection of the keys/indices on the two arrays/objects.",
|
||||
"Analyze the data structure recursively.",
|
||||
"For each key in the intersection, omit if there are no differences in the leaves. Otherwise return the difference."
|
||||
],
|
||||
"solution": {
|
||||
"id": "1914",
|
||||
"canSeeDetail": true,
|
||||
"paidOnly": false,
|
||||
"hasVideoSolution": false,
|
||||
"paidOnlyVideo": true,
|
||||
"__typename": "ArticleNode"
|
||||
},
|
||||
"status": null,
|
||||
"sampleTestCase": "{}\n{\"a\": 1, \"b\": 2}",
|
||||
"metaData": "{\n \"name\": \"objDiff\",\n \"params\": [\n {\n \"name\": \"obj1\",\n \"type\": \"string\"\n },\n {\n \"type\": \"string\",\n \"name\": \"obj2\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
63
leetcode/originData/event-emitter.json
Normal file
63
leetcode/originData/event-emitter.json
Normal file
File diff suppressed because one or more lines are too long
192
leetcode/originData/extra-characters-in-a-string.json
Normal file
192
leetcode/originData/extra-characters-in-a-string.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
188
leetcode/originData/greatest-common-divisor-traversal.json
Normal file
188
leetcode/originData/greatest-common-divisor-traversal.json
Normal file
File diff suppressed because one or more lines are too long
174
leetcode/originData/lexicographically-smallest-palindrome.json
Normal file
174
leetcode/originData/lexicographically-smallest-palindrome.json
Normal file
File diff suppressed because one or more lines are too long
187
leetcode/originData/maximum-strength-of-a-group.json
Normal file
187
leetcode/originData/maximum-strength-of-a-group.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
183
leetcode/originData/modify-graph-edge-weights.json
Normal file
183
leetcode/originData/modify-graph-edge-weights.json
Normal file
File diff suppressed because one or more lines are too long
167
leetcode/originData/remove-trailing-zeros-from-a-string.json
Normal file
167
leetcode/originData/remove-trailing-zeros-from-a-string.json
Normal file
File diff suppressed because one or more lines are too long
56
leetcode/originData/return-length-of-arguments-passed.json
Normal file
56
leetcode/originData/return-length-of-arguments-passed.json
Normal file
@ -0,0 +1,56 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2820",
|
||||
"questionFrontendId": "2703",
|
||||
"boundTopicId": null,
|
||||
"title": "Return Length of Arguments Passed",
|
||||
"titleSlug": "return-length-of-arguments-passed",
|
||||
"content": "Write a function <code>argumentsLength</code> that returns the count of arguments passed to it.\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> argsArr = [5]\n<strong>Output:</strong> 1\n<strong>Explanation:</strong>\nargumentsLength(5); // 1\n\nOne value was passed to the function so it should return 1.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> argsArr = [{}, null, "3"]\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> \nargumentsLength({}, null, "3"); // 3\n\nThree values were passed to the function so it should return 3.\n</pre>\n\n<p> </p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>argsArr is a valid JSON array</code></li>\n\t<li><code>0 <= argsArr.length <= 100</code></li>\n</ul>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 23,
|
||||
"dislikes": 8,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "[5]\n[{},null,\"3\"]",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @return {number}\n */\nvar argumentsLength = function(...args) {\n \n};\n\n/**\n * argumentsLength(1, 2, 3); // 3\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "function argumentsLength(...args: any[]): number {\n\n};\n\n/**\n * argumentsLength(1, 2, 3); // 3\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"2.6K\", \"totalSubmission\": \"2.7K\", \"totalAcceptedRaw\": 2551, \"totalSubmissionRaw\": 2723, \"acRate\": \"93.7%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "[5]",
|
||||
"metaData": "{\n \"name\": \"argumentsLength\",\n \"params\": [\n {\n \"type\": \"string\",\n \"name\": \"argsArr\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ],\n \"manual\": true\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
56
leetcode/originData/to-be-or-not-to-be.json
Normal file
56
leetcode/originData/to-be-or-not-to-be.json
Normal file
@ -0,0 +1,56 @@
|
||||
{
|
||||
"data": {
|
||||
"question": {
|
||||
"questionId": "2813",
|
||||
"questionFrontendId": "2704",
|
||||
"boundTopicId": null,
|
||||
"title": "To Be Or Not To Be",
|
||||
"titleSlug": "to-be-or-not-to-be",
|
||||
"content": "<p>Write a function <code>expect</code> that helps developers test their code. It should take in any value <code>val</code> and return an object with the following two functions.</p>\n\n<ul>\n\t<li><code>toBe(val)</code> accepts another value and returns <code>true</code> if the two values <code>===</code> each other. If they are not equal, it should throw an error <code>"Not Equal"</code>.</li>\n\t<li><code>notToBe(val)</code> accepts another value and returns <code>true</code> if the two values <code>!==</code> each other. If they are equal, it should throw an error <code>"Equal"</code>.</li>\n</ul>\n\n<p> </p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => expect(5).toBe(5)\n<strong>Output:</strong> {"value": true}\n<strong>Explanation:</strong> 5 === 5 so this expression returns true.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => expect(5).toBe(null)\n<strong>Output:</strong> {"error": "Not Equal"}\n<strong>Explanation:</strong> 5 !== null so this expression throw the error "Not Equal".\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> func = () => expect(5).notToBe(null)\n<strong>Output:</strong> {"value": true}\n<strong>Explanation:</strong> 5 !== null so this expression returns true.\n</pre>\n",
|
||||
"translatedTitle": null,
|
||||
"translatedContent": null,
|
||||
"isPaidOnly": false,
|
||||
"difficulty": "Easy",
|
||||
"likes": 24,
|
||||
"dislikes": 1,
|
||||
"isLiked": null,
|
||||
"similarQuestions": "[]",
|
||||
"exampleTestcases": "() => expect(5).toBe(5)\n() => expect(5).toBe(null)\n() => expect(5).notToBe(null)",
|
||||
"categoryTitle": "JavaScript",
|
||||
"contributors": [],
|
||||
"topicTags": [],
|
||||
"companyTagStats": null,
|
||||
"codeSnippets": [
|
||||
{
|
||||
"lang": "JavaScript",
|
||||
"langSlug": "javascript",
|
||||
"code": "/**\n * @param {string} val\n * @return {Object}\n */\nvar expect = function(val) {\n \n};\n\n/**\n * expect(5).toBe(5); // true\n * expect(5).notToBe(5); // throws \"Equal\"\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
},
|
||||
{
|
||||
"lang": "TypeScript",
|
||||
"langSlug": "typescript",
|
||||
"code": "type ToBeOrNotToBe = {\n toBe: (val: any) => boolean;\n notToBe: (val: any) => boolean;\n};\n\nfunction expect(val: any): ToBeOrNotToBe {\n\n};\n\n/**\n * expect(5).toBe(5); // true\n * expect(5).notToBe(5); // throws \"Equal\"\n */",
|
||||
"__typename": "CodeSnippetNode"
|
||||
}
|
||||
],
|
||||
"stats": "{\"totalAccepted\": \"1.3K\", \"totalSubmission\": \"1.8K\", \"totalAcceptedRaw\": 1262, \"totalSubmissionRaw\": 1823, \"acRate\": \"69.2%\"}",
|
||||
"hints": [],
|
||||
"solution": null,
|
||||
"status": null,
|
||||
"sampleTestCase": "() => expect(5).toBe(5)",
|
||||
"metaData": "{\n \"name\": \"expect\",\n \"params\": [\n {\n \"name\": \"func\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}",
|
||||
"judgerAvailable": true,
|
||||
"judgeType": "large",
|
||||
"mysqlSchemas": [],
|
||||
"enableRunCode": true,
|
||||
"enableTestMode": false,
|
||||
"enableDebugger": false,
|
||||
"envInfo": "{\"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://github.com/datastructures-js/queue/tree/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\">datastructures-js/queue</a>.</p>\"], \"typescript\": [\"Typescript\", \"<p><code>TypeScript 4.5.4, Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES2020 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\"]}",
|
||||
"libraryUrl": null,
|
||||
"adminUrl": null,
|
||||
"challengeQuestion": null,
|
||||
"__typename": "QuestionNode"
|
||||
}
|
||||
}
|
||||
}
|
48
leetcode/problem/array-wrapper.html
Normal file
48
leetcode/problem/array-wrapper.html
Normal file
@ -0,0 +1,48 @@
|
||||
<p>Create a class <code>ArrayWrapper</code> that accepts an array of integers in it's constructor. This class should have two features:</p>
|
||||
|
||||
<ul>
|
||||
<li>When two instances of this class are added together with the <code>+</code> operator, the resulting value is the sum of all the elements in both arrays.</li>
|
||||
<li>When the <code>String()</code> function is called on the instance, it will return a comma separated string surrounded by brackets. For example, <code>[1,2,3]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,2],[3,4]], operation = "Add"
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong>
|
||||
const obj1 = new ArrayWrapper([1,2]);
|
||||
const obj2 = new ArrayWrapper([3,4]);
|
||||
obj1 + obj2; // 10
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[23,98,42,70]], operation = "String"
|
||||
<strong>Output:</strong> "[23,98,42,70]"
|
||||
<strong>Explanation:</strong>
|
||||
const obj = new ArrayWrapper([23,98,42,70]);
|
||||
String(obj); // "[23,98,42,70]"
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[],[]], operation = "Add"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
const obj1 = new ArrayWrapper([]);
|
||||
const obj2 = new ArrayWrapper([]);
|
||||
obj1 + obj2; // 0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
<li><code>Note: nums is the array passed to the constructor</code></li>
|
||||
</ul>
|
31
leetcode/problem/buy-two-chocolates.html
Normal file
31
leetcode/problem/buy-two-chocolates.html
Normal file
@ -0,0 +1,31 @@
|
||||
<p>You are given an integer array <code>prices</code> representing the prices of various chocolates in a store. You are also given a single integer <code>money</code>, which represents your initial amount of money.</p>
|
||||
|
||||
<p>You must buy <strong>exactly</strong> two chocolates in such a way that you still have some <strong>non-negative</strong> leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.</p>
|
||||
|
||||
<p>Return <em>the amount of money you will have leftover after buying the two chocolates</em>. If there is no way for you to buy two chocolates without ending up in debt, return <code>money</code>. Note that the leftover must be non-negative.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prices = [1,2,2], money = 3
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prices = [3,2,3], money = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> You cannot buy 2 chocolates without going in debt, so we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>
|
52
leetcode/problem/call-function-with-custom-context.html
Normal file
52
leetcode/problem/call-function-with-custom-context.html
Normal file
@ -0,0 +1,52 @@
|
||||
<p>Enhance all functions to have the <code>callPolyfill</code> method. The method accepts an object <code>obj</code> as it's first parameter and any number of additional arguments. The <code>obj</code> becomes the <code>this</code> context for the function. The additional arguments are passed to the function (that the <code>callPolyfill</code> method belongs on).</p>
|
||||
|
||||
<p>For example if you had the function:</p>
|
||||
|
||||
<pre>
|
||||
function tax(price, taxRate) {
|
||||
const totalCost = price * (1 + taxRate);
|
||||
console.log(`The cost of ${this.item} is ${totalCost}`);
|
||||
}
|
||||
</pre>
|
||||
|
||||
<p>Calling this function like <code>tax(10, 0.1)</code> will log <code>"The cost of undefined is 11"</code>. This is because the <code>this</code> context was not defined.</p>
|
||||
|
||||
<p>However, calling the function like <code>tax.callPolyfill({item: "salad"}, 10, 0.1)</code> will log <code>"The cost of salad is 11"</code>. The <code>this</code> context was appropriately set, and the function logged an appropriate output.</p>
|
||||
|
||||
<p>Please solve this without using the built-in <code>Function.call</code> method.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = function add(b) {
|
||||
return this.a + b;
|
||||
}
|
||||
args = [{"a": 5}, 7]
|
||||
<strong>Output:</strong> 12
|
||||
<strong>Explanation:</strong>
|
||||
fn.callPolyfill({"a": 5}, 7); // 12
|
||||
callPolyfill sets the "this" context to {"a": 5}. 7 is passed as an argument.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
fn = function tax(price, taxRate) {
|
||||
return `The cost of the ${this.item} is ${price * taxRate}`;
|
||||
}
|
||||
args = [{"item": "burger"}, 10, 1,1]
|
||||
<strong>Output:</strong> "The cost of the burger is 11"
|
||||
<strong>Explanation:</strong> callPolyfill sets the "this" context to {"item": "burger"}. 10 and 1.1 are passed as additional arguments.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>
|
35
leetcode/problem/compact-object.html
Normal file
35
leetcode/problem/compact-object.html
Normal file
@ -0,0 +1,35 @@
|
||||
<p>Given an object or array <code>obj</code>, return a <strong>compact object</strong>. A <strong>compact object</strong> is the same as the original object, except with keys containing <strong>falsy</strong> values removed. This operation applies to the object and any nested objects. Arrays are considered objects where the indices are keys. A value is considered <strong>falsy</strong> when <code>Boolean(value)</code> returns <code>false</code>.</p>
|
||||
|
||||
<p>You may assume the <code>obj</code> is the output of <code>JSON.parse</code>. In other words, it is valid JSON.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> obj = [null, 0, false, 1]
|
||||
<strong>Output:</strong> [1]
|
||||
<strong>Explanation:</strong> All falsy values have been removed from the array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> obj = {"a": null, "b": [false, 1]}
|
||||
<strong>Output:</strong> {"b": [1]}
|
||||
<strong>Explanation:</strong> obj["a"] and obj["b"][0] had falsy values and were removed.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> obj = [null, 0, 5, [0], [false, 16]]
|
||||
<strong>Output:</strong> [5, [], [16]]
|
||||
<strong>Explanation:</strong> obj[0], obj[1], obj[3][0], and obj[4][0] were falsy and removed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>obj is a valid JSON object</code></li>
|
||||
<li><code>2 <= JSON.stringify(obj).length <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,50 @@
|
||||
<p>Given a <strong>0-indexed</strong> 2D <code>grid</code> of size <code>m x n</code>, you should find the matrix <code>answer</code> of size <code>m x n</code>.</p>
|
||||
|
||||
<p>The value of each cell <code>(r, c)</code> of the matrix <code>answer</code> is calculated in the following way:</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>topLeft[r][c]</code> be the number of <strong>distinct</strong> values in the top-left diagonal of the cell <code>(r, c)</code> in the matrix <code>grid</code>.</li>
|
||||
<li>Let <code>bottomRight[r][c]</code> be the number of <strong>distinct</strong> values in the bottom-right diagonal of the cell <code>(r, c)</code> in the matrix <code>grid</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Then <code>answer[r][c] = |topLeft[r][c] - bottomRight[r][c]|</code>.</p>
|
||||
|
||||
<p>Return <em>the matrix</em> <code>answer</code>.</p>
|
||||
|
||||
<p>A <strong>matrix diagonal</strong> is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end.</p>
|
||||
|
||||
<p>A cell <code>(r<sub>1</sub>, c<sub>1</sub>)</code> belongs to the top-left diagonal of the cell <code>(r, c)</code>, if both belong to the same diagonal and <code>r<sub>1</sub> < r</code>. Similarly is defined bottom-right diagonal.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/04/19/ex2.png" style="width: 786px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>
|
||||
Input:</strong> grid = [[1,2,3],[3,1,5],[3,2,1]]
|
||||
<strong>Output:</strong> [[1,1,0],[1,0,1],[0,1,1]]
|
||||
<strong>Explanation:</strong> The 1<sup>st</sup> diagram denotes the initial grid.
|
||||
The 2<sup>nd</sup> diagram denotes a grid for cell (0,0), where blue-colored cells are cells on its bottom-right diagonal.
|
||||
The 3<sup>rd</sup> diagram denotes a grid for cell (1,2), where red-colored cells are cells on its top-left diagonal.
|
||||
The 4<sup>th</sup> diagram denotes a grid for cell (1,1), where blue-colored cells are cells on its bottom-right diagonal and red-colored cells are cells on its top-left diagonal.
|
||||
- The cell (0,0) contains [1,1] on its bottom-right diagonal and [] on its top-left diagonal. The answer is |1 - 0| = 1.
|
||||
- The cell (1,2) contains [] on its bottom-right diagonal and [2] on its top-left diagonal. The answer is |0 - 1| = 1.
|
||||
- The cell (1,1) contains [1] on its bottom-right diagonal and [1] on its top-left diagonal. The answer is |1 - 1| = 0.
|
||||
The answers of other cells are similarly calculated.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1]]
|
||||
<strong>Output:</strong> [[0]]
|
||||
<strong>Explanation:</strong> - The cell (0,0) contains [] on its bottom-right diagonal and [] on its top-left diagonal. The answer is |0 - 0| = 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>
|
117
leetcode/problem/differences-between-two-objects.html
Normal file
117
leetcode/problem/differences-between-two-objects.html
Normal file
@ -0,0 +1,117 @@
|
||||
<p>Write a function that accepts two deeply nested objects or arrays <code>obj1</code> and <code>obj2</code> and returns a new object representing their differences.</p>
|
||||
|
||||
<p>The function should compare the properties of the two objects and identify any changes. The returned object should only contains keys where the value is different from <code>obj1</code> to <code>obj2</code>. For each changed key, the value should be represented as an array <code>[obj1 value, obj2 value]</code>. Keys that exist in one object but not in the other should not be included in the returned object. When comparing two arrays, the indices of the arrays are considered to be their keys. The end result should be a deeply nested object where each leaf value is a difference array.</p>
|
||||
|
||||
<p>You may assume that both objects are the output of <code>JSON.parse</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
obj1 = {}
|
||||
obj2 = {
|
||||
"a": 1,
|
||||
"b": 2
|
||||
}
|
||||
<strong>Output:</strong> {}
|
||||
<strong>Explanation:</strong> There were no modifications made to obj1. New keys "a" and "b" appear in obj2, but keys that are added or removed should be ignored.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
obj1 = {
|
||||
"a": 1,
|
||||
"v": 3,
|
||||
"x": [],
|
||||
"z": {
|
||||
"a": null
|
||||
}
|
||||
}
|
||||
obj2 = {
|
||||
"a": 2,
|
||||
"v": 4,
|
||||
"x": [],
|
||||
"z": {
|
||||
"a": 2
|
||||
}
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{
|
||||
"a": [1, 2],
|
||||
"v": [3, 4],
|
||||
"z": {
|
||||
"a": [null, 2]
|
||||
}
|
||||
}
|
||||
<strong>Explanation:</strong> The keys "a", "v", and "z" all had changes applied. "a" was chnaged from 1 to 2. "v" was changed from 3 to 4. "z" had a change applied to a child object. "z.a" was changed from null to 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
obj1 = {
|
||||
"a": 5,
|
||||
"v": 6,
|
||||
"z": [1, 2, 4, [2, 5, 7]]
|
||||
}
|
||||
obj2 = {
|
||||
"a": 5,
|
||||
"v": 7,
|
||||
"z": [1, 2, 3, [1]]
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{
|
||||
"v": [6, 7],
|
||||
"z": {
|
||||
"2": [4, 3],
|
||||
"3": {
|
||||
"0": [2, 1]
|
||||
}
|
||||
}
|
||||
}
|
||||
<strong>Explanation:</strong> In obj1 and obj2, the keys "v" and "z" have different assigned values. "a" is ignored because the value is unchanged. In the key "z", there is a nested array. Arrays are treated like objects where the indices are keys. There were two alterations to the the array: z[2] and z[3][0]. z[0] and z[1] were unchanged and thus not included. z[3][1] and z[3][2] were removed and thus not included.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
obj1 = {
|
||||
"a": {"b": 1},
|
||||
}
|
||||
obj2 = {
|
||||
"a": [5],
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{
|
||||
"a": [{"b": 1}, [5]]
|
||||
}
|
||||
<strong>Explanation:</strong> The key "a" exists in both objects. Since the two associated values have different types, they are placed in the difference array.</pre>
|
||||
|
||||
<p><strong>Example 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
obj1 = {
|
||||
"a": [1, 2, {}],
|
||||
"b": false
|
||||
}
|
||||
obj2 = {
|
||||
"b": false,
|
||||
"a": [1, 2, {}]
|
||||
}
|
||||
<strong>Output:</strong>
|
||||
{}
|
||||
<strong>Explanation:</strong> Apart from a different ordering of keys, the two objects are identical so an empty object is returned.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</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>
|
64
leetcode/problem/event-emitter.html
Normal file
64
leetcode/problem/event-emitter.html
Normal file
@ -0,0 +1,64 @@
|
||||
<p>Design an <code>EventEmitter</code> class. This interface is similar (but with some differences) to the one found in Node.js or the Event Target interface of the DOM. The <code>EventEmitter</code> should allow for subscribing to events and emitting them.</p>
|
||||
|
||||
<p>Your <code>EventEmitter</code> class should have the following two methods:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>subscribe</strong> - This method takes in two arguments: the name of an event as a string and a callback function. This callback function will later be called when the event is emitted.<br />
|
||||
An event should be able to have multiple listeners for the same event. When emitting an event with multiple callbacks, each should be called in the order in which they were subscribed. An array of results should be returned. You can assume no callbacks passed to <code>subscribe</code> are referentially identical.<br />
|
||||
The <code>subscribe</code> method should also return an object with an <code>unsubscribe</code> method that enables the user to unsubscribe. When it is called, the callback should be removed from the list of subscriptions and <code>undefined</code> should be returned.</li>
|
||||
<li><strong>emit</strong> - This method takes in two arguments: the name of an event as a string and an optional array of arguments that will be passed to the callback(s). If there are no callbacks subscribed to the given event, return an empty array. Otherwise, return an array of the results of all callback calls in the order they were subscribed.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> actions = ["EventEmitter", "emit", "subscribe", "subscribe", "emit"], values = [[], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent"]]
|
||||
<strong>Output:</strong> [[],["emitted",[]],["subscribed"],["subscribed"],["emitted",[5,6]]]
|
||||
<strong>Explanation:</strong>
|
||||
const emitter = new EventEmitter();
|
||||
emitter.emit("firstEvent"); // [], no callback are subscribed yet
|
||||
emitter.subscribe("firstEvent", function cb1() { return 5; });
|
||||
emitter.subscribe("firstEvent", function cb2() { return 6; });
|
||||
emitter.emit("firstEvent"); // [5, 6], returns the output of cb1 and cb2
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> actions = ["EventEmitter", "subscribe", "emit", "emit"], values = [[], ["firstEvent", "function cb1(...args) { return args.join(','); }"], ["firstEvent", [1,2,3]], ["firstEvent", [3,4,6]]]
|
||||
<strong>Output:</strong> [[],["subscribed"],["emitted",["1,2,3"]],["emitted",["3,4,6"]]]
|
||||
<strong>Explanation: </strong>Note that the emit method should be able to accept an OPTIONAL array of arguents.
|
||||
|
||||
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 class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> actions = ["EventEmitter", "subscribe", "emit", "unsubscribe", "emit"], values = [[], ["firstEvent", "(...args) => args.join(',')"], ["firstEvent", [1,2,3]], [0], ["firstEvent", [4,5,6]]]
|
||||
<strong>Output:</strong> [[],["subscribed"],["emitted",["1,2,3"]],["unsubscribed",0],["emitted",[]]]
|
||||
<strong>Explanation:</strong>
|
||||
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]); // [], there are no subscriptions
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= actions.length <= 10</code></li>
|
||||
<li><code>values.length === actions.length</code></li>
|
||||
<li>All test cases are valid, e.g. you don't need to handle scenarios when unsubscribing from a non-existing subscription.</li>
|
||||
<li>There are only 4 different actions: <code>EventEmitter</code>, <code>emit</code>, <code>subscribe</code>, and <code>unsubscribe</code>.</li>
|
||||
<li>The <code>EventEmitter</code> action doesn't take any arguments.</li>
|
||||
<li>The <code>emit</code> action takes between either 1 or 2 arguments. The first argument is the name of the event we want to emit, and the 2nd argument is passed to the callback functions.</li>
|
||||
<li>The <code>subscribe</code> action takes 2 arguments, where the first one is the event name and the second is the callback function.</li>
|
||||
<li>The <code>unsubscribe</code> action takes one argument, which is the 0-indexed order of the subscription made before.</li>
|
||||
</ul>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user