小编ser*_*ent的帖子

Java Math.pow(a,b)时间复杂度

我想问下面代码的时间复杂度.是O(n)?(Math.pow()的时间复杂度是否为O(1)?)一般来说,Math.pow(a,b)是否具有时间复杂度O(b)或O(1)?提前致谢.

public void foo(int[] ar) {
   int n = ar.length;
   int sum = 0;
   for(int i = 0; i < n; ++i) {

     sum += Math.pow(10,ar[i]);

   }
}
Run Code Online (Sandbox Code Playgroud)

java time-complexity

11
推荐指数
2
解决办法
7969
查看次数

对象的equals()方法与String对象和我创建的对象有什么不同?

我对案例2和案例3中的对象的.equals()方法感到困惑.对于案例1,我可以理解引用和内容是相同的,所以我们分别得到了true,true.

在案例2中,equals()方法比较类型和内容,并返回true(根据文档,它首先比较对象类型,然后比较内容).但是在第三种情况下,即使类型和内容相同,它也会返回False!?String对象有什么特殊功能吗?任何帮助/提示/解释表示赞赏.

public class equalMethods {

static class MyObject {

       String name;

    // constructor
    MyObject(String s) {
        this.name = s;
    }
}

public static void main(String[] args) {

    // ***CASE 1****
    String a = "str1"; 
    String b = "str1";
    System.out.println(a == b); // True
    System.out.println(a.equals(b));// True
    System.out.println();

    // ***CASE 2****
    String an = new String("oracle");
    String bn = new String("oracle");
    System.out.println(an == bn); // False
    System.out.println(an.equals(bn));// True ( ? Compare with case 3)
    System.out.println();

    // // ***CASE 3****
    MyObject object1 …
Run Code Online (Sandbox Code Playgroud)

java oop string methods equals

-1
推荐指数
1
解决办法
108
查看次数

包含TreeNode的Array的ArrayList

我想创建一个由treeNodes组成的数组Arraylist.我的审判是

ArrayList<Arrays<treeNode>> aList = new ArrayList<Arrays<treeNode>>();
Arrays<TreeNode> aNodes = new ArrayList<TreeNode>();
Run Code Online (Sandbox Code Playgroud)

但它给出了一个错误.(包括utils)

写这个的正确方法是什么?我的目标是在二叉树中找到节点的最小深度(只是为了找到最小的不找到该节点本身,我将水平放入arraylist,一旦大小不是2 ^ j,那么最小级别是j-1 ).

提前感谢,任何帮助/提示/解决方案......

java arrays tree binary-tree arraylist

-2
推荐指数
1
解决办法
968
查看次数

标签 统计

java ×3

arraylist ×1

arrays ×1

binary-tree ×1

equals ×1

methods ×1

oop ×1

string ×1

time-complexity ×1

tree ×1