diff --git a/README.md b/README.md index f0ae2d5..9e889a8 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,10 @@ C++标准库提供的数据结构实在是太多了,参考[C++标准库头文 ### 数组/队列/集合/映射 +- [找出叠涂元素](src/array/first_completely_painted_row_or_column.cpp) [数组, 哈希表, 矩阵] + + - LeetCode 2661. 找出叠涂元素 + - [打家劫舍](src/array/house_robber.cpp) [数组, 动态规划] - LeetCode 198. 打家劫舍 diff --git a/images/array/first_completely_painted_row_or_column.jpeg b/images/array/first_completely_painted_row_or_column.jpeg new file mode 100644 index 0000000..5bfdb75 Binary files /dev/null and b/images/array/first_completely_painted_row_or_column.jpeg differ diff --git a/src/array/first_completely_painted_row_or_column.cpp b/src/array/first_completely_painted_row_or_column.cpp new file mode 100644 index 0000000..b059054 --- /dev/null +++ b/src/array/first_completely_painted_row_or_column.cpp @@ -0,0 +1,36 @@ +// 找出叠涂元素 +// https://leetcode.cn/problems/first-completely-painted-row-or-column +// INLINE ../../images/array/first_completely_painted_row_or_column.jpeg + +#include + +class Solution { +public: + int firstCompleteIndex(vector &arr, vector> &mat) { + int n = mat.size(); + int m = mat[0].size(); + unordered_map> mp; + vector rowCnt(n, 0); + vector colCnt(m, 0); + int completeIndex = -1; + for (int i = 0; i < n; ++i) { + for (int j = 0; j < m; ++j) { + mp[mat[i][j]] = {i, j}; + } + } + for (int i = 0; i < arr.size(); ++i) { + auto &v = mp[arr[i]]; + ++rowCnt[v.first]; + if (rowCnt[v.first] == m) { + completeIndex = i; + break; + } + ++colCnt[v.second]; + if (colCnt[v.second] == n) { + completeIndex = i; + break; + } + } + return completeIndex; + } +}; \ No newline at end of file diff --git a/src/lib/list_node.cpp b/src/lib/list_node.cpp index 04bfe4f..790f1c3 100644 --- a/src/lib/list_node.cpp +++ b/src/lib/list_node.cpp @@ -1,16 +1,14 @@ #include // 创建链表,输入数组和数组长度,返回链表头指针 -ListNode *createLinkedList(int arr[], int n) -{ +ListNode *createLinkedList(int arr[], int n) { if (n == 0) return nullptr; ListNode *head = new ListNode(arr[0]); ListNode *curNode = head; - for (int i = 1; i < n; i++) - { + for (int i = 1; i < n; i++) { curNode->next = new ListNode(arr[i]); curNode = curNode->next; } @@ -18,12 +16,10 @@ ListNode *createLinkedList(int arr[], int n) } // 将链表转换为字符串输出 -string listNodeToString(ListNode *head) -{ +string listNodeToString(ListNode *head) { string str = ""; ListNode *curNode = head; - while (curNode != nullptr) - { + while (curNode != nullptr) { str.append(to_string(curNode->val)); str.append(" -> "); curNode = curNode->next; @@ -33,11 +29,9 @@ string listNodeToString(ListNode *head) } // 输出链表 -void printLinkedList(ListNode *head) -{ +void printLinkedList(ListNode *head) { ListNode *curNode = head; - while (curNode != nullptr) - { + while (curNode != nullptr) { cout << curNode->val << " -> "; curNode = curNode->next; } @@ -45,11 +39,9 @@ void printLinkedList(ListNode *head) } // 删除链表 -void deleteLinkedList(ListNode *head) -{ +void deleteLinkedList(ListNode *head) { ListNode *curNode = head; - while (curNode != nullptr) - { + while (curNode != nullptr) { ListNode *delNode = curNode; curNode = curNode->next; delete delNode; diff --git a/test/array/first_completely_painted_row_or_column_test.cpp b/test/array/first_completely_painted_row_or_column_test.cpp new file mode 100644 index 0000000..908fb25 --- /dev/null +++ b/test/array/first_completely_painted_row_or_column_test.cpp @@ -0,0 +1,20 @@ +#include + +TEST(找出叠涂元素, firstCompleteIndex) { + Solution solution; + // 示例 1: + // 输入:arr = [1,3,4,2], mat = [[1,4],[2,3]] + // 输出:2 + // 解释:遍历如上图所示,arr[2] 在矩阵中的第一行或第二列上都被涂色。 + vector arr1 = {1, 3, 4, 2}; + vector> mat1 = {{1, 4}, {2, 3}}; + EXPECT_EQ(solution.firstCompleteIndex(arr1, mat1), 2); + + // 示例 2: + // 输入:arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]] + // 输出:3 + // 解释:遍历如上图所示,arr[3] 在矩阵中的第二列上都被涂色。 + vector arr2 = {2, 8, 7, 4, 1, 3, 5, 6, 9}; + vector> mat2 = {{3, 2, 5}, {1, 4, 6}, {8, 7, 9}}; + EXPECT_EQ(solution.firstCompleteIndex(arr2, mat2), 3); +} diff --git a/test/lib/lib_test.cpp b/test/lib/lib_test.cpp index 47f80f9..08b7709 100644 --- a/test/lib/lib_test.cpp +++ b/test/lib/lib_test.cpp @@ -1,4 +1,4 @@ -// 执行编译时间:2023-09-17 15:52:26 +// 执行编译时间:2023-12-01 16:35:53 #include #include