From b48141f7321b15bc19891873063b86287c6a6196 Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Sat, 6 Jun 2020 11:32:07 +0800 Subject: [PATCH] Update --- .../{0000.TwoSum.md => 0000.两数之和.md} | 0 problems/0021.合并两个有序链表.md | 47 +++++++++++++++++++ ...0026.删除排序数组中的重复项.md} | 0 problems/0035.搜索插入位置.md | 1 - problems/0059.螺旋矩阵II.md | 10 ++-- problems/0237.删除链表中的节点.md | 21 +++++++++ .../{0383.RansomNote.md => 0383.赎金信.md} | 14 +++--- 7 files changed, 80 insertions(+), 13 deletions(-) rename problems/{0000.TwoSum.md => 0000.两数之和.md} (100%) create mode 100644 problems/0021.合并两个有序链表.md rename problems/{0026.Remove-Duplicates-from-Sorted-Array.md => 0026.删除排序数组中的重复项.md} (100%) create mode 100644 problems/0237.删除链表中的节点.md rename problems/{0383.RansomNote.md => 0383.赎金信.md} (73%) diff --git a/problems/0000.TwoSum.md b/problems/0000.两数之和.md similarity index 100% rename from problems/0000.TwoSum.md rename to problems/0000.两数之和.md diff --git a/problems/0021.合并两个有序链表.md b/problems/0021.合并两个有序链表.md new file mode 100644 index 00000000..d9b1b367 --- /dev/null +++ b/problems/0021.合并两个有序链表.md @@ -0,0 +1,47 @@ + + +``` +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +// 内存消耗比较大,尝试一下 使用题目中的链表 +class Solution { +public: +// 题目中我们为什么定义了一个 新的head + ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { + ListNode* head = new ListNode(); + ListNode* index = head; + while(l1 != nullptr && l2 != nullptr) { // 注意null 和nullptr的区别 + if (l1->val < l2->val) { + index->next = l1; + l1 = l1->next; + } else { + index->next = l2; + l2 = l2->next; + } + index = index->next; + } + if (l1 != nullptr) { + index->next = l1; + l1 = l1->next; + } + if (l2 != nullptr) { + index->next = l2; + l2 = l2->next; + } + ListNode* tmp = head; // 注意清理内存,不清理也没事 + head = head->next; + delete tmp; + return head; + // return head->next + + } +}; +``` diff --git a/problems/0026.Remove-Duplicates-from-Sorted-Array.md b/problems/0026.删除排序数组中的重复项.md similarity index 100% rename from problems/0026.Remove-Duplicates-from-Sorted-Array.md rename to problems/0026.删除排序数组中的重复项.md diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index 2054620e..5b8f971b 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -1,4 +1,3 @@ - ## 题目地址 https://leetcode-cn.com/problems/search-insert-position/ diff --git a/problems/0059.螺旋矩阵II.md b/problems/0059.螺旋矩阵II.md index 520dfb3f..c5c41d36 100644 --- a/problems/0059.螺旋矩阵II.md +++ b/problems/0059.螺旋矩阵II.md @@ -28,10 +28,10 @@ public: vector> generateMatrix(int n) { vector> res(n, vector(n, 0)); // 使用vector定义一个二维数组 int startx = 0, starty = 0; // 定义每循环一个圈的起始位置 - int loop = n / 2; // 每个圈循环几次 - int mid = n / 2; // 矩阵中间的位置,例如:n为3, 中间的位置就是(1,1),n为5,中间位置为(3, 3) - int count = 1; // 用来计数 - int offset = 1; // 每一圈循环,需要偏移的位置 + int loop = n / 2; // 每个圈循环几次,例如n为奇数3,那么loop = 1 只是循环一圈,矩阵中间的值需要单独处理 + int mid = n / 2; // 矩阵中间的位置,例如:n为3, 中间的位置就是(1,1),n为5,中间位置为(2, 2) + int count = 1; // 用来给矩阵中每一个空格赋值 + int offset = 1; // 每一圈循环,需要控制每一条边遍历的长度 int i,j; while (loop --) { i = startx; @@ -59,7 +59,7 @@ public: startx++; starty++; - // offset 控制每一圈,遍历的长度 + // offset 控制每一圈里每一条边遍历的长度 offset += 2; } diff --git a/problems/0237.删除链表中的节点.md b/problems/0237.删除链表中的节点.md new file mode 100644 index 00000000..c908962d --- /dev/null +++ b/problems/0237.删除链表中的节点.md @@ -0,0 +1,21 @@ + +## 题目地址 +https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ + +## 思路 + +题目中已经说了是非末尾节点,所以不用对末尾节点的情况经行判断 + +**链表中所有节点的值都是唯一的。** 题目题意强调这一点,是为什么,因为 我们删除的node在内存地址 删除的并不是这个。 +## 代码 + +``` +class Solution { +public: + void deleteNode(ListNode* node) { + node->val = node->next->val;// 将前一个值赋给当前node + node->next = node->next->next; // 将当前node的next 指向下下node + } +}; +``` + diff --git a/problems/0383.RansomNote.md b/problems/0383.赎金信.md similarity index 73% rename from problems/0383.RansomNote.md rename to problems/0383.赎金信.md index 25257c17..1a736444 100644 --- a/problems/0383.RansomNote.md +++ b/problems/0383.赎金信.md @@ -21,13 +21,13 @@ public: bool canConstruct(string ransomNote, string magazine) { for (int i = 0; i < magazine.length(); i++) { for (int j = 0; j < ransomNote.length(); j++) { - if (magazine[i] == ransomNote[j]) { - ransomNote.erase(ransomNote.begin() + j); + if (magazine[i] == ransomNote[j]) { // 在ransomNote中找到和magazine相同的字符 + ransomNote.erase(ransomNote.begin() + j); // ransomNote删除这个字符 break; } } } - if (ransomNote.length() == 0) { + if (ransomNote.length() == 0) { // 如果ransomNote为空,则说明magazine的字符可以组成ransomNote return true; } return false; @@ -39,7 +39,7 @@ public: 我们想一想优化解法 -## 优化解法 +## 字典计数法(哈希策略) 因为题目所只有小写字母,那我们可以采用空间换区时间的哈希策略, 用一个长度为26的数组还记录magazine里字母出现的次数,然后再用ransomNote去验证这个数组是否包含了ransomNote所需要的所有字母。 代码如下: @@ -52,11 +52,11 @@ public: bool canConstruct(string ransomNote, string magazine) { int record[26] = {0}; for (int i = 0; i < magazine.length(); i++) { - record[magazine[i]-'a'] ++; + record[magazine[i]-'a'] ++; // 通过recode数据记录 magazine里各个字符出现次数 } for (int j = 0; j < ransomNote.length(); j++) { - record[ransomNote[j]-'a']--; - if(record[ransomNote[j]-'a'] < 0) { + record[ransomNote[j]-'a']--; // 遍历ransomNote,在record里对应的字符个数做--操作 + if(record[ransomNote[j]-'a'] < 0) { // 如果小于零说明 magazine里出现的字符,ransomNote没有 return false; } }