11.16 - 11.22

11.16日

406.根据身高重建队列

class Solution {
    public int[][] reconstructQueue(int[][] people) {
    // 按照身高降序排序,如果身高一样,则按照排在这个人前面的人数升序排序
        Arrays.sort(people, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0] == o2[0]) {
                    return o1[1] - o2[1];
                } else {
                    return o2[0] - o1[0];
                }
            }
        });
        List<int[]> list = new ArrayList<>();
        for(int[] i : people) {
            list.add(i[1], i);
        }
        int[][] ans = list.toArray(new int[list.size()][]);
        return ans;
    }
}

11.17日

1030.距离顺序排序矩阵单元格

class Solution {
    public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {
        int[][] ret = new int[R * C][2];
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                ret[i * C + j] = new int[]{i, j};
            }
        }
        Arrays.sort(ret, (int[] a, int[] b) -> {
            return (abs(a[0] - r0) + abs(a[1] - c0)) - (abs(b[0] - r0) + abs(b[1] - c0));
        });
        return ret;
    }

    public int abs(int n) {
        return n >= 0 ? n : n * -1;
    }
}

11.18日

134.加油站

class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int now = 0, all = 0, start = 0;
        for(int i = 0; i < gas.length; i ++) {
            now = now + gas[i] - cost[i];
            all = all + gas[i] - cost[i];
            if(now < 0) {
                start = i + 1;
                now = 0;
            }
        }
        return all >= 0 ? start : -1;
    }
}

11.19日

283.移动零

class Solution {
    public void moveZeroes(int[] nums) {
        if(nums == null || nums.length < 2) {
            return;
        }
        int index = 0;
        for(int i = 0; i < nums.length; i ++) {
            if (nums[i] != 0) {
                nums[index] = nums[i];
                index++;
            }
        }
        for(int i = index; i < nums.length; i ++) {
            nums[i] = 0;
        }
    }
}

11.20日

147.对链表进行插入排序

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode insertionSortList(ListNode head) {
        ListNode node = new ListNode(0), pre;
        node.next = head;
        
        while(head != null && head.next != null) {
            if(head.val <= head.next.val) {
                head = head.next;
                continue;
            }
            pre = node;
            
            while (pre.next.val < head.next.val){
                pre = pre.next;
            }
            
            ListNode temp = head.next;
            head.next = temp.next;
            temp.next = pre.next;
            pre.next = temp;
        }
        return node.next;
    }
}

11.22日

242.有效的字母异位词

class Solution {
    public boolean isAnagram(String s, String t) {
        char[] ss = s.toCharArray();
        char[] tt = t.toCharArray();
        if(ss.length != tt.length) {
            return false;
        }
        int[] arrs = new int[26];
        int[] arrt = new int[26];
        for(int i = 0; i < ss.length; i ++) {
            arrs[ss[i] - 'a'] ++;
            arrt[tt[i] - 'a'] ++;
        }
        for(int i = 0; i < 26; i ++) {
            if(arrs[i] != arrt[i]) {
                return false;
            }
        }
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值