力扣打卡(5):国庆快乐~~ 又是偷懒的一天。

18 篇文章 0 订阅
16 篇文章 0 订阅

9.30lc

随机刷题的一天 国庆放假了 想出去玩…

所以就写了,每日一题 … 偷懒了

223. 矩形面积 - 力扣(LeetCode) (leetcode-cn.com)

:没什么算法 就是 找值

	class Solution {
    public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {

     
       
       int area1=(ax2-ax1)*(ay2-ay1);
       int area2=(bx2-bx1)*(by2-by1);

       int cx1=Math.min(ax2,bx2)-Math.max(ax1,bx1);
       int cy1=Math.min(by2,ay2)- Math.max(ay1,by1);

       
     

       int area3=Math.max(cy1,0)*Math.max(cx1,0);

       return area1-area3+area2;

    }
}

231. 2 的幂 - 力扣(LeetCode) (leetcode-cn.com)

: 位运算的题 有意思

2的次方 满足 n &n-1 ==0; & 是 全1出1

如果一个数 是n的 2的次方 就满足 他的二进制 只有最高位置 为1 他的n-1整个相反 所以会造成 0的情况

8 -> 1000; 7->0111;   8&7 ==0;
class Solution {
    public boolean isPowerOfTwo(int n) {
                return n>0&&(n&n-1)== 0 ? true:false;
    }
}

191. 位1的个数 - 力扣(LeetCode) (leetcode-cn.com)

暴力遍历:

public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
        int res=0;
        for(int i=0;i<32;i++){
            if((n & (1 << i)) != 0){
                res++;
            }
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值