- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在 Leet Code 准备算法测试,这里是 N°160 Intersection of Two Linked Lists 的描述:
编写一个程序,找出两个单向链表的交集开始的节点。
例如下面两个链表:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
在节点 c1 处开始相交。
注意事项:
null
。然后我写了一个Java程序来解决。它通过了 39/42 个测试用例,然后在测试 39 时失败了。我研究了一个多小时也没有找到错误行。有人可以帮助我吗?
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
Stack<Integer> stackA = new Stack<>();
Stack<Integer> stackB = new Stack<>();
ListNode pointerA = headA;
ListNode pointerB = headB;
while (pointerA != null) {
stackA.push(pointerA.val);
pointerA = pointerA.next;
}
while (pointerB != null) {
stackB.push(pointerB.val);
pointerB = pointerB.next;
}
// find the common part between A & B, then build the comment node
ListNode intersect = null;
while (!stackA.empty() && !stackB.empty() && stackA.peek() == stackB.peek()) {
ListNode newHead = new ListNode(stackA.peek());
newHead.next = intersect;
intersect = newHead;
stackA.pop();
stackB.pop();
}
return intersect;
}
}
Submission Result: Wrong Answer
- Input: Intersected at '10000':
- [1,3,5,7,9,...,9991,9993,9995,9997,9999,10000]
- [2,4,6,8,10,...,9990,9992,9994,9996,9998,10000]
- Output: No intersection
- Expected: Intersected at '10000'
我知道有更好的算法来处理这个测试,但在这篇文章中,我只想找出哪一行是错的。
最佳答案
Stack
数据结构使用对象Integer
而不是原始类型int
。所以我需要将比较更改为
stackA.peek().equals(stackB.peek())
这次提交被接受了。
现在如果你对这个算法测试感兴趣,你可以在the LeetCode forum中找到更好的解决方案。或 Program Creek由 danh 提供。
关于java - 我执行 "Intersection of Two Linked Lists"的错误在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39807850/
我有四个返回整数的查询。 select listOfIntegers from [something]... (编辑结果为 ROWS) 需要一种方法来做 select ... intersect se
我尝试过 Area.intersect(),并且想知道是否有一种方法可以使用 Path2D 创建与此类似的方法,因为我注意到使用 Path2D 作为形状时性能有所提升。换句话说,获取大 Path2D
我在使用 MARIADB 语言进行此查询时遇到问题。此查询不返回任何内容,但此查询应返回一行。我该怎么做才能让它发挥作用? `SELECT c.Raza, c.Nombre FROM caballos
为什么在 Groovy 中,当我创建 2 个列表时,如果我执行 a.intersect(b) 和 b.intersect(a) 会有区别: def list1 = ["hello", "world",
我需要在 MS Access 中交叉多个子查询。遗憾的是,Access-SQL 不支持 INTERSECT 关键字。我明白,我们如何使用两个表的 INNER JOIN 来获得我们想要的交集。但是我怎样
I have 5 equations and I've to take combination of 3 equations at a time to check if I'm getting
使用基本运算符,导出额外的运算符交集。 R ∩ S = ? 我认为可能是:(R ∪ S) - ((R -S) ∪ (S -R)) 但我也认为可能有更简单的方法吗? 最佳答案 A 与 B 相交等于 A
我有一个向量 A 定义为:(Ao+t∗Ad) 我还有一个圆锥体,其顶点(圆锥尖)V、轴方向D、底半径R 和高度H . 如何找到矢量和圆锥体之间的交点?我正在使用 glm 进行数学运算。 这是一个简单的
我正在寻找合适的加速结构来进行射线球相交测试(在游戏中)。适用以下条件: - 每帧大约有 100 个球体和 100 条光线可以相互测试 - 球体在每一帧中移动,光线也是如此 - 可以在每一帧中添加/删
我有两个代理集。是否有查找功能: 两个(相交)中都存在的代理的代理集 一个而不是另一个中存在的代理的代理集 我发现手动执行此操作非常困难,尤其是在三重ask内部需要它时 理想的用法类似于with语法:
如何测试交叉射线和三角形,如果存在如何获得从射线原点到交点的距离?如果在我的程序中我必须检查 1 条射线到 ~10000 个三角形,我可以使用什么优化?? 最佳答案 单个多边形射线相交测试很简单,只涉
我们如何使用交集方法组合两个 dfa? 最佳答案 使用叉积结构,正式解释here . 本质上,您将每个状态中的状态集交叉乘积,以获得与每台机器的任意状态组合相对应的元状态列表。如果两者都接受,这允许您
我的问题是关于 python pandas 的。我有两个系列,每个系列都有如下字符串元素:为了简化,我在 DataFrame 中连接了两个系列。 import pandas as pd import
我有两个表:P 和 PC(主要/详细信息由列 Id 连接) Table P: Id integer Name varchar(12) Table PC: Id integer Code varch
我正在使用Intersection Observer API,我想知道: 如何消除或限制Intersection Observer API? 如果我想提高性能,是否建议使用防抖或 throttle 功
可能真正的问题是“是否有人关心”,但这是不一致的地方: intersect(c(),1:3) integer(0) intersect(1:3,c()) NULL 同样的事情发生在 setdiff 上
我正在从事一个涉及 Neo4J Db 的项目,但我遇到了一个我自己无法解决的问题。 我们正在处理图中的 Acl。每个 Acl 都链接到一组元数据。项目也链接到这些元数据。当链接到项目的所有元数据也链接
我正在尝试在具有一个障碍物(暂时)的昆虫(蟑螂)社会中实现一种碰撞检测算法(极限环方法),为此障碍物(红色)被一圈包围影响(绿色),在这里,一旦检测到“roach”和“影响圈”之间的交叉点,我将计算每
我正在做题 Find all students who do not appear in the Likes table (as a student who likes or is liked) an
我有两个结果,希望得到这两个结果的最佳“顺序”。示例: 我们有一场比赛,一场比赛有 5 人,另一场比赛有 7 人。结果是:比赛 1. 1. Karl 2. Fred 3. John 4. Peter
我是一名优秀的程序员,十分优秀!