头像

ramihoo




离线:1天前


最近来访(6)
用户头像
北半球陈冠希
用户头像
巷口风铃
用户头像
Pikachuu
用户头像
M4ex
用户头像
相顧無相識


ramihoo
9天前
class Solution {

    static ListNode getTail(ListNode head) {
        while(head.next != null) head = head.next;
        return head;
    }

    public ListNode quickSortList(ListNode head) {
        if(head == null || head.next == null) return head;

        //虚拟链头
        ListNode left = new ListNode(-1);
        ListNode mid = new ListNode(-1);
        ListNode right = new ListNode(-1);

        //链尾
        ListNode ltail = left, mtail = mid, rtail = right;
        //取一个val值。将小于val的放left链表,等于val放mid链表,大于val放right链表
        int val = head.val;
        for(ListNode p = head; p != null; p = p.next) {
            if(p.val < val) ltail = ltail.next = p;
            else if(p.val == val) mtail = mtail.next = p;
            else rtail = rtail.next = p;
        }
        ltail.next = mtail.next = rtail.next = null;

        //递归排左右两边,由于中间是一样的,无需再排序
        //将新的链头加到链尾
        left.next = quickSortList(left.next);
        right.next = quickSortList(right.next);

        //拼接链表
        getTail(left).next = mid.next;
        getTail(left).next = right.next;

        //记录答案链表
        ListNode p = left.next;

        //清理链表
        left = null;
        mid = null;
        right =null;
        return p;
    }
}


活动打卡代码 AcWing 756. 蛇形矩阵

ramihoo
10天前
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[][] res = new int[n][m];
        int[] dx={0,1,0,-1},dy = {1,0,-1,0};
        for (int x = 0, y = 0, d = 0, k = 1; k <= n * m; k++) {
            res[x][y] = k;
            int _x = x + dx[d];
            int _y = y + dy[d];
            if (_x < 0 || _x >= n || _y < 0 || _y >= m || res[_x][_y] != 0) {
                d = (d + 1) % 4;
                _x = x + dx[d];
                _y = y + dy[d];
            }
            x = _x;
            y = _y;
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(res[i][j] + " ");
            }
            System.out.println("");
        }



    }
}





ramihoo
20天前



ramihoo
20天前



ramihoo
20天前



ramihoo
20天前



ramihoo
20天前



ramihoo
21天前

AcWing《算法笔试面试辅导课》拼团优惠!https://www.acwing.com/activity/content/introduction/23/group_buy/149381/




ramihoo
28天前



ramihoo
28天前