douqiao1997 2017-08-18 07:40
浏览 476

将JAVA程序转换为PHP代码

I have below program in JAVA.

private static int frogJump(int[] arrEl,int postion) {
        /** Marker array for the leaf found on the way. */
        boolean[] leafArray = new boolean[postion+1];

        /** Total step needed for frog. */
        int steps = postion;

        for(int i = 0; i<arrEl.length; i++) {

            /** if leaf needed for frog and it does not exist earlier. **/
            if(postion>=arrEl[i] && !leafArray[arrEl[i]]) {

                /* Mark leaf found */
                leafArray[arrEl[i]] = true;

                /** Reduce the step by one(coz one jump found). */
                steps--;
            }

            if(steps == 0 && arrEl[i]==postion) {
                return i;
            }
        }

        return -1;
    }

Which i want to convert in PHP. Till now what i have done is

function solution ($A = [], $Position) {

    $StonesArray = array();

    $StonesArray[TRUE] = $Position + 1;

    $steps = $Position;

    for($i = 0; $i< count($A); $i++) {

        echo "<pre>";
        print_r($StonesArray);

        if($Position >= $A[$i] && !$StonesArray[$A[$i]]) {


            $StonesArray[$A[$i]] = true;


            $steps--;
        }

        if($steps == 0 && $A[$i] == $Position) {
            return $i;
        }
    }

    return -1;
}

$GetSolution  = solution([3,2,1], 1);
echo "<pre>";
print_r($GetSolution);

above program should return 3. but after i have converted the program to PHP language its not returning the expected value.

I am sure I have done everything correct except converting the below line

boolean[] leafArray = new boolean[postion+1];

How to write this line in PHP?

  • 写回答

1条回答

  • duanpo7354 2017-08-18 08:24
    关注

    I just translated your original Java code to PHP 1:1, most of it can be used as is with little change, look at this example:

    function frogJump(array $arrEl, $postion) {
        /** Marker array for the leaf found on the way. */
        $leafArray = array_fill(0, $postion+1, false);
    
        /** Total step needed for frog. */
        $steps = $postion;
    
        for($i = 0; $i<count($arrEl); $i++) {
    
            /** if leaf needed for frog and it does not exist earlier. **/
            if($postion>=$arrEl[$i] && !$leafArray[$arrEl[$i]]) {
    
                /* Mark leaf found */
                $leafArray[$arrEl[$i]] = true;
    
                /** Reduce the step by one(coz one jump found). */
                $steps--;
            }
    
            if($steps == 0 && $arrEl[$i]==$postion) {
                return $i;
            }
        }
    
        return -1;
    }
    

    print_r(frogJump([3,2,1], 1)); outputs 2.

    I also compiled the Java code and the output is also 2, so it seems correct to me? Run with System.out.println(frogJump(new int[]{3,2,1}, 1));

    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入