TigerS
V2EX  ›  问与答

谁懂如何把Java的代码Transfer成Python的呢? 期中涉及到两个Java的Class 换成一个Python的. 只需要两个Function

  •  
  •   TigerS · Apr 13, 2013 · 2712 views
    This topic created in 4783 days ago, the information mentioned may be changed or developed.
    public int distance (Vertex x, Vertex y) {
    int returnVal = 0;
    Queue que = new LinkedList();
    x.touched = true;
    que.add(x);
    while (!que.isEmpty()) {
    returnVal++;
    Vertex n = (Vertex)que.remove();
    Vertex child = null;
    while ((child = getUnvisited(n))!=null) {
    if (child.value==y.value)
    return returnVal;
    child.touched=true;
    que.add(child);
    }
    }
    return returnVal;
    }

    public int diameter () {
    int maxDistance = 0;
    int tempDistance = 0;
    Vertex one;
    Vertex two;
    for (int x = 1; x < numNodes; x++) {
    one = (Vertex)vert.get(x);
    for (int y = 0; y < x; y++) {
    two = (Vertex)vert.get(y);
    tempDistance = distance(one, two);
    if (tempDistance > maxDistance)
    maxDistance = tempDistance;
    refreshTouched();
    }
    }
    return maxDistance;
    }

    ==============

    Vertex class

    public class Vertex {
    public boolean touched;
    public int value;
    public Vertex (int value) {
    touched = false;
    this.value = value;
    }
    }

    ====================

    主要需要实现上面的两个功能.
    有些部分比如 x.touched = true; 这个需要调用到第二个class vertex 不知道怎么在Python中(一个文件,没有class)表示
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2997 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:33 · PVG 21:33 · LAX 06:33 · JFK 09:33
    ♥ Do have faith in what you're doing.