第一题
package exam; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; public class YongYou01 { public static void main(String[] args) { String [] s = {"nice","try","do"}; String[] str = new YongYou01().quick_sort(s); System.out.println(Arrays.toString(str)); } public String[] quick_sort (String[] array) { // write code here LinkedList<String> ll = new LinkedList<>(); for (int i = 0; i < array.length; i++) { ll.add(array[i]); } Collections.sort(ll, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }); for (int i = 0; i < array.length; i++) { array[i] = ll.get(i); } return array; } }
第二题
package exam; import java.util.Arrays; public class YongYou02 { public static void main(String[] args) { int [] one = {3,1,2}; int[] two = {2,5,4}; int[] merge = new YongYou02().merge(one, two); System.out.println(Arrays.toString(merge)); } public int[] merge (int[] one, int[] two) { int [] temp = new int[one.length+two.length]; for (int i = 0; i < one.length; i++) { temp[i] = one[i]; } for (int i = 0; i < two.length; i++) { temp[i+one.length] = two[i]; } Arrays.sort(temp); return temp; } }
第三题
package exam; import java.util.ArrayList; public class YongYou03 { public static void main(String[] args) { int [][] grid = {{1,3,1,2},{1,5,1,3},{3,2,1,2}}; System.out.println(new YongYou03().minPathSum(grid)); } ArrayList<Integer> al = new ArrayList<>(); public int minPathSum(int[][] grid) { int row = grid.length; int col = grid[0].length; func(grid, 0, 0,0); int min = al.get(0); for (int i = 1; i < al.size(); i++) { if (min > al.get(i)) { min = al.get(i); } } return min; } private void func(int[][] grid, int i, int j,int sum) { if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length ) return; sum = sum + grid[i][j]; if (i == grid.length-1 && j == grid[0].length -1) { al.add(sum); return; } func(grid,i+1,j,sum); func(grid,i,j+1,sum); } }
全部评论
(2) 回帖