为什么这个java代码比cpp占用更少的空间和时间

0md85ypi  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(336)
  1. class Solution {
  2. public:
  3. void inorder(TreeNode* root, int low, int high, int& res){
  4. if(root != nullptr){
  5. inorder(root->left, low, high, res);
  6. if(root->val <= high && root->val >= low){
  7. res += root->val;
  8. }
  9. inorder(root->right, low, high, res);
  10. }
  11. }
  12. int rangeSumBST(TreeNode* root, int low, int high) {
  13. int res=0;
  14. inorder(root, low, high, res);
  15. return res;
  16. }
  17. };

java 语:

  1. class Solution {
  2. int res;
  3. public void inorder(TreeNode root, int low, int high){
  4. if(root != null){
  5. inorder(root.left, low, high);
  6. if(root.val <= high && root.val >= low){
  7. res += root.val;
  8. }
  9. inorder(root.right, low, high);
  10. }
  11. }
  12. public int rangeSumBST(TreeNode root, int low, int high) {
  13. res = 0;
  14. inorder(root, low, high);
  15. return res;
  16. }
  17. }


leetcode问题938。问题是找到bst[低,高]的范围和
我在这两种语言中做的都差不多(我猜,但我对java是新手),但是java代码与cpp相比怎么那么快呢?它与指针和引用或者站点使用的编译器有关吗?
当做

x759pob2

x759pob21#

经过一番研究,我发现它与平台本身有关。leetcode:java比c快吗++
根据一条评论“他们从java的基准中删除了编译和其他过程运行时。这意味着对于c/c++解决方案,编译时间是在运行时计算的”
不知道这有多可靠。

相关问题