嘿伙计我正在做一个问题,我必须在N x M行的矩阵A中找到一个点,以便
该点上方的行总和等于行的总和
考虑这个例子
/**
* A[0][0] = 2 A[0][1] = 7 A[0][2] = 5
* A[1][0] = 3 A[1][1] = 1 A[1][2] = 1
* A[2][0] = 2 A[2][1] = 1 A[2][2] = -7
* A[3][0] = 0 A[3][1] = 2 A[3][2] = 1
* A[4][0] = 1 A[4][1] = 6 A[4][2] = 8
* @param matrix
* @return
*/
在这个例子中,如果我们看点A [1] [1],可以说上面的行(sum = 14)等于点下面的行的总和。 谁可以帮我这个事?
到目前为止,我已经走到了这一步。但我知道这是一个很糟糕的方法。public int solution(int[][] matrix) {
int rows = matrix[0].length;
int columns = matrix.length;
int sumabove = 0;
int sumbelow = 0;
for( int i = 1; i < rows; i++ ) {
for (int j = 0; j < columns; j++) {
sumabove += matrix[i - 1][j];
sumbelow += matrix[i + 1][j];
}
}
return 0;
}
答案 0 :(得分:2)
我们的想法是计算每一行的总和(int[] rowsSum
)和所有行的总和(totalRowsSum
)。然后迭代遍历行,比较前一行(currentSum
)的总和与下一行的总和(totalRowsSum - currentSum - rowsSum[i]
)。
public static int solution(int[][] matrix)
{
int foundRowNumber = -1;
int rows = matrix.length;
int columns = matrix[0].length;
int[] rowsSum = new int[rows];
int totalRowsSum = 0;
for (int i = 0; i < rows; i++)
{
int rowSum = 0;
for (int j = 0; j < columns; j++)
{
rowSum += matrix[i][j];
}
rowsSum[i] = rowSum;
totalRowsSum += rowSum;
}
int currentSum = 0;
for (int i = 0; i < rows; i ++)
{
if (currentSum == totalRowsSum - currentSum - rowsSum[i])
{
foundRowNumber = i;
break;
}
currentSum += rowsSum[i];
}
return foundRowNumber;
}
答案 1 :(得分:0)
我说: 1st:得到整个矩阵的总和
第二名:除以2并存入var(中)
第3步:每次循环它以使其成为一行并且它将获得该行的总和并从总和中减去它,如果它小于剩余的则然后保持运行,如果剩余的数字小于那么( mid)然后你走到中间,开始检查你所在行的列,然后再使用相同的概念检查另一个循环
像这样的东西(伪代码):int sum = matrix.sum();
int mid = sum/2;
int topsum = 0;
int botsum = sum;
int counter=0;
bool fail = false;
while(topsum!=botsum && !fail) //either break when both top and bottom are same or if there is no middle gorund
{
int rowsum; //use loop to get the sum of the row
for(int i=0; i>colLength(); i++) rowsum=+ matrix[counter][i];
topsum =+ rowsum;
botsum=-rowsum;
counter++;
if(botsum>mid) //if you go over the middle
{
botsum=-rowsum; //take it back before last addition acction
counter --; //go back to the last row;
//loop
//here you will start checking columns to get to the middle.
}
}
这是为了让你获得单元而不是行,但你可以根据自己的喜好进行更改。
答案 2 :(得分:0)
好的,我能够做出解决方案。但我认为我搞砸了复杂性。我原本应该做一个线性的复杂性。无论如何我的解决方案是
public class MainClass {
static int matrix[][] = {{2, 7, 5}, {3, 1, 1}, {2, 1, -7}, {0, 2, 1}, {1, 6, 8}};
public static void main(String[] args) {
System.out.print(solution(matrix));
}
/**
* A[0][0] = 2 A[0][1] = 7 A[0][2] = 5
* A[1][0] = 3 A[1][1] = 1 A[1][2] = 1
* A[2][0] = 2 A[2][1] = 1 A[2][2] = -7
* A[3][0] = 0 A[3][1] = 2 A[3][2] = 1
* A[4][0] = 1 A[4][1] = 6 A[4][2] = 8
* @param matrix
* @return
*/
public static int solution(int[][] matrix) {
int columns = matrix[0].length;
int rows = matrix.length;
int sumRowsAbove = 0;
int sumRowsBelow = 0;
int sumColLeft = 0;
int sumColRight = 0;
int equilibrium = 0;
for( int i = 0; i < rows; i++ ) {
for (int j = 0; j < columns; j++) {
sumRowsBelow = getSumRowsBelow(i);
sumRowsAbove = getSumAbove(i);
sumColLeft = getSumColumnLeft(j);
sumColRight = getSumColumnRight(j);
if(sumRowsAbove == sumRowsBelow && sumColLeft == sumColRight) {
equilibrium++;
}
int x = 2;
x+=2;
}
}
return equilibrium;
}
public static int getSumRowsBelow(int rowNum) {
int columns = matrix[0].length;
int rows = matrix.length;
int sumBelow = 0;
for( int i = rowNum; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if((i+1) < rows){
sumBelow += matrix[i + 1][j];
}
}
}
return sumBelow;
}
public static int getSumColumnRight(int column) {
int columns = matrix[0].length;
int rows = matrix.length;
int sumBelow = 0;
for (int j = column; j <= columns; j++) {
for( int i = 0; i < rows; i++) {
if((j+1) < columns){
sumBelow += matrix[i][j + 1];
}
}
}
return sumBelow;
}
public static int getSumColumnLeft(int column) {
int columns = matrix[0].length;
int rows = matrix.length;
int sumBelow = 0;
for (int j = column; j >= 0; j--) {
for( int i = 0; i < rows; i++) {
if((j-1) >= 0){
sumBelow += matrix[i][j - 1];
}
}
}
return sumBelow;
}
public static int getSumAbove(int rowNum) {
int columns = matrix[0].length;
int rows = matrix.length;
int sumBelow = 0;
for( int i = rowNum; i >= 0; i--) {
for (int j = 0; j < columns; j++) {
if((i-1) >= 0){
sumBelow += matrix[i - 1][j];
}
}
}
return sumBelow;
}
} 这是一个关于找到点数(平衡)的密码问题,这样所选点上方的行中的元素总和等于该点下面的行的总和。此外,所选点左侧的列总和等于该点右侧的列总和。