leetcode-删除链表节点,设计停车系统,宝石与石头

删除链表节点:

class Solution {
    public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }
}

1603.设计停车系统

class ParkingSystem {

    int big,medium,small;

    public ParkingSystem(int big, int medium, int small) {
        this.big = big;
        this.medium = medium;
        this.small = small;
    }
    
    public boolean addCar(int carType) {
        if(carType == 1){
            if(big == 0) return false;
            else {
                big--;
                return true;
            }
        }else if(carType == 2){
            if(medium == 0) return false;
            else {
                medium--;
                return true;
                
            }
        }else if(carType == 3){
            if(small == 0) return false;
            else {
                small--;
                return true;
                
            }

        }
        return false;

    }
}

修改:

class ParkingSystem {

    int big,medium,small;

    public ParkingSystem(int big, int medium, int small) {
        this.big = big;
        this.medium = medium;
        this.small = small;
    }
    
    public boolean addCar(int carType) {
        if(carType == 1){
            if(big > 0){
                big--;
                return true;
            }
        }else if(carType == 2){
            if(medium > 0){
                medium--;
                return true;
            }
        }else if(carType == 3){
            if(small > 0){
                small--;
                return true;
            }
        }
        return false;
    }
}

写一次return false

771.宝石与石头

方法一:

class Solution {
    public int numJewelsInStones(String jewels, String stones) {
        int jewelscount = 0;
        HashSet<Character> jewelsset = new HashSet<>();
        for(int i = 0; i < jewels.length(); i++){
            char jew = jewels.charAt(i);
            jewelsset.add(jew);
        }
        for(int j = 0; j<stones.length(); j++){
            char sto = stones.charAt(j);
            if(jewelsset.contains(sto)){
                jewelscount++;
            }
        }
        return jewelscount;

    }
}

方法二:

class Solution {
    public int numJewelsInStones(String jewels, String stones) {
        int jewelscount = 0;
        HashSet<Character> jewelsset = new HashSet<>();
        for(int i = 0; i < jewels.length(); i++){
            for(int j = 0; j<stones.length(); j++){
                char jew = jewels.charAt(i);
                char sto = stones.charAt(j);
                if (sto ==jew ){
                    jewelscount++;
                    }
            }
        }
        return jewelscount;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值