LeetCode刷题笔记 简单题优秀题解集合(800~1200)

868. 二进制间距

给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离。

如果没有两个连续的 1,返回 0 。

示例:
输入:22
输出:2
解释:
22 的二进制是 0b10110 。
在 22 的二进制表示中,有三个 1,组成两对连续的 1 。
第一对连续的 1 中,两个 1 之间的距离为 2 。
第二对连续的 1 中,两个 1 之间的距离为 1 。
答案取两个距离之中最大的,也就是 2 。

class Solution {
    public int binaryGap(int N) {
        int max = 0;
        int tmp = 0;
        while((N & 1) != 1) N >>= 1;
        while(N > 0){
            if((N & 1) == 1){
                max = Math.max(max, tmp);
                tmp = 0;
            }
            tmp++;
            N >>= 1;
        }
        return max;
    }
}

884. 两句话中的不常见单词

给定两个句子 A 和 B 。 (句子是一串由空格分隔的单词。每个单词仅由小写字母组成。)

如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的。

返回所有不常用单词的列表。

您可以按任何顺序返回列表。

示例:
输入:A = “this apple is sweet”, B = “this apple is sour”
输出:[“sweet”,“sour”]

总结
“可以理解成拼接字符串A+B,然后返回拼接后的字符串中只出现过一次的单词。”
真是醍醐灌顶

class Solution {
    public String[] uncommonFromSentences(String A, String B) {
        Map<String, Integer> count = new HashMap();
        for (String word: A.split(" "))
            count.put(word, count.getOrDefault(word, 0) + 1);
        for (String word: B.split(" "))
            count.put(word, count.getOrDefault(word, 0) + 1);

        List<String> ans = new LinkedList();
        for (String word: count.keySet())
            if (count.get(word) == 1)
                ans.add(word);

        return ans.toArray(new String[ans.size()]);
    }
}

888. 公平的糖果交换

爱丽丝和鲍勃有不同大小的糖果棒:A[i] 是爱丽丝拥有的第 i 块糖的大小,B[j] 是鲍勃拥有的第 j 块糖的大小。

因为他们是朋友,所以他们想交换一个糖果棒,这样交换后,他们都有相同的糖果总量。(一个人拥有的糖果总量是他们拥有的糖果棒大小的总和。)

返回一个整数数组 ans,其中 ans[0] 是爱丽丝必须交换的糖果棒的大小,ans[1] 是 Bob 必须交换的糖果棒的大小。

如果有多个答案,你可以返回其中任何一个。保证答案存在。

示例:
输入:A = [2], B = [1,3]
输出:[2,3]

Fastest Code
时空转移大法用到了极致

class Solution {
    public int[] fairCandySwap(int[] A, int[] B) {
        int sumA = 0;
        int sumB = 0;
        for(int a : A)
            sumA += a;
        
        int arr[] = new int[100001];
        for(int b : B){
            sumB += b;
            arr[b] = 1;
        }
        int diff = (sumA - sumB) / 2;

        for(int a : A){
            if((a-diff) >= 0 && (a-diff) <= 100000 && arr[a-diff] == 1)
                return new int[]{a, a-diff};
        }
        return null;
    }
}

Demo Code

class Solution {
    public int[] fairCandySwap(int[] A, int[] B) {
        Arrays.sort(A);
        Arrays.sort(B);
        int sumA = 0, sumB = 0;
        for(int i = 0; i < A.length; i++) 
            sumA += A[i];
        for(int i = 0; i < B.length; i++)        
            sumB += B[i];
        int[] res = new int[2];
        int diff = sumB - sumA;
        int le = 0, ri = B.length-1;
        for(int i = 0; i < A.length; i++) {
            for(int j = B.length-1; j >= 0; j--) {
                if(B[j] - A[i] == diff/2) {
                    res[0] = A[i];
                    res[1] = B[j];
                    break;
                }
                if(B[j] - A[i] < diff/2) continue;
            }
        }
        return res;
    }
}

892. 三维形体的表面积

在 N * N 的网格上,我们放置一些 1 * 1 * 1 的立方体。

每个值 v = grid[i][j] 表示 v 个正方体叠放在对应单元格 (i, j) 上。

请你返回最终形体的表面积。

总结
仔细想想还是很简单

class Solution {
	public int surfaceArea(int[][] grid) {
		int result=0;
		for (int i = 0; i < grid.length; i++) {
			for (int j = 0; j < grid[i].length; j++) {
				if (grid[i][j]!=0) 
					//假设每个v=grid[i][j]都是独立的。
					result+=grid[i][j]*4+2;
				//减去面贴在一起的情况
				if (i>0)
					result -= Math.min(grid[i-1][j], grid[i][j])*2;
				if (j>0)
					result -= Math.min(grid[i][j-1], grid[i][j])*2;
			}
		}
		return result;
	}
}

1071. 字符串的最大公因子

对于字符串 S 和 T,只有在 S = T + … + T(T 与自身连接 1 次或多次)时,我们才认定 “T 能除尽 S”。

返回字符串 X,要求满足 X 能除尽 str1 且 X 能除尽 str2。

示例 1:
输入:str1 = “ABCABC”, str2 = “ABC”
输出:“ABC”

示例 2:
输入:str1 = “LEET”, str2 = “CODE”
输出:""

class Solution {
    public String gcdOfStrings(String str1, String str2) {
        if (!(str1 + str2).equals(str2 + str1))
            return "";
        
        return str1.substring(0, gcd(str1.length(), str2.length()));
    }
    public int gcd(int a, int b) {
        return a == 0 ? b : gcd(b % a, a);
    }
}

1103. 分糖果 II

排排坐,分糖果。

我们买了一些糖果 candies,打算把它们分给排好队的 n = num_people 个小朋友。

给第一个小朋友 1 颗糖果,第二个小朋友 2 颗,依此类推,直到给最后一个小朋友 n 颗糖果。

然后,我们再回到队伍的起点,给第一个小朋友 n + 1 颗糖果,第二个小朋友 n + 2 颗,依此类推,直到给最后一个小朋友 2 * n 颗糖果。

重复上述过程(每次都比上一次多给出一颗糖果,当到达队伍终点后再次从队伍起点开始),直到我们分完所有的糖果。注意,就算我们手中的剩下糖果数不够(不比前一次发出的糖果多),这些糖果也会全部发给当前的小朋友。

返回一个长度为 num_people、元素之和为 candies 的数组,以表示糖果的最终分发情况(即 ans[i] 表示第 i 个小朋友分到的糖果数)。

示例:
输入:candies = 7, num_people = 4
输出:[1,2,3,1]
解释:
第一次,ans[0] += 1,数组变为 [1,0,0,0]。
第二次,ans[1] += 2,数组变为 [1,2,0,0]。
第三次,ans[2] += 3,数组变为 [1,2,3,0]。
第四次,ans[3] += 1(因为此时只剩下 1 颗糖果),最终数组变为 [1,2,3,1]。

class Solution {
    public int[] distributeCandies(int candies, int num_people) {
        int[] res = new int[num_people];
        int idx = 0, ex = 1;
        while(candies > 0) {
            if(idx >= num_people) {
                idx = 0;
                ex += num_people;
            }
            res[idx] += Math.min(candies, idx+ex);
            candies = Math.max(0, candies-idx-ex);
            idx++;
        }
        return res;
    }
}
class Solution {
    public int[] distributeCandies(int candies, int num_people) {
        int curr_give = 0;
        int[] res = new int[num_people];
        
        while (candies > 0) {
            res[curr_give % num_people] += Math.min(++curr_give, candies);
            candies -= curr_give;
        }
        
        return res;
    }
}

1128. 等价多米诺骨牌对的数量

给你一个由一些多米诺骨牌组成的列表 dominoes。

如果其中某一张多米诺骨牌可以通过旋转 0 度或 180 度得到另一张多米诺骨牌,我们就认为这两张牌是等价的。

形式上,dominoes[i] = [a, b] 和 dominoes[j] = [c, d] 等价的前提是 ac 且 bd,或是 ad 且 bc。

在 0 <= i < j < dominoes.length 的前提下,找出满足 dominoes[i] 和 dominoes[j] 等价的骨牌对 (i, j) 的数量。

示例:
输入:dominoes = [[1,2],[2,1],[3,4],[5,6]]
输出:1

class Solution {
    public int numEquivDominoPairs(int[][] dominoes) {
        int[] map = new int[100];
        int res = 0;
        for(int i = 0; i < dominoes.length; i++){
            int m = dominoes[i][0];
            int n = dominoes[i][1];
            int k = m > n ? m * 10 + n : n * 10 + m;
            map[k] ++;
        }
        for(int i = 0; i < 100; i++)
            res += map[i] * (map[i] - 1) / 2;   // 取 n 里面有多少个 2

        return res;
    }
}

1170. 比较字符串最小字母出现频次

我们来定义一个函数 f(s),其中传入参数 s 是一个非空字符串;该函数的功能是统计 s 中(按字典序比较)最小字母的出现频次。

例如,若 s = “dcce”,那么 f(s) = 2,因为最小的字母是 “c”,它出现了 2 次。

现在,给你两个字符串数组待查表 queries 和词汇表 words,请你返回一个整数数组 answer 作为答案,其中每个 answer[i] 是满足 f(queries[i]) < f(W) 的词的数目,W 是词汇表 words 中的词。

示例:
输入:queries = [“cbd”], words = [“zaaaz”]
输出:[1]
解释:查询 f(“cbd”) = 1,而 f(“zaaaz”) = 3 所以 f(“cbd”) < f(“zaaaz”)。

class Solution {
    public int[] numSmallerByFrequency(String[] queries, String[] words) {
        int[] num = new int[12];

        for(int i = 0; i < words.length; i++)
            num[helper(words[i])]++;

        for(int i = 9; i >= 0; i--)
            num[i] += num[i+1];

        int[] res = new int[queries.length];
        for(int i = 0; i < queries.length; i++)
            res[i] = num[helper(queries[i])+1];

        return res;
    }
    public int helper(String s){
        int[] map = new int[26];
        for(char c : s.toCharArray())
            map[c-'a']++;

        for(int m : map){
           if(m != 0) return m;
        }
        return 0;
    }
}

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fair-candy-swap
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值