滴滴出行2015在线笔试题目

最大子矩阵

题目:求一个矩阵中最大2*2矩阵(元素和最大)的和。

如:

1 2 3 0 4

2 3 4 5 1

1 1 5 3 0

中最大的是

4 5

5 3

和为17

输入:m*n的矩阵

输出:该m*n矩阵的最大2*2子矩阵的和。

例如输入:

1 2 0 3 4 ; 2 3 4 5 1; 1 1 5 3 0

输出:

17

 

分析:这道题目是一道OJ的题目,原题是求最大子矩阵的和,题目里子矩阵是随意的,没有2*2的限制。这里2*2子矩阵是将问题简化了。最蛋疼的是,问题的输入格式太奇葩,大部分时间都花在调这个输入格式上了!!!最简单的方法是对矩阵进行遍历,得到最大子矩阵的和即可。不得不说的是,如果这个题目不考输入输出,真心不知道是要考察什么。

package com.didi;

import java.util.Scanner;

/**
 * Created by fang on 2015/9/25.
 */


class Solution {
    public int maxSubMatrix(int[][] matrix) {
        int result = Integer.MIN_VALUE;

        for (int i = 0; i < matrix.length - 1; i++) {
            for (int j = 0; j < matrix[0].length - 1; j++) {
                int tempSum = matrix[i][j] + matrix[i][j + 1] + matrix[i + 1][j] + matrix[i + 1][j + 1];
                result = Math.max(result, tempSum);
            }
        }

        return result;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] lines = line.split(";");
        int row = lines.length;

        String[] line2 = line.split(" ");
        int col = (line2.length - row + 1) / row;


        int[][] matrix = new int[row][col];

        for (int i = 0; i < row; i++) {
            String str = lines[i];
            String[] temp = str.split(" ");
            int k = 0;
            for (int j = 0; j < temp.length; j++) {
                if (!temp[j].equals(" ") && !temp[j].equals("")) {
                    matrix[i][k++] = Integer.parseInt(temp[j]);
                }
            }
        }


        Solution sln = new Solution();
        int result = sln.maxSubMatrix(matrix);
        System.out.println(result);
    }
}

 

最长和为0的子序列

输入:

所有数据在一行,元素之间用空格隔开

输出:

所有数据在一行,元素之间用空格隔开

 

样例输入:

1 2 3 4 -1 -2 -4 -3 1 2

样例输出:

1 2 3 4 -1 -2 -4 -3

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by fang on 2015/9/25.
 */
class Interval {
    int start;
    int end;

    public Interval(int start, int end) {
        this.end = end;
        this.start = start;
    }

    public int getLength() {
        return Math.abs(end - start + 1);
    }
}

class Solution {
    public int[] getMaxZero(int[] nums) {
        int thisSum = 0;
        ArrayList<Interval> intervals = new ArrayList<>();

        for (int i = 0; i < nums.length; i++) {
            thisSum = nums[i];
            for (int j = i + 1; j < nums.length; j++) {
                thisSum += nums[j];
                if (thisSum == 0) {
                    Interval interval = new Interval(i, j);
                    intervals.add(interval);
                }
            }
        }

//        Collections.sort(intervals, new Comparator<Interval>() {
//            @Override
//            public int compare(Interval o1, Interval o2) {
//                return o1.getLength() > o2.getLength() ? 1 : 0;
//            }
//        });

        intervals.sort((Interval i1, Interval i2) -> i1.getLength() > i2.getLength() ?
                1 : (i1.getLength() == i2.getLength() ? 0 : -1));

        Interval maxInterval = intervals.get(intervals.size() - 1);

        int[] result;
        result = new int[maxInterval.getLength()];
        System.arraycopy(nums, maxInterval.start, result, 0, maxInterval.getLength());

        return result;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        String[] strs = line.split(" |,");
        int[] nums = new int[strs.length];

        for (int i = 0; i < nums.length; i++) {
            nums[i] = Integer.parseInt(strs[i]);
        }

        Solution sln = new Solution();
        int[] result = sln.getMaxZero(nums);

        for (int i : result) {
            System.out.print(i + " ");
        }
    }
}

  

posted @ 2015-09-26 10:57  fangying  阅读(279)  评论(0编辑  收藏  举报