将代码从python转换为Java

时间:2017-01-26 21:49:04

标签: java python

我正在尝试将this代码从python转换为java,我陷入困境并需要帮助:

代码找到数组中最长的连续体,连续体中的值之和等于零模3,例如对于数组a[]={2,-3,5,7,-20,7}

我们有2-3 + 5 + 7-20 = -9所以输出为5,

Python代码:

a = [2,-3,5,7,-20,7]

best = 0
bestIndex = -1

dp = [[0,0,0] for i in range(len(a) + 1)]

for i in range(1,len(a) + 1):
  r = a[i-1] % 3

  for j in range(3):
    canSumToJ = dp[i-1][(3 + j - r) % 3] > 0

    dp[i][j] = max(1 if r == j else 0
              ,1 + dp[i-1][(3 + j - r) % 3] if canSumToJ else 0)

  bestIndex = i - 1 if dp[i][0] > best else bestIndex
  best = max(best,dp[i][0])

print(best,(bestIndex - best + 1, bestIndex)) # (5, (0, 4))

# dp
# => [[0, 0, 0]
#    ,[0, 0, 1]
#    ,[1, 0, 2]
#    ,[0, 3, 2]
#    ,[3, 1, 4]
#    ,[5, 4, 2]
#    ,[3, 6, 5]]

尝试转换为Java:

 public class mmn123 {
public static void main(String[] args)
{
    //int[] a = {1, 3, 3, 3, 3, 1};
 int[] a = {2, -3, 5, 7, -20, 7};
int best = 0;
//int bestIndex=-1 ;
int len=a.length;
int r;
int a1[]={0,0,0};
int a2[]={0,0,0};
int a3[]={0,0,0};
int a4[]={0,0,0};
int a5[]={0,0,0};
int a6[]={0,0,0};
int a7[]={0,0,0};
boolean canSumToJ=false;
 int [][]dp={a1,a2,a3,a4,a5,a6,a7};



// for i in range(1,len(a) + 1):
       for(int i=1;i<len+1;i++)
       {
           r = a[i-1] % 3;

   //for j in range(3):
           for(int j=0;j<3;j++)
           {
               if(dp[i][(3 + j - r) % 3]>0)
                  canSumToJ =true; ;

                  if(r==j&&canSumToJ ==true)
                     dp[i][j] = Math.max(1,1 + dp[i-1][(3 + j - r) % 3]);
                  if(r!=j&&canSumToJ ==true)
                    dp[i][j] = Math.max(0,1 + dp[i-1][(3 + j - r) % 3]);
                  if(r!=j&&canSumToJ ==false)
                      dp[i][j] = Math.max(0,0);
                  if(r==j&&canSumToJ ==false)
                      dp[i][j] = Math.max(1,0);
                 canSumToJ=false;
           }
           //canSumToJ=false;
           //if (dp[i][0] > best)
   //bestIndex = (i - 1);
           //else bestIndex;
   best = Math.max(best,dp[i][0]);
       }
       for(int k=0;k<a.length;k++)
       {
           for(int l=0;l<3;l++)
           {
               System.out.print(dp[k][l]);
           }
           System.out.println();
       }
System.out.println("best: "+best);
/*
 # dp
 # => [[0, 0, 0]
 #    ,[0, 0, 1]
 #    ,[1, 0, 2]
 #    ,[0, 3, 2]
 #    ,[3, 1, 4]
 #    ,[5, 4, 2]
 #    ,[3, 6, 5]]
 */
}
}
  

输出不一样。

2 个答案:

答案 0 :(得分:3)

有两个问题:

首先:错误 - 您忘记了-1中的dp[i-1]

    if(dp[i-1][(3 + j - r) % 3]>0)
        canSumToJ =true; ;

BTW:你可以写它类似于Python

    canSumToJ = (dp[i-1][(3 + j - r) % 3] > 0)

第二个: Java中的模数给出了负值 - 所以你得到-20 % 3 = -2

SO:Mod in Java produces negative numbers

你可以编写自己的函数来计算正模数。

public class Test {

    public static int modulo(int a, int b)
    {
        int r = a % b;

        if(r < 0)
        {
            //System.out.println(a + " % " + b + " = " + r);
            return r + b;
        }

        return r;
    }

    public static void main(String[] args)
    {
        int[] a = {2, -3, 5, 7, -20, 7};
        int[][] dp={{0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}, {0,0,0}};

        int r;
        int best = 0;

        boolean canSumToJ = false;

        int m, x, y;

        for(int i = 1 ; i < a.length + 1 ; i++)
        {
            r = modulo(a[i-1], 3);

            for(int j = 0 ; j < 3 ; j++)
            {
                m = modulo(3+j-r, 3);

                canSumToJ = (dp[i-1][m] > 0);

                if(r == j)
                    x = 1;
                else
                    x = 0;

                if(canSumToJ == true)
                    y = 1 + dp[i-1][m];
                else
                    y = 0;

                dp[i][j] = Math.max(x, y);    
            }

            best = Math.max(best, dp[i][0]);
        }

        System.out.println();

        for(int k = 0 ; k < a.length ; k++)
        {
            for(int l = 0 ; l < 3 ; l++)
            {
                System.out.print(dp[k][l]);
            }
            System.out.println();
        }

        System.out.println("best: " + best);

        /*
         # dp
         # => [[0, 0, 0]
         #    ,[0, 0, 1]
         #    ,[1, 0, 2]
         #    ,[0, 3, 2]
         #    ,[3, 1, 4]
         #    ,[5, 4, 2]
         #    ,[3, 6, 5]]
         */
    }
}

答案 1 :(得分:0)

def is_pallindrome(s): n = len(s) 如果n == 1: 返回True 对于范围(n // 2)中的i: 如果s [i]!= s [-i-1]: 返回False 返回True s =输入() 定义f(s): 对于范围(1,len(s)-2): 如果is_pallindrome(s [:a]): 对于范围(a + 1,len(s))中的b: 如果is_pallindrome(s [a:b])和is_pallindrome(s [b:]): 打印(s [:a]) 打印(s [a:b]) 打印(s [b:]) 返回 打印(“不可能”)

f(s)