力扣刷题记录(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;
        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于力扣刷C++常用操作,我可以给你一些常见的操作和技巧: 1. 使用 STL 容器和算法库:STL(Standard Template Library)是 C++ 标准库中的一个重要组成部分,包含了许多常用的容器和算法。在力扣刷中,使用 STL 可以大大提高代码的效率和可读性。例如,vector 可以用来存储动态数组,sort 可以用来排序等等。 2. 使用 auto 关键字:auto 关键字可以自动推导变量类型,可以减少代码量和提高可读性。例如,auto x = 1; 可以自动推导出 x 的类型为 int。 3. 使用 lambda 表达式:lambda 表达式是 C++11 中引入的一种匿名函数,可以方便地定义一些简单的函数对象。在力扣刷中,使用 lambda 表达式可以简化代码,例如在 sort 函数中自定义比较函数。 4. 使用位运算:位运算是一种高效的运算方式,在力扣刷中经常会用到。例如,左移运算符 << 可以用来计算 2 的幂次方,右移运算符 >> 可以用来除以 2 等等。 5. 使用递归:递归是一种常见的算法思想,在力扣刷中也经常会用到。例如,二叉树的遍历、链表的反转等等。 6. 使用 STL 中的 priority_queue:priority_queue 是 STL 中的一个容器,可以用来实现堆。在力扣刷中,使用 priority_queue 可以方便地实现一些需要维护最大值或最小值的算法。 7. 使用 STL 中的 unordered_map:unordered_map 是 STL 中的一个容器,可以用来实现哈希表。在力扣刷中,使用 unordered_map 可以方便地实现一些需要快速查找和插入的算法。 8. 使用 STL 中的 string:string 是 STL 中的一个容器,可以用来存储字符串。在力扣刷中,使用 string 可以方便地处理字符串相关的问。 9. 注意边界条件:在力扣刷中,边界条件往往是解决问的关键。需要仔细分析目,考虑各种边界情况,避免出现错误。 10. 注意时间复杂度:在力扣刷中,时间复杂度往往是评判代码优劣的重要指标。需要仔细分析算法的时间复杂度,并尽可能优化代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值