java - 为什么这个java代码比cpp占用更少的空间和时间
问题描述
class Solution {
public:
void inorder(TreeNode* root, int low, int high, int& res){
if(root != nullptr){
inorder(root->left, low, high, res);
if(root->val <= high && root->val >= low){
res += root->val;
}
inorder(root->right, low, high, res);
}
}
int rangeSumBST(TreeNode* root, int low, int high) {
int res=0;
inorder(root, low, high, res);
return res;
}
};
爪哇:
class Solution {
int res;
public void inorder(TreeNode root, int low, int high){
if(root != null){
inorder(root.left, low, high);
if(root.val <= high && root.val >= low){
res += root.val;
}
inorder(root.right, low, high);
}
}
public int rangeSumBST(TreeNode root, int low, int high) {
res = 0;
inorder(root, low, high);
return res;
}
}
Leetcode 第 938 题 问题是求 BST [low, high] 的 Range sum
我在两种语言中都做的几乎一样(我猜,但我是 java 新手),但是 java 代码与 cpp 相比如何如此之快,它与指针和引用或站点使用的编译器有关吗?
问候
解决方案
经过一番研究,我发现它与平台本身有关。Leetcode:java 比 C++ 快吗
根据一条评论“他们从 Java 的基准测试中删除了编译和其他过程运行时。这意味着对于 C/C++ 解决方案,编译时间计入运行时”
不确定这有多可靠。
推荐阅读
- html - 如果有内容,我想不出一种方法让我的身体至少有全屏或更多?
- html - 覆盖 react-multiselect-checkboxes 样式
- aws-glue - 在没有 Glue 数据目录的情况下使用 AWS Glue 数据连接
- javascript - Moment.js 日期验证 DD-MMM-YYYY
- google-bigquery - 使用 Dataflow 将文件名和时间戳添加到 BigQuery 中的每条记录中
- java - 有没有一种方法可以比较java中不同长度的字符串?
- go - 如何提供 WASM 文件
- html - 我创建了一个 ID“滑块”并将其显示为 inline-flex 但 chrome 没有显示水平对齐的项目,为什么会这样?
- reactjs - 在 makeStyles 关键帧动画中传递道具
- arrays - 【求一些提示】将排序后的数组插入到构造的BST中