LeetCode刷题(简单)笔记(2)

5、宝石与石头

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/jewels-and-stones

给定字符串J代表石头中宝石的类型,和字符串S代表你拥有的石头。S中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。J中的字母不重复,J和S中的所有字符都是字母。字母区分大小写,因此"a"和"A"是不同类型的石头。

 

示例 1:

输入: J = "aA", S = "aAAbbbb"

输出: 3

 

代码:

class Solution {

        public int numJewelsInStones(String jewels, String stones) {

        int count = 0;

        for (int i = 0; i < stones.length(); i++) {

            for (int j = 0; j < jewels.length(); j++) {

                if (stones.charAt(i) == jewels.charAt(j)) {

                    count++;

                    break;

                }

            }

        }

        return count;

    }

 

}

 

6、最富有客户的资产总量

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/richest-customer-wealth

给你一个 m x n 的整数网格 accounts ,其中 accounts[i][j]是第i位客户在第j家银行托管的资产数量。返回最富有客户所拥有的资产总量。客户的资产总量就是他们在各家银行托管的资产数量之和。最富有客户就是资产总量最大的客户。

 

示例 1:

输入:accounts = [[1,2,3],[3,2,1]]

输出:6

解释:

第 1 位客户的资产总量 = 1 + 2 + 3 = 6

第 2 位客户的资产总量 = 3 + 2 + 1 = 6

两位客户都是最富有的,资产总量都是 6 ,所以返回 6 。

 

代码:

class Solution {

    public int maximumWealth(int[][] accounts) {

        int count=0;

        int acount[]=new int[accounts.length];

        for(int i=0;i<accounts.length;i++){

            for(int j=0;j<accounts[i].length;j++){

                acount[i]+=accounts[i][j];

            }

        }

        int max=acount[0];

        for(int i=1;i<acount.length;i++){

            if(acount[i]>max){

                max=acount[i];

            }

        }

        return max;

    }

}

 

7、好数对的数目

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/number-of-good-pairs

给你一个整数数组 nums。如果一组数字(i,j)满足 nums[i]==nums[j]且i <j,就可以认为这是一组好数对。返回好数对的数目。

 

示例 1:

输入:nums = [1,2,3,1,1,3]

输出:4

解释:有 4 组好数对,分别是 (0,3), (0,4), (3,4), (2,5) ,下标从 0 开始。

代码:

class Solution {

    public int numIdenticalPairs(int[] nums) {

        int count=0;

        for(int i=0;i<nums.length;i++){

            for(int j=i+1;j<nums.length;j++){

                if(nums[j]==nums[i]){

                    count++;

                }

            }

        }

        return count;

 

    }

}

 

8、设计停车系统

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/design-parking-system

请你给一个停车场设计一个停车系统。停车场总共有三种不同大小的车位:大,中和小,每种尺寸分别有固定数目的车位。

请你实现ParkingSystem类:

ParkingSystem(int big, int medium, int small) 初始化ParkingSystem类,三个参数分别对应每种停车位的数目。

bool addCar(int carType)检查是否有carType对应的停车位。carType有三种类型:大,中,小,分别用数字1,2和3表示。一辆车只能停在carType对应尺寸的停车位中。如果没有空车位,请返回false,否则将该车停入车位并返回true。

 

示例 1:

输入:

["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]

[[1, 1, 0], [1], [2], [3], [1]]

输出:

[null, true, true, false, false]

解释:

ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);

parkingSystem.addCar(1); // 返回 true ,因为有 1 个空的大车位

parkingSystem.addCar(2); // 返回 true ,因为有 1 个空的中车位

parkingSystem.addCar(3); // 返回 false ,因为没有空的小车位

parkingSystem.addCar(1); // 返回 false ,因为没有空的大车位,唯一一个大车位已经被占据了。

代码:

class ParkingSystem {

 

    private  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) {

        switch (carType){

            case 1://大型车

                if(this.big>0){

                    this.big--;

                    return true;

                }

                break;

            case 2://中型车

                if(this.medium>0){

                    this.medium--;

                    return true;

                }

                break;

            case 3://小型车

                if(this.small>0){

                    this.small--;

                    return true;

                }

                break;

            default:

                return  false;

        }

        return false;

    }

}

 

/**

 * Your ParkingSystem object will be instantiated and called as such:

 * ParkingSystem obj = new ParkingSystem(big, medium, small);

 * boolean param_1 = obj.addCar(carType);

 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值