data-structures 相关问题

数据结构是以允许有效地查询和/或更新该数据的特定属性的方式组织数据的方式。

我的代码有什么问题检查是否平衡高度

任何人都可以修复我的代码吗 类解决方案{ 布尔值 c = true; 公共布尔 isBalanced(TreeNode root) { int diff = 助手(根, 0); System.out.println(diff); ...

回答 1 投票 0

Python 字典:将排序数据帧列映射到字典时出现问题

有人可以帮我理解为什么Python字典保留原始数据顺序而不是采用排序后的数据。 例如,这里是一个数据帧(df): 身份证日期 0 AB001 12-03-20...

回答 1 投票 0

最长有效括号

给定一个仅包含字符“(”和“)”的字符串,找到最长的有效(格式良好)括号子字符串的长度。 我已经实现了下面的代码。 公共 int 最长有效括号(

回答 2 投票 0

使用 lru_cache 斐波那契命中数

我正在使用 functools.lru_cache 通过记忆实现斐波那契。但我不明白我的输出产生的点击次数。我的代码如下: `@lru_cache(最大大小=8) def fib(n...

回答 1 投票 0

基数树与哈希表

我想比较哈希表和基数树。对于固定长度的键,当然,考虑到我们不关心排序顺序,哈希表总是最好的选择,但对于可变长度的 k...

回答 1 投票 0

如果哈希函数不是常数,哈希表查找时间会令人困惑

在哈希表中,我们一般说插入/查找时间为O(1)。 我读过,只有当使用的散列函数具有恒定时间时,这才是正确的,并且据说恒定时间取决于...

回答 1 投票 0

不同情况下数组与二分查找

我一直在研究二叉搜索树与数组,很好奇我的假设是否正确。所以我会解释一下我的理解,如果你这么认为,请纠正我。我们还假设 w...

回答 1 投票 0

使用递归组合求和

我是数据结构新手,我正在尝试这个问题组合和 我知道这可能是非常基本的东西,但我正在学习递归,不想了解我做错的地方,这样我就可以

回答 1 投票 0

C 语言字数统计

所以它是c程序中排除特殊字符和数字的字数统计 int main(){ 字符str[100]; 整数 i,j = 0; int 长度; 字符选择; 做 { printf("输入圣...

回答 1 投票 0

实现两种数据结构以迭代不同值的最有效方法是什么

我正在尝试实现一个数据结构来解析数据并进行数据分析 我的数据格式如下 日期时间数据 数据 数据 1999年3月31日 20:40 6 130 19.95 1999年3月31日 23:50 3 440 1...

回答 1 投票 0

如何在Python中高效地对大型数据集进行排序?

我正在开发一个项目,需要在Python中有效地对大型数据集进行排序。该数据集包含数百万条记录,我目前正在使用内置的sorted()函数。不过,我

回答 1 投票 0

满二叉树的递归关系

对于一般的n,导出Bn的递推关系。这里Bn表示具有n个顶点的满二叉树的数量。我发现它是 B(n)=2B(n-1) +1。但解完这个方程后,...

回答 1 投票 0

哈希表创建运行时复杂性

插入哈希表的最坏情况复杂度为 O(n)。 但是,当创建一个新的哈希表并插入 n 个元素时,据我了解,这将导致 n 个插入不断增长......

回答 1 投票 0

在 C# 中基于时间戳合并 2 个大型列表的最有效方法是什么?

我有 2 个数据列表,事件和心跳。 公共类 RunEvent :EntityBase { 公共 DateTime EventDateUtc { 获取;放; } 公共列表相关心跳{...

回答 1 投票 0

链表第n次插入失败

我写了一段代码在LinkedList中插入数据。但是输出数据有问题。这是代码。 #包括 #包括 #包括 #包括 我写了一段代码来在LinkedList中插入数据。但是输出数据有问题。这是代码。 #include <stdio.h> #include<math.h> #include<string.h> #include<stdlib.h> struct node { char *name; int age; struct node *next; }; struct node *linkeslist1head= NULL; struct node *linkedlist1tail= NULL; void llinsertend(const char *a,const int *b){ struct node *current = malloc(sizeof(struct node)); if(current == NULL){ printf("Current creation failed.\n"); } current->name = malloc(strlen(a)+1); if(current->name == NULL) { printf("String allocation failed\n"); } strcpy(current->name,a); current->age = *b; if(linkeslist1head == NULL){ linkeslist1head = current; linkedlist1tail = current; }else{ //If the list is not empty, append the new node to the end linkedlist1tail->next = current; // Update tail to point to the new last node linkedlist1tail = current; } } void llinsertbegin(const char *a, const int *b) { struct node *newnode = malloc(sizeof(struct node)); if (newnode == NULL) { printf("Memory allocation failed\n"); return; } newnode->name = malloc(strlen(a) + 1); if (newnode->name == NULL) { printf("String allocation failed\n"); free(newnode); return; } strcpy(newnode->name, a); newnode->age = *b; if (linkeslist1head == NULL) { // If the list is empty newnode->next = NULL; linkeslist1head = newnode; linkedlist1tail = newnode; } else { // If the list is not empty newnode->next = linkeslist1head; linkeslist1head = newnode; } } void llinsertaftern(const char *a, const int *b, int n) { struct node *current = linkeslist1head; int i; for (i = 1; current != NULL && i < n; i++){ current = current->next; // Iterate until the (n-1)th node or until current becomes NULL } if (current == NULL){ printf("LL short\n"); return; // Exit the function if current is NULL } printf("Reached node %d\n", i); struct node *newnode = malloc(sizeof(struct node)); if (newnode == NULL) { printf("Memory allocation failed\n"); return; } newnode->name = malloc(strlen(a) + 1); if (newnode->name == NULL) { printf("String allocation failed\n"); free(newnode); return; } strcpy(newnode->name, a); newnode->age = *b; if (current == NULL) { printf("LL is shorter than %d\n", n); free(newnode->name); free(newnode); return; } newnode->next = current->next; current->next = newnode; } void outputLinkedList(struct node *head){ struct node *p = head; while(p != NULL){ printf("Name:%s Age:%d\n",p->name,p->age); p = p->next; } printf("\n"); } int main() { printf("How many persons' details you want to add\n"); int t; scanf("%d",&t); getchar(); for(int i=1;i<=t;i++){ int x; char name[50]; scanf("%s",name); getchar(); scanf("%d",&x); llinsertend(name,&x); } int x=10,y=20,z=30; llinsertbegin("facebook",&x );//(const int *) 10 llinsertbegin("instragram", &y); llinsertbegin("whatsapp", &z); outputLinkedList(linkeslist1head); int code=1200,pos = 3; llinsertaftern("Dhaka",&code,pos); outputLinkedList(linkeslist1head); } 在第二个输出中,LinkedList 在我的 CLion 和 Codeblocks 中都不起作用,但它在编译器资源管理器中起作用。我的意思是通过 llinsertend() 函数添加一些元素然后 llinsertbegin() 输出函数完美运行。但是当我使用 llinsertaftern 函数时,它在调试器中显示分段错误。假设如果我通过 insertend 函数添加 3 个元素,并通过 insertbegin 函数添加 3 个元素,那么 pos = 3 的 inseraftern 的问题是什么。它应该工作得很好。 您的代码包含许多错误。例如,让我们考虑函数 llinsertend void llinsertend(const char *a,const int *b){ struct node *current = malloc(sizeof(struct node)); if(current == NULL){ printf("Current creation failed.\n"); } current->name = malloc(strlen(a)+1); if(current->name == NULL) { printf("String allocation failed\n"); } strcpy(current->name,a); current->age = *b; if(linkeslist1head == NULL){ linkeslist1head = current; linkedlist1tail = current; }else{ //If the list is not empty, append the new node to the end linkedlist1tail->next = current; // Update tail to point to the new last node linkedlist1tail = current; } } 首先,如果未分配新节点,则继续处理空指针 if(current == NULL){ printf("Current creation failed.\n"); } current->name = malloc(strlen(a)+1); //... 同样,如果未分配字符数组,您将再次继续使用空指针 if(current->name == NULL) { printf("String allocation failed\n"); } strcpy(current->name,a); //... 并且您忘记将新创建节点的数据成员next设置为NULL。 所以只有这一个函数包含三个错误。 注意函数llinsertaftern可以将新节点追加到列表的尾部。然而该函数不会改变指针linkedlist1tail。

回答 1 投票 0

为什么创建一个集合并在每个循环中获取其 len 比预先完成要花费更长的时间?

m = len(集合(nums)) n = len(数字) 答案=0 对于范围 (n) 内的 i: s = 设置() 对于范围 (i, n) 内的 j: s.add(nums[j]) ans += len(s) == m 返回答案 从上面的代码中,当我用 len(set(

回答 1 投票 0

对计算大O复杂度感到困惑

最近我开始用Python学习数据结构 列表_ = [6, 4, 3, 2, 1, 7] 预期总和结果 = 9 deftwo_pair_sum_using_complement(数组,expected_sum): unique_numbers = set() #...

回答 1 投票 0

求数组的最大反转和

您将获得一个由 n 个整数组成的 0 索引数组 vect。您需要执行以下 2 个操作一次 - 选择索引 i {0, 1, 2, ..., n-1} 并乘以整个前缀段 {0..i}...

回答 1 投票 0

在给定条件下查找字符串中不同字母的数量;小写字母不能在大写字母之前

给你一串由N个英文字母组成的字母。计算以大写和小写形式出现的不同字母的数量,其中给定字母的所有小写字母都出现

回答 1 投票 0

搜索长字符串数组中的特定事物。然后将其保存在 JSON 对象中

我是新来的。我正在开发一个读取一堆 docx 文档的程序。我使用 XPATH 和 xmldom 从他的 XML 中获取文档内容。它为我提供了包含文档每一行的数组。薄...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.