- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在阅读 Cracking the Coding Interview, Fourth Edition: 150 Programming Interview Questions and Solutions我正在尝试解决以下问题:
2.1 Write code to remove duplicates from an unsorted linked list. FOLLOW UP: How would you solve this problem if a temporary buffer is not allowed?
我正在用 C# 解决它,所以我制作了自己的 Node
类:
public class Node<T> where T : class
{
public Node<T> Next { get; set; }
public T Value { get; set; }
public Node(T value)
{
Next = null;
Value = value;
}
}
我的解决方案是遍历列表,然后针对每个节点遍历列表的其余部分并删除所有重复项(请注意,我没有按照书中的说明实际编译或测试它):
public void RemoveDuplicates(Node<T> head)
{
// Iterate through the list
Node<T> iter = head;
while(iter != null)
{
// Iterate to the remaining nodes in the list
Node<T> current = iter;
while(current!= null && current.Next != null)
{
if(iter.Value == current.Next.Value)
{
current.Next = current.Next.Next;
}
current = current.Next;
}
iter = iter.Next;
}
}
这是书中的解决方案(作者用java写的):
Without a buffer, we can iterate with two pointers: “current” does a normal iteration, while “runner” iterates through all prior nodes to check for dups. Runner will only see one dup per node, because if there were multiple duplicates they would have been removed already.
public static void deleteDups2(LinkedListNode head)
{
if (head == null) return;
LinkedListNode previous = head;
LinkedListNode current = previous.next;
while (current != null)
{
LinkedListNode runner = head;
while (runner != current) { // Check for earlier dups
if (runner.data == current.data)
{
LinkedListNode tmp = current.next; // remove current
previous.next = tmp;
current = tmp; // update current to next node
break; // all other dups have already been removed
}
runner = runner.next;
}
if (runner == current) { // current not updated - update now
previous = current;
current = current.next;
}
}
}
所以我的解决方案总是从当前节点到末尾查找重复项,而他们的解决方案从头部到当前节点查找重复项。我觉得这两种解决方案都会遇到性能问题,具体取决于列表中有多少重复项以及它们的分布方式(密度和位置)。但总的来说:我的答案和书中的答案差不多好还是差得多?
最佳答案
如果你给一个人一条鱼,他们会吃一天。授人以渔...
我对实现质量的衡量标准是:
至于您的实现:
关于c# - 面试题: remove duplicates from an unsorted linked list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4542600/
我有一个内部表,里面有 108 个条目。从 9 到 9 个条目重复条目,我想删除这些重复项。由于它们完全相同,我使用了 delete adjacent duplicates from itab com
在 Team Foundation Server (TFS) 中链接两个工作项 (WI) 时,在什么情况下将“Duplicate”和“Duplicate Of”区分为“链接类型”是有意义的? 如何处理
ld: duplicate symbol _velocityX in \ /Users/Student/Library/Developer/Xcode/DerivedData/finalproject
我使用 PHP 和 Mysql。 此 SQL 有效: INSERT INTO products (id, title, description) VALUES (10, 'va
我有一个大数据框 (120000x40),我尝试在每一行中找到重复项并显示它们。这就是我的尝试: 创建数据框 import pandas as pd df = pd.DataFrame({'col1'
我是 mySQL 和 PHP 的新手,请多多包涵。 如果我的查询有重复字段,我该如何做到这一点,检索到的数据将具有重复查询字段的重复数据。 一个例子是这样的: 查询 id = 34, 54, 21,
我一直遇到这个错误,但我无法理解它,因为它提示一个值恰好出现一次。 Exception in thread "main" java.lang.IllegalStateException: Duplic
我有一个带有 Vuejs 和 Laravel 的 Web 应用程序 我想使用 CKEditor 5 我安装了依赖项 npm install --save @ckeditor/ckeditor5-vue
我有一个包含以下数据的 csv 文件: Id,Name,Type,date 1,name1,employee,25/04/2017 2,name2,contrator,26/04/2017 3,nam
import CKEditor from '@ckeditor/ckeditor5-react'; import ClassicEditor from '@ckeditor/ckeditor5-bui
表定义: CREATE TABLE PositionalDataNGS ( Date DATE, Time TIME(3) , X FLOAT(5), Y FLOAT(5), D FLOAT(5) ,
我目前正在做一个项目,我要处理数以千计的数据包。现在,我记录每个数据包的 IP 和 MAC 地址以及一些其他信息。为了存储所有这些,我使用 MySQL 并且我的脚本是用 Node.js 编写的。目前我
I am using MySQL 5.1.56, MyISAM. My table looks like this:我使用的是MySQL 5.1.56,MyISAM。我的桌子是这样的: CR
我是新来的,对 SQL 比较陌生。我有一个类似这样的表: [Pk], [Case_No], [Status], [Open_Date], [Close_Date], [Case_Age], [Repo
为什么会收到此警告? warning No duplicate props allowed react/jsx-no-duplicate-props# 它显示的是第28行,但没有使用 Prop 。 最
是否有任何函数或方法可以在 python 2.7 中递归实现此目的? Input : ['and', ['or', 'P', '-R', 'P'], ['or', '-Q', '-R', 'P']]
我正在分析 hadoop 中的数据。有一些重复条目,其中 A、B 列重复,而 C 列不同。我想要做的是仅识别 A、B 重复项,然后为每个重复项打印出 C 列的不同值。 示例数据: row, data
您好,感谢阅读并可能对我有所帮助 我的问题的简要说明: 我正在将数据从一个 Firebird 数据库复制到另一个(称为 V14),并且我正在使用 IBExpert 来执行此操作。这些表的名称相同并且具
我想制作一张很像下面的图片: 我想使用 seaborn 使图表看起来漂亮,并让我自己以后更容易使用 facetgrids(我有十个不同的数据集,我想在同一个图表中显示。) 我在 seaborn 中找到
我在两列“user_id”和“project_id”上有一个复合唯一键。 当我尝试对单行或多行运行 DELETE 查询时,出现错误。 ERROR 1062: 1062: Duplicate entry
我是一名优秀的程序员,十分优秀!