当前位置 : 主页 > 网络编程 > 其它编程 >

NC_36_findMedianinTwoSortedArayNC_37_MERGEINTERVAL

来源:互联网 收集:自由互联 发布时间:2023-07-02
packageorg.example.interview.practice;***@authorxianzhe.ma*@date2021116*publicclassNC_36_f package org.example.interview.practice;/** * @author xianzhe.ma * @date 2021/11/6 */public class NC_36_findMedianinTwoSortedAray { public int findMedi
packageorg.example.interview.practice;***@authorxianzhe.ma*@date2021116*publicclassNC_36_f

package org.example.interview.practice;/** * @author xianzhe.ma * @date 2021/11/6 */public class NC_36_findMedianinTwoSortedAray { public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) { // write code here int finalPos = arr1.length; int firstIndex = 0; int secOndIndex= 0; if (finalPos == 1) { return Math.min(arr1[0], arr2[0]); } int tag = -1;//0 first 1 second while (firstIndex + secondIndex < finalPos) { if (arr1[firstIndex] <= arr2[secondIndex]) { firstIndex++; tag = 0; } else { secondIndex++; tag = 1; } } if (tag == 0) { return arr1[firstIndex-1]; } else { return arr2[secondIndex-1]; } }}

package org.example.interview.practice;import java.util.ArrayList;import java.util.Comparator;/** * @author xianzhe.ma * @date 2021/7/27 */public class NC_37_MERGEINTERVAL { public static class Interval { int start; int end; Interval() { start = 0; end = 0; } Interval(int s, int e) { start = s; end = e; } } public ArrayList merge(ArrayList intervals) { intervals.sort(new Comparator() { @Override public int compare(Interval o1, Interval o2) { return o1.start - o2.start; } }); ArrayList ans = new ArrayList(); int len = intervals.size(); if (len == 0) return ans; for (int i = 1; i  

上一篇:Linux之常用命令——04
下一篇:没有了
网友评论