[topcoder]TallPeople

水题。http://community.topcoder.com/stat?c=problem_statement&pm=2923&rd=5854

一开始错了是因为理解错题意。还有就是初始化min应该设为Integer.MAX_VALUE,而不是相反。

public class TallPeople {

	public int[] getPeople(String[] people) {

		int min = Integer.MAX_VALUE;

		int max = Integer.MIN_VALUE;

		int len1 = people.length;

		if (len1 == 0) return null;

		String[] hgts = people[0].split(" ");

		int len2 = hgts.length;

		if (len2 == 0) return null;

		int mx[][] = new int[len1][len2];



		for (int i = 0; i < len1; i++) {

			String line = people[i];

			String[] heights = line.split(" ");

			int _min = Integer.MAX_VALUE;

			for (int j = 0; j < len2; j++) {

				int tmp = Integer.parseInt(heights[j]);

				mx[i][j] = tmp;

				if (tmp < _min) _min = tmp;

			}

			if (_min > max) max = _min;

		}

		for (int j = 0; j < len2; j++) {

			int _max = Integer.MIN_VALUE;

			for (int i = 0; i < len1; i++) {

				if (mx[i][j] > _max) _max = mx[i][j];

			}

			if (_max < min) min = _max;

		}



		return new int[] {max, min};

	}

}

  

你可能感兴趣的:(topcoder)