leetcode刷题记录

小白一个,每天打卡,遇到不会的记录一下!

目录

1.比特位计数

2.俄罗斯套娃信封问题

3.用栈实现队列


1.比特位计数

给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。

示例 1:

输入: 2
输出: [0,1,1]
示例 2:

输入: 5
输出: [0,1,1,2,1,2]

思路

对于所有的数字,只有两类:

奇数:二进制表示中,奇数一定比前面那个偶数多一个 1,因为多的就是最低位的 1。
          举例: 
         0 = 0       1 = 1
         2 = 10      3 = 11
偶数:二进制表示中,偶数中 1 的个数一定和除以 2 之后的那个数一样多。因为最低位是 0,除以 2 就是右移一位,也就是把那个 0 抹掉而已,所以 1 的个数是不变的。
           举例:
          2 = 10       4 = 100       8 = 1000
          3 = 11       6 = 110       12 = 1100
另外,0 的 1 个数为 0,于是就可以根据奇偶性开始遍历计算了。

作者:duadua
链接:https://leetcode-cn.com/problems/counting-bits/solution/hen-qing-xi-de-si-lu-by-duadua/
来源:力扣(LeetCode)

class Solution {
    public int[] countBits(int num) {
        int []result = new int[num+1];
        result[0]=0;
        for(int i=0;i<=num;i++){
            if(i%2==0){
                result[i]=result[i/2];
            }else{
                result[i]=result[i-1]+1;
            }

        }
        return result;

    }
}

2.俄罗斯套娃信封问题

给定一些标记了宽度和高度的信封,宽度和高度以整数对形式 (w, h) 出现。当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。

请计算最多能有多少个信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。

说明:
不允许旋转信封。

示例:

输入: envelopes = [[5,4],[6,4],[6,7],[2,3]]
输出: 3 
解释: 最多信封的个数为 3, 组合为: [2,3] => [5,4] => [6,7]。

参考结题思路:

https://leetcode-cn.com/problems/longest-increasing-subsequence/solution/dong-tai-gui-hua-she-ji-fang-fa-zhi-pai-you-xi-jia/

https://leetcode-cn.com/problems/russian-doll-envelopes/solution/zui-chang-di-zeng-zi-xu-lie-kuo-zhan-dao-er-wei-er/

代码

class Solution {

    public int maxEnvelopes(int[][] envelopes) {

        int n = envelopes.length;
        //默认是升序,但我们对w按升序,当w相等按降序
        //将小的放在前面,大的放在后面。
        // 例如当返回负数的时候,表明第一个数应该排在第二个数的上面,也就是位置不变。
        Arrays.sort(envelopes,new Comparator<int[]>(){
            public int compare(int []a,int []b){
                if(a[0]==b[0]){
                    return b[1]-a[1];
                }else{
                   return a[0]-b[0];
                }
            }
        });
        //将h单独提取出来,作为一个数字,找到最长上升子序列
        int []h =new int[n];
        for(int i=0;i<n;i++){
            h[i]=envelopes[i][1];
        }

        return maxlen(h);

    }


    public int maxlen(int []h){
        int n=h.length;
        int []m = new int [n];
        // for(int i=0;i<n;i++){
        //     max[i]=1;
        // }
        // base case:dp 数组全都初始化为 1     
        Arrays.fill(m, 1);
        //动态规划
        for(int j=0;j<n;j++){
            for(int k =0;k<j;k++){
                if(h[j]>h[k]){
                    m[j]=Math.max(m[j],m[k]+1);
                }
                
            }
        }

        int res=0;
        for(int i=0;i<n;i++){
            res=Math.max(res,m[i]);
        }

        return res;

    }
}

3.用栈实现队列

请你仅使用两个栈实现先入先出队列。队列应当支持一般队列的支持的所有操作(push、pop、peek、empty):

实现 MyQueue 类:

  • void push(int x) 将元素 x 推到队列的末尾
  • int pop() 从队列的开头移除并返回元素
  • int peek() 返回队列开头的元素
  • boolean empty() 如果队列为空,返回 true ;否则,返回 false

说明:

你只能使用标准的栈操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-queue-using-stacks

思路

https://leetcode-cn.com/problems/implement-queue-using-stacks/solution/shi-yong-liang-ge-dui-lie-yong-shi-0ms-b-34bm/

https://leetcode-cn.com/problems/implement-queue-using-stacks/solution/yong-zhan-shi-xian-dui-lie-by-leetcode/

代码

 

class MyQueue {

    /** Initialize your data structure here. */
    public Stack<Integer> s1;
    public Stack<Integer> s2;
    public int front;
    public MyQueue() {
           s1=new Stack();
           s2=new Stack();
    }
    
    /** Push element x to the back of queue. */
    public void push(int x) {
        if(s1.empty()){
            front =x;
        }
        s1.push(x);

    }
    
    /** Removes the element from in front of queue and returns that element. */
    public int pop() {
        if(s2.empty()){
            while(!s1.empty()){
                s2.push(s1.pop());
            }

        }
        int s = s2.pop();
        return s;

    }
    
    /** Get the front element. */
    public int peek() {
        if(s2.empty()){
            return front;
        }else{
            return s2.peek();
        }

    }
    
    /** Returns whether the queue is empty. */
    public boolean empty() {
        // if(s1.empty()&&s2.empty()){
        //     return true;
        // }else{
        //     return false;
        // }
           return s1.empty()&&s2.empty();
    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */

4.  整数反转

给你一个 32 位的有符号整数 x ,返回 x 中每位上的数字反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 [−231,  231 − 1] ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)。
 

示例 1:

输入:x = 123
输出:321
示例 2:

输入:x = -123
输出:-321
示例 3:

输入:x = 120
输出:21
示例 4:

输入:x = 0
输出:0

提示:

-231 <= x <= 231 - 1
通过次数606,255提交次数1,732,444

解题思路

https://leetcode-cn.com/problems/reverse-integer/solution/tu-jie-7-zheng-shu-fan-zhuan-by-wang_ni_ma/

代码

class Solution {
    public int reverse(int x) {
        int res = 0;
        while(x!=0) {
            //每次取末尾数字
            int tmp = x%10;
            //判断是否 大于 最大32位整数
            if (res>214748364 || (res==214748364 && tmp>7)) {
                return 0;
            }
            //判断是否 小于 最小32位整数
            if (res<-214748364 || (res==-214748364 && tmp<-8)) {
                return 0;
            }
            res = res*10 + tmp;
            x /= 10;
        }
        return res;
    }
}	

 

Vivado2023是一款集成开发环境软件,用于设计和验证FPGA(现场可编程门阵列)和可编程逻辑器件。对于使用Vivado2023的用户来说,license是必不可少的。 Vivado2023的license是一种许可证,用于授权用户合法使用该软件。许可证分为多种类型,包括评估许可证、开发许可证和节点许可证等。每种许可证都有不同的使用条件和功能。 评估许可证是免费提供的,让用户可以在一段时间内试用Vivado2023的全部功能。用户可以使用这个许可证来了解软件的性能和特点,对于初学者和小规模项目来说是一个很好的选择。但是,使用评估许可证的用户在使用期限过后需要购买正式的许可证才能继续使用软件。 开发许可证是付费的,可以永久使用Vivado2023的全部功能。这种许可证适用于需要长期使用Vivado2023进行开发的用户,通常是专业的FPGA设计师或工程师。购买开发许可证可以享受Vivado2023的技术支持和更新服务,确保软件始终保持最新的版本和功能。 节点许可证是用于多设备或分布式设计的许可证,可以在多个计算机上安装Vivado2023,并共享使用。节点许可证适用于大规模项目或需要多个处理节点进行设计的用户,可以提高工作效率和资源利用率。 总之,Vivado2023 license是用户在使用Vivado2023时必须考虑的问题。用户可以根据自己的需求选择合适的许可证类型,以便获取最佳的软件使用体验。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值