二、 总持续时间可被 60 整除的歌曲(Weekly Contest 128)

题目描述:
在这里插入图片描述
一开始想成了用map来实现,但是一想不对劲,奈何只能用笨方法来实现了
代码如下:

class Solution {
    public int numPairsDivisibleBy60(int[] time) {
       for (int i = 0; i < time.length; i++) {
			time[i] = time[i] % 60;
		}
		int result = 0;
		for (int i = 0; i < time.length; i++) {
			for (int j =  i + 1; j < time.length; j++) {
				if((time[i] + time[j]) % 60 == 0){
					result ++;
				}
			}
		}
		return result; 
    }
}

再看看别人写的代码
好像也是这个思路

class Solution {
    public int numPairsDivisibleBy60(int[] time) {
        int count=0;
        if(time.length==1){
            return count;
        }
        for(int i=0;i<time.length-1;i++){
            for(int j=i+1;j<time.length;j++){
                if((time[i]+time[j])%60==0){
                    count++;
                }
            }
        }
        return count;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34446716/article/details/88633069
今日推荐