Skip to content

Merge from master #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Language: Cpp
# LS_Cpp03 (in configuration: Cpp03) Use C++03-compatible syntax.
# LS_Cpp11 (in configuration: Cpp11) Use features of C++11 (e.g. A<A<int>> instead of A<A<int> >).
# LS_Auto (in configuration: Auto) Automatic detection based on the input.
Standard: Cpp20
Standard: Cpp23

# Pointer and reference alignment style. Possible values: Left, Right, Middle.
PointerAlignment: Left
Expand Down
2 changes: 2 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CompileFlags:
Add: [-std=c++23]
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,10 @@ members = [
"problems/problems_1432",
"problems/problems_2016",
"problems/problems_3405",
"problems/problems_2966",
"problems/problems_2294",
"problems/problems_3443",
"problems/problems_3085",
]

[package]
Expand Down Expand Up @@ -728,3 +732,7 @@ solution_2566 = { path = "problems/problems_2566", features = ["solution_2566"]
solution_1432 = { path = "problems/problems_1432", features = ["solution_1432"] }
solution_2016 = { path = "problems/problems_2016", features = ["solution_2016"] }
solution_3405 = { path = "problems/problems_3405", features = ["solution_3405"] }
solution_2966 = { path = "problems/problems_2966", features = ["solution_2966"] }
solution_2294 = { path = "problems/problems_2294", features = ["solution_2294"] }
solution_3443 = { path = "problems/problems_3443", features = ["solution_3443"] }
solution_3085 = { path = "problems/problems_3085", features = ["solution_3085"] }
6 changes: 3 additions & 3 deletions cpp/models/ListNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ ListNode *IntArrayToListNode(const std::vector<int> &arr) {
}

std::vector<int> ListNodeToIntArray(ListNode *head) {
auto arr = std::vector<int>();
std::vector<int> arr;
while (head != nullptr) {
arr.push_back(head->val);
head = head->next;
}
return arr;
}

ListNode *IntArrayToListNodeCycle(std::vector<int> &arr, int pos) {
ListNode *IntArrayToListNodeCycle(const std::vector<int> &arr, int pos) {
ListNode dummy = ListNode();
ListNode *p = &dummy;
ListNode *cycle = nullptr;
Expand All @@ -43,7 +43,7 @@ ListNode *IntArrayToListNodeCycle(std::vector<int> &arr, int pos) {
}

std::tuple<ListNode *, ListNode *>
IntArrayToIntersectionListNode(int iv, std::vector<int> &arr1, std::vector<int> &arr2, int idx_a, int idx_b) {
IntArrayToIntersectionListNode(int iv, const std::vector<int> &arr1, const std::vector<int> &arr2, int idx_a, int idx_b) {
auto headA = IntArrayToListNode(arr1);
if (iv == 0 || idx_a == static_cast<int>(arr1.size()) || idx_b == static_cast<int>(arr2.size())) {
return {headA, IntArrayToListNode(arr2)};
Expand Down
4 changes: 2 additions & 2 deletions cpp/models/TreeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ vector<TreeNode *> JsonArrayToTreeNodeWithTargets(json arr, vector<int> targets)
if (arr.empty()) {
return {targets.size() + 1, nullptr};
}
vector<TreeNode *> ans = vector<TreeNode *>(targets.size() + 1);
vector<TreeNode *> ans{targets.size() + 1};
auto root = new TreeNode(arr[0]);
int isLeft = true;
std::queue<TreeNode *> q;
Expand Down Expand Up @@ -79,7 +79,7 @@ std::vector<TreeNode*> JsonArrayToTreeNodeArray(json arr) {
if (arr.empty()) {
return {};
}
auto ans = std::vector<TreeNode*>(arr.size(), nullptr);
std::vector<TreeNode*> ans{arr.size(), nullptr};
for (auto i = 0; i < static_cast<int>(arr.size()); i++) {
ans[i] = JsonArrayToTreeNode(arr[i]);
}
Expand Down
2 changes: 1 addition & 1 deletion daily-problems.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"daily": "813",
"daily": "1662",
"plans": ["3582", "problems", "3583", "problems", "3584", "problems", "3585", "problems"]
}
4 changes: 2 additions & 2 deletions golang/solution_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package golang

import (
problem "leetCode/problems/problems_813"
problem "leetCode/problems/problems_1662"
"testing"
)

func TestSolution(t *testing.T) {
TestEach(t, "813", "problems", problem.Solve)
TestEach(t, "1662", "problems", problem.Solve)
}
9 changes: 5 additions & 4 deletions problems/problems_1/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//go:build ignore
#include "cpp/common/Solution.h"
#include <map>
#include <unordered_map>


using namespace std;
Expand All @@ -9,10 +9,11 @@ using json = nlohmann::json;
class Solution {
public:
vector<int> twoSum(vector<int> &nums, int target) {
map<int, int> m;
unordered_map<int, int> m;
for (int i = 0; i < nums.size(); i++) {
if (m.find(target - nums[i]) != m.end()) {
return {m[target - nums[i]], i};
auto it = m.find(target - nums[i]);
if (it != m.end()) {
return {it->second, i};
}
m[nums[i]] = i;
}
Expand Down
2 changes: 2 additions & 0 deletions problems/problems_15/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//go:build ignore
#include "cpp/common/Solution.h"

#include <algorithm>


using namespace std;
using json = nlohmann::json;
Expand Down
59 changes: 59 additions & 0 deletions problems/problems_1662/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// go:build ignore
#include "cpp/common/Solution.h"

using namespace std;
using json = nlohmann::json;

template <typename ForwardIters1, typename ForwardIters2,
typename Compare = std::equal_to<> // 默认使用标准相等比较
>
bool areEqual(ForwardIters1 begin1, ForwardIters1 end1, ForwardIters2 begin2,
ForwardIters2 end2, Compare comp = Compare()) {
auto it1 = begin1, it2 = begin2;
auto inner_it1 = it1->begin(), inner_it2 = it2->begin();
while (it1 != end1 && it2 != end2) {
while (inner_it1 != it1->end() && inner_it2 != it2->end()) {
if (!comp(*inner_it1, *inner_it2)) {
return false;
}
++inner_it1;
++inner_it2;
}
if (inner_it1 == (*it1).end()) {
++it1;
if (it1 != end1) {
inner_it1 = it1->begin();
}
}
if (inner_it2 == (*it2).end()) {
++it2;
if (it2 != end2) {
inner_it2 = it2->begin();
}
}
}
return it1 == end1 && it2 == end2;
}

class Solution {
public:
bool arrayStringsAreEqual(vector<string> &word1, vector<string> &word2) {
return areEqual(word1.begin(), word1.end(), word2.begin(), word2.end());
}
};

json leetcode::qubh::Solve(string input_json_values) {
vector<string> inputArray;
size_t pos = input_json_values.find('\n');
while (pos != string::npos) {
inputArray.push_back(input_json_values.substr(0, pos));
input_json_values = input_json_values.substr(pos + 1);
pos = input_json_values.find('\n');
}
inputArray.push_back(input_json_values);

Solution solution;
vector<string> word1 = json::parse(inputArray.at(0));
vector<string> word2 = json::parse(inputArray.at(1));
return solution.arrayStringsAreEqual(word1, word2);
}
59 changes: 29 additions & 30 deletions problems/problems_1662/problem.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
# 1662. Check If Two String Arrays are Equivalent [Rating: 1217.12]

Given two string arrays `word1` and `word2`, return `true` *if the two arrays **represent** the same string, and* `false` *otherwise.*
<p>Given two string arrays <code>word1</code> and <code>word2</code>, return<em> </em><code>true</code><em> if the two arrays <strong>represent</strong> the same string, and </em><code>false</code><em> otherwise.</em></p>

A string is **represented** by an array if the array elements concatenated **in order** forms the string.
<p>A string is <strong>represented</strong> by an array if the array elements concatenated <strong>in order</strong> forms the string.</p>


<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

**Example 1:**
<pre>
<strong>Input:</strong> word1 = [&quot;ab&quot;, &quot;c&quot;], word2 = [&quot;a&quot;, &quot;bc&quot;]
<strong>Output:</strong> true
<strong>Explanation:</strong>
word1 represents string &quot;ab&quot; + &quot;c&quot; -&gt; &quot;abc&quot;
word2 represents string &quot;a&quot; + &quot;bc&quot; -&gt; &quot;abc&quot;
The strings are the same, so return true.</pre>

```
Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
Output: true
Explanation:
word1 represents string "ab" + "c" -> "abc"
word2 represents string "a" + "bc" -> "abc"
The strings are the same, so return true.
```
<p><strong class="example">Example 2:</strong></p>

**Example 2:**
<pre>
<strong>Input:</strong> word1 = [&quot;a&quot;, &quot;cb&quot;], word2 = [&quot;ab&quot;, &quot;c&quot;]
<strong>Output:</strong> false
</pre>

```
Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
Output: false
```
<p><strong class="example">Example 3:</strong></p>

**Example 3:**
<pre>
<strong>Input:</strong> word1 = [&quot;abc&quot;, &quot;d&quot;, &quot;defg&quot;], word2 = [&quot;abcddefg&quot;]
<strong>Output:</strong> true
</pre>

```
Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"]
Output: true
```
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>



**Constraints:**

- `1 <= word1.length, word2.length <= 103`
- `1 <= word1[i].length, word2[i].length <= 103`
- `1 <= sum(word1[i].length), sum(word2[i].length) <= 103`
- `word1[i]` and `word2[i]` consist of lowercase letters.
<ul>
<li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>3</sup></code></li>
<li><code>1 &lt;= word1[i].length, word2[i].length &lt;= 10<sup>3</sup></code></li>
<li><code>1 &lt;= sum(word1[i].length), sum(word2[i].length) &lt;= 10<sup>3</sup></code></li>
<li><code>word1[i]</code> and <code>word2[i]</code> consist of lowercase letters.</li>
</ul>
42 changes: 42 additions & 0 deletions problems/problems_1662/problem_zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 1662. 检查两个字符串数组是否相等 [难度分: 1217.12]

<p>给你两个字符串数组 <code>word1</code> 和 <code>word2</code> 。如果两个数组表示的字符串相同,返回<em> </em><code>true</code><em> </em>;否则,返回 <code>false</code><em> 。</em></p>

<p><strong>数组表示的字符串</strong> 是由数组中的所有元素 <strong>按顺序</strong> 连接形成的字符串。</p>

<p> </p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>word1 = ["ab", "c"], word2 = ["a", "bc"]
<strong>输出:</strong>true
<strong>解释:</strong>
word1 表示的字符串为 "ab" + "c" -> "abc"
word2 表示的字符串为 "a" + "bc" -> "abc"
两个字符串相同,返回 true</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong>word1 = ["a", "cb"], word2 = ["ab", "c"]
<strong>输出:</strong>false
</pre>

<p><strong>示例 3:</strong></p>

<pre>
<strong>输入:</strong>word1 = ["abc", "d", "defg"], word2 = ["abcddefg"]
<strong>输出:</strong>true
</pre>

<p> </p>

<p><strong>提示:</strong></p>

<ul>
<li><code>1 <= word1.length, word2.length <= 10<sup>3</sup></code></li>
<li><code>1 <= word1[i].length, word2[i].length <= 10<sup>3</sup></code></li>
<li><code>1 <= sum(word1[i].length), sum(word2[i].length) <= 10<sup>3</sup></code></li>
<li><code>word1[i]</code> 和 <code>word2[i]</code> 由小写字母组成</li>
</ul>
46 changes: 46 additions & 0 deletions problems/problems_1662/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package problem1662

import (
"encoding/json"
"log"
"strings"
)

func arrayStringsAreEqual(word1 []string, word2 []string) bool {
m, n := len(word1), len(word2)
idx1, idx2 := 0, 0
i1, i2 := 0, 0
for idx1 < m && idx2 < n {
for i1 < len(word1[idx1]) && i2 < len(word2[idx2]) {
if word1[idx1][i1] != word2[idx2][i2] {
return false
}
i1++
i2++
}
if i1 == len(word1[idx1]) {
idx1++
i1 = 0
}
if i2 == len(word2[idx2]) {
idx2++
i2 = 0
}
}
return idx1 == m && idx2 == n
}

func Solve(inputJsonValues string) any {
inputValues := strings.Split(inputJsonValues, "\n")
var word1 []string
var word2 []string

if err := json.Unmarshal([]byte(inputValues[0]), &word1); err != nil {
log.Fatal(err)
}
if err := json.Unmarshal([]byte(inputValues[1]), &word2); err != nil {
log.Fatal(err)
}

return arrayStringsAreEqual(word1, word2)
}
2 changes: 2 additions & 0 deletions problems/problems_1662/testcase
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
["[\"ab\", \"c\"]\n[\"a\", \"bc\"]", "[\"a\", \"cb\"]\n[\"ab\", \"c\"]", "[\"abc\", \"d\", \"defg\"]\n[\"abcddefg\"]"]
[true, false, true]
Loading