力扣刷题记录(1)

剑指 Offer 04. 二维数组中的查找

class Solution {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        if(matrix==null || matrix.length==0){
            return false;
        }
        //行的个数
        int rows= matrix.length;
        //列的个数
        int cols=matrix[0].length;
        int i=0;
        int j=cols-1;
        while(i<rows && j>=0){
            if(matrix[i][j]>target){
                j--;
            }else if(matrix[i][j]<target){
                i++;
            }else {
                return true;
            }
        }
        return false;
    }
}

剑指 Offer 06. 从尾到头打印链表

class Solution {
    public int[] reversePrint(ListNode head) {
        
        ListNode node = head;
        int count = 0;
        //记录链表的个数
        while (node != null) {
            ++count;
            node = node.next;
        }
        int[] nums = new int[count];
        node = head;
        for (int i = count - 1; i >= 0; --i) {
            nums[i] = node.val;
            node = node.next;
        }
        return nums;
    }
}

剑指 Offer 09. 用两个栈实现队列

class CQueue {
    //先从s1倒入s2,取出顶端数字返回,再倒回s1
    Stack<Integer> s1,s2;

    public CQueue() {
        s1=new Stack<>();
        s2=new Stack<>();
    }
    
    public void appendTail(int value) {
        s1.push(value);
    }
    
    public int deleteHead() {
        if(s1.empty()){
            return -1;
        }
        while(!s1.empty()){
            s2.push(s1.peek());
            s1.pop();
        }
        int num=s2.peek();
        s2.pop();
        while(!s2.empty()){
            s1.push(s2.peek());
            s2.pop();
        }
        return num;
    }
}

剑指 Offer 18. 删除链表的节点

class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        //双节点
        ListNode pre=null,cur=head;
        while(cur.val!=val){
            pre=cur;
            cur=cur.next;
        }
        if(cur==head){
            head=head.next;
        }else{
            pre.next=cur.next;
        }
        return head;
    }
}

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

class Solution {
    public int[] exchange(int[] nums) {
        //快排,双指针
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            while (left < right && nums[left] % 2 != 0) {
                left++;
            }
            while (left < right && nums[right] % 2 == 0) {
                right--;
            }
            if (left < right) {
                int temp = nums[left];
                nums[left] = nums[right];
                nums[right] = temp;
            }
        }
        return nums;
    }
}

剑指 Offer 22. 链表中倒数第k个节点

class Solution {
    public ListNode getKthFromEnd(ListNode head, int k) {
        // 快慢指针,先让快指针走k步,然后两个指针同步走,当快指针走到头时,慢指针就是链表倒数第k个节点。
        ListNode frontNode=head, behindNode=head;
        while(frontNode!=null && k>0){
            frontNode=frontNode.next;
            k--;
        }

        while(frontNode !=null){
            frontNode=frontNode.next;
            behindNode=behindNode.next;
        }
    return behindNode;
    }
}

剑指 Offer 24. 反转链表

//第一种方法
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre = null,next=null, cur=head;
        while(cur!=null){
            next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }
        return pre;
    }
}
//第二种方法
//用于反转整个链表
    public void reverse(){
        //先判断当前链表是否为空,若为空则结束运行,若不是,则调用重载得reverse方法完成反转
        if (isEmpt()){
            return;
        }
        reverse(head.next);
    }
    //反转指定的结点,并把反转后的结点返回
    public Node reverse(Node curr){
        if (curr.next==null){
            head.next=curr;
            return curr;
        }
        //递归反转当前结点,反转值就是链表反转后,当前结点的上一个结点
        Node pre = reverse(curr.next);
        //让返回的结点的下一个结点变为当前结点;
        pre.next=curr;
        //当前结点的下一个结点变为null
        curr.next=null;
        return curr;
    }

剑指 Offer 25. 合并两个排序的链表

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode newNode = new ListNode(0), pre = newNode;
        while(l1!=null && l2 != null){
            if(l1.val <= l2.val){
                pre.next=l1;
                pre=pre.next;
                l1=l1.next;
            }else{
                pre.next=l2;
                pre=pre.next;
                l2=l2.next;
            }
        }
        //若其中一个不为空
        if(l1!=null){
            pre.next=l1;
        }
        if(l2!=null){
            pre.next=l2;
        }
        
        return newNode.next;
    }
}

剑指 Offer 29. 顺时针打印矩阵

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        
        int row=matrix.length;
        if(row == 0){
            return new int[0];
        }
        int col=matrix[0].length;
        int[] nums=new int[row*col];
        int index=0;
        int left=0,top=0,right=col-1,bottom=row-1;
        while(true){
            //从左往右走
            for(int i=left;i<=right;i++){
                nums[index++]=matrix[top][i];
            }
            if(++top>bottom){
                break;
            }
            //从上往下走
            for(int i=top;i<=bottom;i++){
                nums[index++]=matrix[i][right];
            }          
            if(--right<left){
                break;
            }

            //从右往左走
            for(int i=right;i>=left;i--){
                nums[index++]=matrix[bottom][i];
            }
            if(--bottom<top){
                break;
            }
            //从下往上走
            for(int i=bottom;i>=top;i--){
                nums[index++]=matrix[i][left];
            }
            if(++left>right){
                break;
            }
        }
        return nums;
        
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值