Skip to content

Commit

Permalink
Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
SomeBottle committed Aug 1, 2023
1 parent 1f972de commit 5490eb4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Algo/code/C-Cpp/DataStructure/408-2010-BF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ int main()
这个源代码是我第一时间能想到的暴力解(Brute-Force)。
主要思路就是把序列分为两半来看待,将两半对调即可,借用一个辅助数组。
这题可能被坑的地方就在于,需要考虑到循环移位次数超过序列长度的情况。
这题可能被坑的地方就在于,需要考虑到循环移位次数超过序列长度的情况。
空间复杂度O(n)级别(需辅助数组),时间复杂度O(n)级别
- SomeBottle 2023.7.30
*/
74 changes: 74 additions & 0 deletions Algo/code/C-Cpp/DataStructure/408-2015-OP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <cstdio>
#include <cstring>

using namespace std;

// 定义单链表数据结构
typedef int ElementType;

typedef struct Node
{
ElementType data;
struct Node *next;
Node(ElementType data)
{
this->data = data;
next = NULL;
}
} *List, LNode;

int main()
{
// input:
int n = 500; // 假如题目给出的数值最大不超过500
int in[] = {21, -15, -15, -7, 15, 500, 17, 28, -17, 90}; // 单链表中的值
int inSize = sizeof(in) / sizeof(in[0]);
// initialization:
List list = new LNode(0); // head node
List tail = list;
for (int i = 0; i < inSize; i++)
{
List node = new LNode(in[i]); // 插入结点
tail->next = node;
tail = node;
}
// algorithm:
bool ht[n + 1]; // 开一个n这么大的数组,可以把数值作为下标来对数组进行访问
memset(ht, 0, sizeof(ht)); // ht置空
// 我这个思路实际上就是哈希表的直接定址法
List ptr = list;
while (ptr->next)
{
// 取个绝对值先
ElementType absData = ptr->next->data < 0 ? -ptr->next->data : ptr->next->data;
if (ht[absData]) // 如果哈希表中已标记此值,说明出现过
{
// 移除该节点
List tmp = ptr->next;
ptr->next = ptr->next->next;
delete tmp;
}
else
{
ht[absData] = true;
}
ptr = ptr->next;
}
// output:
ptr = list->next;
while (ptr)
{
printf("%d -> ", ptr->data);
ptr = ptr->next;
}
return 0;
}

/*
这题很有意思的一点是特意在题干强调了 |data|<=n ,出题人显然不会给出没有用的信息。
实际上,题目并没有限制空间复杂度,而在知道了数据规模n后,我可以直接开一张哈希表来标记每个数值的绝对值是否出现过。
该算法实现的时间复杂度为O(n),空间复杂度也和数据规模同等级,O(n)
- SomeBottle 2023.8.1
*/
3 changes: 2 additions & 1 deletion Algo/code/C-Cpp/DataStructure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
| 平台-题目编号 | 题目 | 我的题解 | 值得玩味 |备注|
|:---:|:---:|:---:|:---:|:---:|
|Dotcpp-1713|[平衡二叉树的基本操作](https://www.dotcpp.com/oj/problem1713.html)|[AVLTree-Insertion.cpp](./AVLTree-Insertion.cpp)|| 主要考察**AVL**(二叉平衡搜索)树的**插入****查找**操作 |
|408-2010统考-42题|[循环左移p位](https://zhuanlan.zhihu.com/p/574160078)|暴力解:[408-2010-BF.cpp](./408-2010-BF.cpp)| | 要考虑到循环左移超过一个序列长度的情况。 |
|408-2010统考-42题|[循环左移p位](https://zhuanlan.zhihu.com/p/574160078)|暴力解:[408-2010-BF.cpp](./408-2010-BF.cpp)| | 要考虑到循环左移超过一个序列长度的情况。 |
|408-2015统考-41题|[单链表去重复绝对值](https://zhuanlan.zhihu.com/p/570729543)|优解:[408-2015-OP.cpp](./408-2015-OP.cpp)| | 注意到题目**特意强调** $$ \left \| data \right \| \le n $$ |

0 comments on commit 5490eb4

Please sign in to comment.