小编Noo*_*tor的帖子

将二叉搜索树转换为双向链表

最近的一次编码采访中提到了这个问题.

问:给定二叉树,编写程序将其转换为双向链表.双链表中的节点按照由Z字形级别顺序遍历形成的顺序排列

我的方法

我总是可以对树的zig-zag级别顺序进行遍历并将其存储在数组中,然后创建一个双链表.但问题需要一个就地解决方案.任何人都可以帮助解释应该使用的递归方法吗?

tree-traversal binary-search-tree doubly-linked-list

4
推荐指数
1
解决办法
2万
查看次数

如何使用二叉树中的recusrion完成回溯

我正在尝试插入二进制节点.我的代码很复杂,没有希望拯救它,所以我打算重写它(基本上我没有考虑回溯,并没有考虑所有密切关注的算法).

我正在尝试使用顺序遍历插入二进制节点,但我不明白我应该如何回溯.

     D
    / \
   B   E 
  / \ / \
 A  C F 
Run Code Online (Sandbox Code Playgroud)

我如何搜索根D的左子树,然后返回并搜索正确的子树?这可能是一个愚蠢的问题,但我很难过.我能想到的最好的是这样的:

 if (!root.hasLeftChild) {
      root = root.getLeftChild(); 
      recurse(root); 
 } 
Run Code Online (Sandbox Code Playgroud)

但是当我到达底部时,我无法回到根部.此外,它没有解决问题,如果我到达左下方节点,我必须在开始回溯之前填充该节点的两个子节点.

我想我正在以错误的方式思考这个问题.

java recursion binary-tree

4
推荐指数
1
解决办法
3885
查看次数

Nock:与请求不匹配

我的诺克呼叫如下所示

app_url='myshop.app.com'
result = nock(app_url, {
            reqheaders: {
                 "content-type": "application/json",
                 'authorization': 'Basic Auth'
            }
          })
        .get('/admin/products.json?collection_id=10001&limit=250&fields=id')    
        .reply(200, {
                "products": [
                { "id": 101},
                {"id": 102},
            ]
        });
Run Code Online (Sandbox Code Playgroud)

解决 :

(node:1378) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Nock: No match for request { .  
Run Code Online (Sandbox Code Playgroud)

但现在,

=========已更新=============

我已经更新了我的调用,它没有抛出错误,但现在它没有拦截请求......它仍然在点击shopify来获取数据

mocha.js node.js nock

4
推荐指数
1
解决办法
2万
查看次数

基于条件的 lombok 对象构建

所以这是为构建Humans编写的现有代码片段(如matrix电影中:))

if (gender.equals("male")){
    return Human.builder()
        .gender('male')
        .name('abc')
        .speaks("english")
        .alive(true)
        .build();
}else{
    return Human.builder()
        .gender('female')
        .name('abcd')
        .speaks("english")
        .alive(true)
        .build();    
}
Run Code Online (Sandbox Code Playgroud)

如果你看一下,这段代码在属性分配中有很多冗余,可以最小化。现在想象一下 10 个类似的条件(这里,只有 2 个!),无论您尝试什么,它最终都会导致看起来很丑的冗余代码。

我尝试在网上搜索大量资源,但找不到任何按照 builder design构建对象的方法。我想在这里实现什么(减少代码冗余)如下所示:

Human human = Human.builder()
            .speaks("english")
            .alive(true);

if (gender.equals("male")){
        human = human    // or just human.gender('male').name('abc'); no assignment
        .gender('male')
        .name('abc');
}else{
        human = human // or just human.gender('female').name('abcd'); no assignment
        .gender('female')
        .name('abcd');
}            
return human.build();
Run Code Online (Sandbox Code Playgroud)

这是否可以通过 lombok 或任何人知道在这里构建对象的更好方法?
如果它值得,我在drop-wizard

java builder lombok dropwizard

4
推荐指数
1
解决办法
3720
查看次数

在bash中逐个字符地读取用户给定的文件

我有一个未格式化的文件,我想在每100个字符后放置一个换行符并删除其中的任何其他新行,以便文件看起来具有一致的宽度和可读性

此代码段有助于阅读所有行

 while read LINE
        do
                len=${#LINE}
                echo "Line length is : $len"
        done < $file
Run Code Online (Sandbox Code Playgroud)

但我如何为角色做同样的事情

想法是这样的:( 只是一个例子,它可能有语法错误,尚未实现)

 while read ch  #read character
  do
         chcount++ # increment character count

    if [ "$chcount" -eq "100" && "$ch"!="\n" ] #if 100th character and is not a new line
    then
        echo -e "\n" #echo new line
    elif [ "$ch"=="\n" ]  #if character is not 100th but new line
        then
        ch=" " $replace it with space
     fi
  done < $file
Run Code Online (Sandbox Code Playgroud)

我正在学习bash …

bash shell

3
推荐指数
2
解决办法
7816
查看次数

java中的转义序列无效(有效转义序列为\ b\t \n\f\r \"\'\\)

我正在使用父域和I帧的URL匹配.获取此错误以将以下代码放入Java类中.

out.print("ref = url.match(/:\/\/(.[^/]+)/)[1];");
Run Code Online (Sandbox Code Playgroud)

获取此行的错误:

Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
Run Code Online (Sandbox Code Playgroud)

原文:

    out.print("<script>");
Run Code Online (Sandbox Code Playgroud)

//需要在java类中放入以下行,所以使用 out.print(" ");

ref = url.match(/:\/\/(.[^/]+)/)[1];
out.print("</script>");
Run Code Online (Sandbox Code Playgroud)

如何正确使用?

javascript java regex escaping

3
推荐指数
1
解决办法
4663
查看次数

Java类型混乱

以这两个片段为例:

情况1:

Scanner s = new Scanner(System.in);
int n = s.nextInt(); /** take user input **/

n *= Double.NEGATIVE_INFINITY;
Run Code Online (Sandbox Code Playgroud)

案例2:

int k=10;
double kk = 10.10;

int rst = k*kk;
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,我都没有从我这边进行任何类型转换,但是Case 1执行并n正确打印值,但是Case 2抛出错误,can not convert from double to int.为何如此区别?

java casting

3
推荐指数
1
解决办法
415
查看次数

生命周期配置未涵盖插件执行

春天的开始

背景:到目前为止,我一直在研究核心 JAVA,现在我需要切换到 MVC
尝试从本教程制作我的第一个 Spring MVC Hello World 示例 ,我遇到以下错误: pom.xml

Multiple annotations found at this line:
    - Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-compiler-plugin:
     2.3.2:compile (execution: default-compile, phase: compile)
    - Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-resources-plugin:
     2.5:testResources (execution: default-testResources, phase: process-test-resources)
    - <packaging>war</packaging>
    - Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-resources-plugin:
     2.5:resources (execution: default-resources, phase: process-resources)
    - Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-compiler-plugin:
     2.3.2:testCompile (execution: default-testCompile, phase: test-compile)
Run Code Online (Sandbox Code Playgroud)

在 …

spring spring-mvc pom.xml maven eclipse-kepler

3
推荐指数
1
解决办法
2万
查看次数

在ksh中获取字符串的最后n个字符

假设我有一个变量name持有一些字符串值

要获取最后n个字符,在bash中我们写:

$ echo "${name: -n}"
Run Code Online (Sandbox Code Playgroud)

什么是相同的方式ksh,我已经看到sedawk方法,但我正在寻找的是一个线或管道解决方案类似于bash提取最后的字符

到目前为止,这些是错误和努力:

AB12 $ name="123456"

AB12 $ echo ${name:(-3)}
ksh: ${name:(-3)}: bad substitution

AB12 $ echo${name:0:-3}
ksh: echo${name:0:-3}: bad substitution

AB12 $ print ${name%?}
12345

AB12 $  echo "some string" | tail -c -1
tail: cannot open input

AB12 $ echo -n "my string discard"| tail -c -1
tail: cannot open input

AB12 $ echo "foo"| cut -c 1-2
fo

AB12 $ echo "foo"| …
Run Code Online (Sandbox Code Playgroud)

linux string shell ksh solaris

3
推荐指数
1
解决办法
6886
查看次数

创建一个通用函数来复制不同的对象

所以我有,我需要一个用例复制一个objectclassES(类可以根据在工厂输入类型而变化.

这是我想要做的样本

public interface DataUtil {

    // the main wrapper 
    static Object copyObject(Object payload){
        if(payload instanceof Human))
            return copyEntry((Human) payload);
        if(payload instanceof Car))
            return copyEntry((Car) payload);
        if(payload instanceof Planet))
            return copyEntry((Planet) payload);        
        return payload;
    }

    static Human copyEntry(Human human) {
        return Human.builder()
                .name(human.getName())
                .age(human.getAge())
                .build();
    }

    static Car copyEntry(Car car) {
        return Car.builder()
                .model(car.getModel())
                .brand(car.getBrand())
                .build();
    }

    static Planet copyEntry(Planet planet) {
        // return builder like previous
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你看一下copyObject函数,它会按预期完成工作,但是他的问题是返回类型.目前,为了使自己兼容,它返回一个, …

java copy object java-8

3
推荐指数
1
解决办法
95
查看次数