+ = and = 1+在爪哇

发布于 2025-01-28 17:30:12 字数 302 浏览 0 评论 0原文

我和我的朋友一直在为学校解决问题。我们正在遍历带有DFS的图形,并计算每个给定组件中的节点数量。我们得到的结果很大,并且已经确定了差异的位置。

进入下一个递归时,我的朋友使用语法,

componentSize += DFS_visit(nextNodeToVisit);

而我

componentSize = 1 + DFS_visit(nextNodeToVisit);

最初使用的是这两个是相同的,那么有什么区别?在我们的情况下应该使用哪一个?

Me and my friend have been working on a problem for school. We are traversing a graph with DFS and are counting the number of nodes in each given component. We get widely different results and have identified where the difference lies.

When going into the next recursion, my friend uses the syntax

componentSize += DFS_visit(nextNodeToVisit);

whereas I use

componentSize = 1 + DFS_visit(nextNodeToVisit);

I originally thought these two were the same, so what is the difference? And which one should be used in our case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方圜几里 2025-02-04 17:30:12
 componentSize += DFS_visit(nextNodeToVisit); 

意思是

 componentSize = componentSize + DFS_visit(nextNodeToVisit);

将其与

 componentSize = DFS_visit(nextNodeToVisit) + 1;

差异进行比较吗?

通常,a< op> = b大致表示与a = a< op> b其中< op>操作员。 (a的类型也有LHS的类型。)


在我们的情况下应使用哪一个?

目前尚不清楚哪个是正确的。我们需要查看数据结构和更多算法。

 componentSize += DFS_visit(nextNodeToVisit); 

means

 componentSize = componentSize + DFS_visit(nextNodeToVisit);

Compare that with

 componentSize = DFS_visit(nextNodeToVisit) + 1;

See the difference?

In general a <op>= b means roughly the same as a = a <op> b where <op> an operator. (There is also a typecast of the LHS to the type of a.)


And which one should be used in our case?

It is not clear which is correct. We would need to see the data structure and more of the algorithm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文