public int[][] findIntersection(int[][] firstList, int[][] secondList) { // write code here if (firstList == null || secondList == null) { return null; } if (firstList.length == 0 || secondList.length == 0) { return new int[][]{}; } int size = Math.max(firstList[firstList.length - 1][1], secondList[secondList.length - 1][1]); int[] array = new int[size + 1]; for (int i = 0; i < firstList.length; i++) { for (int j = firstList[i][0]; j <= firstList[i][1]; j++) { array[j]++; } } for (int i = 0; i < secondList.length; i++) { for (int j = secondList[i][0]; j <= secondList[i][1]; j++) { array[j]++; } } List<int[]> list = new ArrayList<>(); for (int i = 0; i < array.length; i++) { if (array[i] == 2) { int j = i + 1; while (j < array.length && array[j] == 2) { j++; } list.add(new int[]{i, j - 1}); i = j; } } int[][] res = new int[list.size()][2]; for (int i = 0; i < res.length; i++) { res[i] = list.get(i); } return res; } public int StringSplit(String str) { // write code here // 先统计出总的a和b的数量,然后移动指针看在某个位置切割左a加右b的最大数量 int maxNum = 0; int aTotal = 0, bTotal = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == 'a') { aTotal++; } else if (str.charAt(i) == 'b') { bTotal++; } } // 在i后面的位置切割 int aCur = 0, bCur = 0; for (int i = 0; i < str.length() - 1; i++) { if (str.charAt(i) == 'a') { aCur++; } else if (str.charAt(i) == 'b') { bCur++; } maxNum = Math.max(maxNum, aCur + bTotal - bCur); } return maxNum; } public static void main(String[] args) { int i = numberOfWays(1, 2, 3); System.out.println(i); } public static int numberOfWays(int startPos, int endPos, int k) { // write code here // dp[i][j]表示间隔为i走j步有几种方式 int dis = endPos - startPos; int[][] dp = new int[dis + 3][k + 1]; for (int i = 0; i < dp.length; i++) { dp[i] = new int[k + 1]; } for (int j = 0; j < dp[0].length; j++) { if (j % 2 == 0) { dp[0][j] = j; } } dp[0][0] = 1; for (int j = 1; j < dp[0].length; j++) { for (int i = 1; i < dp.length - 1; i++) { dp[i][j] = Math.max(dp[i][j], dp[i + 1][j - 1] + dp[i - 1][j - 1]); } } // System.out.println(Arrays.deepToString(dp)); return dp[dis][k]; }

相关推荐

点赞 评论 收藏
分享
05-12 16:04
已编辑
江西财经大学 Java
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务