LeetCode——日常刷题(一)

简介: LeetCode——日常刷题(一)

目录


第一题: 118. 杨辉三角


第二题:    33. 搜索旋转排序数组


第三题: 81. 搜索旋转排序数组 II


第四题: 153. 寻找旋转排序数组中的最小值


第五题:    70. 爬楼梯


        第六题:     509. 斐波那契数


第七题:  1137. 第 N 个泰波那契数


第八题:     2006. 差的绝对值为 K 的数对数目


第九题:     LCP 01. 猜数字


        第十题:     LCP 06. 拿硬币


第一题:118. 杨辉三角

image.png

代码实现

class Solution {
    public List<List<Integer>> generate(int numRows) {
        //结果
        List<List<Integer>> res=new ArrayList<>();
        //层数
        for(int i=0;i<numRows;i++){
        //定义行
        List<Integer> row=new ArrayList<Integer>();
        //每行的元素数
        for(int j=0;j<=i;j++){ 
        //首尾为0
        if(j==0||i==j){
            row.add(1);
        }else{//取出上一层数据并计算
            row.add(res.get(i-1).get(j-1)+res.get(i-1).get(j));
        }
    }
          res.add(row);
        }
        return res;
    }
}

其他题解:

class Solution {
    public List<List<Integer>> generate(int numRows) {
        //当没有行数时
        if(numRows==0)
        {return null;}
        //顺序表
        List<List<Integer>> res = new ArrayList<>();
        //处理第一行
        List<Integer> firstRow = new ArrayList<>();
        firstRow.add(1);
        res.add(firstRow);
        //从第二行开始处理
        for(int i=1 ; i<numRows ; i++){
            //获取上一行prevRow
            List<Integer> prevRow = res.get(i-1);
            //创建行对象
            List<Integer> curRow = new ArrayList<>();
            curRow.add(1);//处理每一行第一个元素,为1
            //处理每一行中间的元素,杨辉三角里当前元素值等于左上角+右上角
            for(int j=1 ; j< i ; j++){
                int val = prevRow.get(j) + prevRow.get(j-1);
                curRow.add(val);
            }
            //处理每一行的结尾,也都是1
            curRow.add(1);
            res.add(curRow);
        }
        return res;
    }
}

第二题:33. 搜索旋转排序数组

image.png

代码实现

class Solution {
    public int search(int[] nums, int target) {
        for(int i=0;i<nums.length;i++){
            if(nums[i]==target){
                return i;
            }
        }
        return -1;
    }
}

第三题:81. 搜索旋转排序数组 II

image.png

代码实现

class Solution {
    public boolean search(int[] nums, int target) {
        return  exit(nums,target);
    }
    public boolean exit(int[] nums,int target){
            for(int i=0;i<nums.length;i++){
                if(nums[i]==target){
                    return true;
                }
            }
            return false;
        }
}

第四题:153. 寻找旋转排序数组中的最小值

image.png

代码实现

class Solution {
    public int findMin(int[] nums) {
        int min=Integer.MAX_VALUE;
        for(int i=0;i<nums.length;i++){
            if(min>nums[i]){
                min=nums[i];
            }
        }
        return min;
    }
}

第五题:70. 爬楼梯

image.png

代码实现

//动态规划
class Solution {
    public int climbStairs(int n) {
        int[] dp=new int[n+1];
        dp[0]=1;
        dp[1]=1;
        for(int i=2;i<=n;i++){
        dp[i]=dp[i-1]+dp[i-2];
        }
          return dp[n];
    }
}

第六题:509. 斐波那契数

image.png

代码实现

//递归
class Solution {
    public int fib(int n) {
        if(n==0){
            return 0;
        }
        else if(n==1){
            return 1;
        }else {
            return fib(n-1)+fib(n-2);
        }
    }
}
//动态规划
class Solution {
    public int fib(int n) {
         if(n==0){
            return 0;
        }
        int[] dp=new int[n+1];
        dp[0]=0;
        dp[1]=1;
        for(int i=2;i<=n;i++){
        dp[i]=dp[i-1]+dp[i-2];
        }
          return dp[n];
    }
}

第七题:1137. 第 N 个泰波那契数

image.png

代码实现

//动态规划
class Solution {
    public static int tribonacci(int n) {
        if(n==0) return 0;
        if(n==1) return 1;
        int[] dp=new int[n+1];
        dp[0]=0;
        dp[1]=1;
        dp[2]=1;
        for(int i=3;i<=n;i++){
            dp[i]=dp[i-3]+dp[i-2]+dp[i-1];
        }
        return dp[n];
    }
}

第八题:2006. 差的绝对值为 K 的数对数目

image.png

代码实现

class Solution {
    public int countKDifference(int[] nums, int k) {
        int count=0;
        for(int i=0;i<nums.length-1;i++){
            for(int j=i+1;j<nums.length;j++){
                if(Math.abs(nums[i]-nums[j])==k){
                    count++;
                }
            }
        }
        return count;
    }
}

第九题:LCP 01. 猜数字

image.png

代码实现

class Solution {
    public int game(int[] guess, int[] answer) {
        int count=0;
        for(int i=0;i<guess.length;i++){
                if(guess[i]==answer[i]){
                    count++;
                }
        }
        return count;
    }
}

第十题:LCP 06. 拿硬币

image.png

代码实现

class Solution {
    public int minCount(int[] coins) {
        int count=0;
        for(int i=0;i<coins.length;i++){
            if(coins[i]%2==0){
                count+=coins[i]/2;
            }
            else{
                count+=coins[i]/2+1;
            }
        }
        return count;
    }
}
class Solution {
    public int minCount(int[] coins) {
        int count=0;
        for(int i:coins){
            if(i%2==0){
                count+=i/2;
            }
            else{
                count+=i/2+1;
            }
        }
        return count;
    }
}
相关文章
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
41 6
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
35 4
|
1月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
73 2
|
1月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
36 7
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
17 4
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 22. 链表中倒数第k个节点
Leetcode题目"剑指 Offer 22. 链表中倒数第k个节点"的Python解决方案,使用双指针法找到并返回链表中倒数第k个节点。
40 5
|
1月前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
36 3
|
1月前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
15 3
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - II. 从上到下打印二叉树 II
本文提供了一种Python实现方法,用于层次遍历二叉树并按层打印结果,每层节点按从左到右的顺序排列,每层打印到一行。
28 3
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
Leetcode题目"剑指 Offer 21. 调整数组顺序使奇数位于偶数前面"的两种Python解决方案,一种是使用双端队列调整数组顺序,另一种是使用双指针法将奇数移到数组前半部分,偶数移到后半部分。
21 4