小编Sam*_*eow的帖子

Java && || 在RETURN语句中?

我正在看一些Java算法示例,我在递归方法中遇到了这个代码片段:

boolean subTree(TreeNode t1, TreeNode t2) {
    if (t1 == null) {
        return false;
    }
    if (t1.value == t2.value) {
        if (treeMatch(t1, t2))
            return true;;
    }
    return (subTree(t1.left, t2) || subTree(t1.right, t2));
}
Run Code Online (Sandbox Code Playgroud)

不知道(也从未见过)|| 在之前的返回语句中使用,更不用说递归语句了,真让我感到困惑.我将代码复制到Eclipse中以查看它是否有效.然后我替换了|| 用&&​​和Eclipse似乎并没有被它打扰.从逻辑上讲,我理解这个递归代码应该继续沿着TreeNode t1的左右子树,但我正在寻找关于这种Java语法如何工作的更多理论解释.

有人可以解释||背后的含义 和Java的返回声明中的&&?在递归方面它意味着什么?它是否仅在与递归一起使用时才有意义?

java algorithm syntax recursion return

7
推荐指数
2
解决办法
8519
查看次数

C++ int到字符串转换

当前源代码:

string itoa(int i)
{
    std::string s;
    std::stringstream out;
    out << i;
    s = out.str();
    return s;
}

class Gregorian
{
    public:
        string month;
        int day;
        int year;    //negative for BC, positive for AD


        // month day, year
        Gregorian(string newmonth, int newday, int newyear)
        {
            month = newmonth;
            day = newday;
            year = newyear;
        }

        string twoString()
        {
            return month + " " + itoa(day) + ", " + itoa(year);
        }

};
Run Code Online (Sandbox Code Playgroud)

在我的主要:

Gregorian date = new Gregorian("June", 5, 1991); …
Run Code Online (Sandbox Code Playgroud)

c++ string int type-conversion

5
推荐指数
1
解决办法
6359
查看次数

标签 统计

algorithm ×1

c++ ×1

int ×1

java ×1

recursion ×1

return ×1

string ×1

syntax ×1

type-conversion ×1