[算法]剑指offer-easy的题解题思路

1.剑指 Offer 53 - II. 0~n-1中缺失的数字

一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字都在范围0~n-1之内。在范围0~n-1内的n个数字中有且只有一个数字不在该数组中,请找出这个数字。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof

有顺序数组,应该找到关系使用二分查找

class Solution {
    public int missingNumber(int[] nums) {

//有序数组优先考虑二分查找
//注意题意,所有的数都是只出现一次,每个数字范围都是在0-n-1之间,所以0到n-1的数字中,也就是n个数字只有一个不在
//这里面,那么也就是说,如果那个数字开始断掉的话,那么右位数字就是
//假如说是1-n-1的所有数字,少了个0,那么也能找到右位元素0
//因为目标就是找到那个数字缺失,缺失就不连续了呗
        int i = 0, j = nums.length - 1;
        while(i <= j) {
            int m = (i + j) / 2;
            if(nums[m] == m) {
                 i = m + 1;
            }
            else{ j = m - 1};
        }
        return i;
    }
}

2. 双指针方法求链表相交!!

剑指 Offer 52. 两个链表的第一个公共节点

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        
        if (headA == null || headB == null) return null;
        //为什么找两个指向,因为要求要保持原有结构
        ListNode tempA = headA;
        ListNode tempB = headB;
        while(tempA != tempB){

            //这个地方不能使用tempA.next == null,因为这样的话,会有问题,只有为null可以去指向
            if(tempA == null){
               tempA = headB;
            } else{
              tempA = tempA.next;
            }
            if(tempB == null){
               tempB = headA;
            } else{
                tempB = tempB.next;
            }
        }
        return tempA;
    
    }
}

3. 剑指 Offer 57 - II. 和为s的连续正数序列

输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。

序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。

思路:使用滑动窗口  

示例 1:

输入:target = 9
输出:[[2,3,4],[4,5]]
示例 2:

输入:target = 15
输出:[[1,2,3,4,5],[4,5,6],[7,8]]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof
 

class Solution {
    public int[][] findContinuousSequence(int target) {
    //这道题的关键在于,一定是连续正整数序列等于target,并且输出所有的和
    //另一个关键点在于 左边界和右边界以及sum和
    int i = 1; // 滑动窗口的左边界
    int j = 1; // 滑动窗口的右边界
    int sum = 0; // 滑动窗口中数字的和
    List<int[]> res = new ArrayList<>();

    //为什么这边是i<= target/2, 因为这边如果左边窗口都大于了target/2,就不可能有合适的结果的
    //为什么有等于,因为java是向下取整
    while (i <= target / 2) {
        if (sum < target) {
            // 右边界向右移动,这边的sum因为是连续数字相加所以直接可以加上j
            sum += j;
            j++;
        } else if (sum > target) {
            // 左边界向右移动
            sum -= i;
            i++;
        } else {
            // 记录结果
            int[] arr = new int[j-i];
            for (int k = i; k < j; k++) {
                arr[k-i] = k;
            }
            res.add(arr);
            // 左边界向右移动
            sum -= i;
            i++;
        }
    }

    return res.toArray(new int[res.size()][]);
    }
}

4.螺旋打印数组:

思路:上下左右的初始化,做一个L型之后,需要做个if判断在做下边界的L。

          因为最后应该都是上边界的乛

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return new int[0];
        }

        //rows 代表行号  columns代表列
        int rows = matrix.length, columns = matrix[0].length;
        //return的
        int[] order = new int[rows * columns];
        //总的index
        int index = 0;
        //左右上下,但是问题是怎么停下来
        int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
        //停下来的条件,左右相遇 或者上下相遇
        while (left <= right && top <= bottom) {
            //固定行不变,输出列信息
            for (int column = left; column <= right; column++) {
                order[index++] = matrix[top][column];
            }
            //固定列不变,输出行信息
            for (int row = top + 1; row <= bottom; row++) {
                order[index++] = matrix[row][right];
            }
            //第二次做L型的时候需要看看是否符合要求 可能已经到了结束的点了
            if (left < right && top < bottom) {
                for (int column = right - 1; column > left; column--) {
                    order[index++] = matrix[bottom][column];
                }
                for (int row = bottom; row > top; row--) {
                    order[index++] = matrix[row][left];
                }
            }
            left++;
            right--;
            top++;
            bottom--;
        }
        return order;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值