leetcode 44,56,179,274,853,948
44 情侣牵手
硬写,居然通过了。。
public static int minSwapsCouples(int[] row) { Map<Integer,Integer> map = new HashMap<>(); for (int i = 0; i < row.length; i++) { map.put(row[i],i); } int k = 0; for (int i = 0; i < row.length; i+=2) { int left = row[i]; int right = row[i+1]; if ( (Math.abs(left-right) == 1) && (( left+right)/2)%2 == 0){ //无需交换 }else { k++; if ((left&1) ==0){ Integer c = map.get(left + 1); swap(i+1,c,row,map); }else { Integer c = map.get(left - 1); swap(i+1,c,row,map); } } } return k; } private static void swap(int a,int b,int[] arr,Map<Integer,Integer> map){ int tp = arr[b]; arr[b] = arr[a]; map.put(arr[b],b); arr[a] = tp; map.put(arr[a],a); }
56
public static int[][] merge(int[][] intervals) { Arrays.sort(intervals,(k1,k2)->k1[0]==k2[0]?k1[1]-k2[1]:k1[0]-k2[0]); int left = intervals[0][0]; int right = intervals[0][1]; if (intervals.length <= 1){ return intervals; } List<int[]> result = new ArrayList<>(); for (int i = 1; i < intervals.length; i++) { int[] k = intervals[i]; if (k[0] <= right ){ right = Math.max(right,k[1]); if ( i == intervals.length -1){ result.add(new int[]{left,right}); } }else { result.add(new int[]{left,right}); left = k[0]; right = k[1]; if ( i == intervals.length -1){ result.add(new int[]{left,right}); } } } int[][] res = new int[result.size()][2]; for (int i = 0; i < result.size(); i++) { res[i] = result.get(i); } return res; }
57
和上题一模一样 这儿记住数据已经是有序了,所以可以不用排序
179 最大数 这题犯蠢了
重点在于重写比较器的时候可以直接将两个拼接后的字符串做比较就好了
String[] str = new String[nums.length]; for (int i = 0; i < nums.length; i++) { str[i] = String.valueOf(nums[i]); } Arrays.sort(str, new Comparator<String>() { @Override public int compare(String o1, String o2) { return -(o1+o2).compareTo(o2+o1); } }); StringBuilder sn = new StringBuilder(); if (str[0].startsWith("0")){ return "0"; }else { for (String s : str) { sn.append(s); } } return sn.toString();
274 h指数
比较简单的一道题
public static int hIndex(int[] citations) { if (citations.length == 0){ return 0; } Arrays.sort(citations); int max = 0; for (int i = citations.length-1; i >=0; i--) { if (citations.length -i<=citations[i]){ max = Math.max(max,citations.length -i); } } return max; }
853
这个题应该是先分析出题的要求,最终的计算结论就是 按照起点排序,然后如果后面的值到达终点的时间比前一个的时间短,说明是同一车队 (target-position)/speed可以计算出到达终点需要的时间
public static int carFleet(int target, int[] position, int[] speed) { if (position.length <=1){ return position.length; } TreeSet<Integer> tree = new TreeSet<>((a, b)->position[b]-position[a]); for (int i = 0; i < position.length; i++) { tree.add(i); } Integer wz = tree.first(); double sp = (double) (target-position[wz])/speed[wz]; int result = 1; int k = 0; for (Integer key : tree) { if (k == 0){ k++; continue; } double tpSp = (double) (target-position[key])/speed[key]; if (tpSp > sp ){ result++; sp = tpSp; } k++; } return result; }
948 令牌放置 这题一定注意 分数是不能减成负数的
public static int bagOfTokensScore(int[] tokens, int P) { if (tokens.length == 0){ return 0; }else if (tokens.length == 1){ return tokens[0] <=P?1:0; } Arrays.sort(tokens); int left= 0,right = tokens.length-1,result = 0; for (int i = 0; i < tokens.length; i++) { if ( left < right && tokens[left] <= P ){ P-=tokens[left]; left++; result++; } } if (result == 0){ return 0; } int total = P; while (left<right){ total+=tokens[right]; result--; if (total >= tokens[left]){ while (left<right && total >= tokens[left]){ result++; total -= tokens[left]; left++; } }else { return result+1; } right--; } return result; }