✅ 1207 独一无二的出现次数

https://leetcode-cn.com/problems/unique-number-of-occurrences

描述

给你一个整数数组 arr,请你帮忙统计数组中每个数的出现次数。

如果每个数的出现次数都是独一无二的,就返回 true;否则返回 false。

  1. 示例 1
  2. 输入:arr = [1,2,2,1,1,3]
  3. 输出:true
  4. 解释:在该数组中,1 出现了 3 次,2 出现了 2 次,3 只出现了 1 次。没有两个数的出现次数相同。
  5. 示例 2
  6. 输入:arr = [1,2]
  7. 输出:false
  8. 示例 3
  9. 输入:arr = [-3,0,1,-3,1,1,1,-3,10,0]
  10. 输出:true
  11. 来源:力扣(LeetCode
  12. 链接:https://leetcode-cn.com/problems/unique-number-of-occurrences
  13. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解答

我的思路: 用 一个book 数组,标记每一个数字出现的次数,最后尝试把book 数组每个 元素加入set,不能加入(因为已经加入了)就返回false,否则返回true。(另外,如果不知道set 有没有 add(重复)的 警告返回的话,就 return set.size() == book.length;

  1. class Solution {
  2. public boolean uniqueOccurrences(int[] arr) {
  3. int book[] = new int[arr.length];
  4. for (int i : arr) {
  5. book[i]++;
  6. }
  7. Set<Integer> set = new HashSet<>();
  8. for(int j : book){
  9. set.add(j);
  10. }
  11. return set.size() == book.length;
  12. }
  13. }
  14. //failed

实际上,上面这个办法并不行,因为,book length 并不是你想的那么小,而是和arr。length 一样大。我们转而使用hashMap

java hashMap api

put

map.put("zhang", "31");//存放键值对

get

map.get("zhang");//获取值

containsKey

map.containsKey("zhang");//键中是否包含这个数据

remove

map.remove("zhang");//从键值中删除

map.keySet()

  1. for (String key : map.keySet()) {
  2. System.out.println(key);
  3. }

map.values()

  1. for (String values : map.values()) {
  2. System.out.println(values);
  3. }

map.entrySet()

  1. for (Entry<String, String> entry : map.entrySet()) {
  2. String key = entry.getKey();
  3. String value = entry.getValue();
  4. System.out.println(key + "," + value);
  5. }

java my final solution:

  1. class Solution {
  2. public boolean uniqueOccurrences(int[] arr) {
  3. HashMap<Integer, Integer> book = new HashMap<>();
  4. for (int i : arr) {
  5. int val = 0;
  6. if(book.get(i) != null) {
  7. val = book.get(i) + 1;
  8. }
  9. book.put(i, val);
  10. }
  11. Set<Integer> set = new HashSet<>();
  12. for(Integer j : book.values()){
  13. set.add(j);
  14. }
  15. return set.size() == book.size();
  16. }
  17. }
  18. 执行用时 :
  19. 3 ms
  20. , 在所有 Java 提交中击败了
  21. 80.64%
  22. 的用户
  23. 内存消耗 :
  24. 35.9 MB
  25. , 在所有 Java 提交中击败了
  26. 19.21%
  27. 的用户

c other's solution, 用两个 数组 统计

  1. #include <stdlib.h>
  2. bool uniqueOccurrences(int* arr, int arrSize){
  3. int a[2001] = {0}; int b[2001] = {0}; int i;
  4. /* step1:统计每个数字出现的次数到数组a中 */
  5. for (i = 0; i < arrSize; i++) {
  6. if (arr[i] < 0) {
  7. a[abs(arr[i]) + 1000]++;
  8. } else {
  9. a[arr[i]]++;
  10. }
  11. }
  12. /* step2: 在step1数组中 再按出现次数 统计到b,tt 把 a 统计到b 中 */
  13. for (i = 0; i < 2000; i++) {
  14. if (a[i] != 0) {
  15. b[a[i]]++;
  16. }
  17. }
  18. /* step3 :在b中出现超过1次的 即表示有出现相同次数 */
  19. for (i = 0; i < 2000; i++) {
  20. if (b[i] > 1) {
  21. return false;
  22. }
  23. }
  24. return true;
  25. }

✅ 476 数字的补数

https://leetcode-cn.com/problems/number-complement

用时15min

描述

给定一个正整数,输出它的补数。补数是对该数的二进制表示取反。

注意:

给定的整数保证在32位带符号整数的范围内。

你可以假定二进制数不包含前导零位。

  1. 示例 1:
  2. 输入: 5
  3. 输出: 2
  4. 解释: 5的二进制表示为101(没有前导零位),其补数为010。所以你需要输出2
  5. 示例 2:
  6. 输入: 1
  7. 输出: 0
  8. 解释: 1的二进制表示为1(没有前导零位),其补数为0。所以你需要输出0

解答

估计这种题目,用c 的位操作非常方便。java 也有位操作

c 他人 cped!

  1. //不懂二进制的看了好久。。就是num转为二进制,并看看总共x位二进制,然后与这全部的x位都是1的二进制进行异或运算(相同为0,不同为1)。就得出结果。
  2. int temp = num, c = 0;
  3. while(temp > 0){
  4. //根据判断条件
  5. //二进制右移并赋值给temp,
  6. temp >>= 1;
  7. //二进制左移之后结果+1 赋值给c
  8. c = (c << 1) + 1;
  9. }
  10. return num ^ c;// 这里 ^ 是 异或的意思
  11. /*执行用时 :
  12. 0 ms
  13. , 在所有 C 提交中击败了
  14. 100.00%
  15. 的用户
  16. 内存消耗 :
  17. 6.7 MB
  18. , 在所有 C 提交中击败了
  19. 91.73%
  20. 的用户*/

他人jav 解答

  1. class Solution {
  2. public int findComplement(int num) {
  3. int res = 0;
  4. int i = 0;
  5. while(num>0){
  6. // 实际上就是把num 上的这一位去反,然后乘以 2 的 i 次方
  7. res += ((num&1)==0?1:0)*(1<<i++);// 这个nb,
  8. num = num >> 1;
  9. }
  10. return res;
  11. }
  12. }
  13. /*执行用时 :
  14. 1 ms
  15. , 在所有 Java 提交中击败了
  16. 97.67%
  17. 的用户
  18. 内存消耗 :
  19. 33.3 MB
  20. , 在所有 Java 提交中击败了
  21. 34.34%
  22. 的用户*/

✅ 1237 找出给定方程的正整数解

https://leetcode-cn.com/problems/find-positive-integer-solution-for-a-given-equation

描述

  1. 给出一个函数  f(x, y) 和一个目标结果 z,请你计算方程 f(x,y) ==所有可能的正整数 数对 x y
  2. 给定函数是严格单调的,也就是说:
  3. f(x, y) < f(x + 1, y)
  4. f(x, y) < f(x, y + 1)
  5. 函数接口定义如下:
  6. interface CustomFunction {
  7. public:
  8.   // Returns positive integer f(x, y) for any given positive integer x and y.
  9.   int f(int x, int y);
  10. };
  11. 如果你想自定义测试,你可以输入整数 function_id 和一个目标结果 z 作为输入,其中 function_id 表示一个隐藏函数列表中的一个函数编号,题目只会告诉你列表中的 2 个函数。 (如下 示例 1/2 
  12. 你可以将满足条件的 结果数对 按任意顺序返回。
  13.  
  14. 示例 1
  15. 输入:function_id = 1, z = 5
  16. 输出:[[1,4],[2,3],[3,2],[4,1]]
  17. 解释:function_id = 1 表示 f(x, y) = x + y
  18. 示例 2
  19. 输入:function_id = 2, z = 5
  20. 输出:[[1,5],[5,1]]
  21. 解释:function_id = 2 表示 f(x, y) = x * y
  22.  
  23. 提示:
  24. 1 <= function_id <= 9
  25. 1 <= z <= 100
  26. 题目保证 f(x, y) ==的解处于 1 <= x, y <= 1000 的范围内。
  27. 1 <= x, y <= 1000 的前提下,题目保证 f(x, y) 是一个 32 位有符号整数。

解答

这些答案让我读懂了题目:

jav 双指针

  1. public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
  2. List<List<Integer>> res = new LinkedList<>();
  3. int start = 1, end = 1000;
  4. while (start <= 1000 && end >= 1) {
  5. int r = customfunction.f(start, end);
  6. if (r == z) {
  7. List<Integer> tmp = new LinkedList<>();
  8. tmp.add(start);
  9. tmp.add(end);
  10. res.add(tmp);
  11. start++;
  12. end--;
  13. } else if (r > z)
  14. end--;
  15. else/* r<z */
  16. start++;
  17. }
  18. return res;
  19. }

jav 解释这道题的目的,暴力法

  1. class Solution {
  2. public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
  3. List<List<Integer>> result = new ArrayList<List<Integer>>();
  4. for(int i=1;i<=z;i++){
  5. for(int j=1;j<=z;j++){
  6. if(customfunction.f(i,j)==z) {
  7. List<Integer> temp = new ArrayList<Integer>();
  8. temp.add(i);temp.add(j);
  9. result.add(temp);
  10. }
  11. }
  12. }
  13. return result;
  14. }
  15. }

py3 similar like above jav

  1. class Solution:
  2. def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
  3. ans = []
  4. x = 1
  5. y = z
  6. while x <= z and y > 0:
  7. tmp = customfunction.f(x, y)
  8. if tmp > z:
  9. y -= 1
  10. elif tmp < z:
  11. x += 1
  12. elif tmp == z:
  13. ans.append([x,y])
  14. x +=1
  15. y -=1
  16. return ans

my py3 cped above:

  1. class Solution:
  2. def findSolution(self, customfunction: 'CustomFunction', z: int) -> List[List[int]]:
  3. ans = []
  4. x = 1
  5. y = z
  6. while x <= z and y > 0:
  7. tmp = customfunction.f(x, y)
  8. if tmp > z:
  9. y -= 1
  10. elif tmp < z:
  11. x += 1
  12. else:
  13. ans.append([x,y])
  14. x += 1
  15. y -= 1
  16. return ans
  17. '''执行用时 :
  18. 24 ms
  19. , 在所有 Python3 提交中击败了
  20. 99.78%
  21. 的用户
  22. 内存消耗 :
  23. 13 MB
  24. , 在所有 Python3 提交中击败了
  25. 52.73%
  26. 的用户'''

✅ 908 最小差值 I

https://leetcode-cn.com/problems/smallest-range-i

描述

  1. 给定一个整数数组 A,对于每个整数 A[i],我们可以选择任意 x 满足 -K <= x <= K,并将 x 加到 A[i] 中。
  2. 在此过程之后,我们得到一些数组 B
  3. 返回的最大值和的最小值之间可能存在的最小差值。
  4.  
  5. 示例 1
  6. 输入:A = [1], K = 0
  7. 输出:0
  8. 解释:B = [1]
  9. 示例 2
  10. 输入:A = [0,10], K = 2
  11. 输出:6
  12. 解释:B = [2,8]
  13. 示例 3
  14. 输入:A = [1,3,6], K = 3
  15. 输出:0
  16. 解释:B = [3,3,3] B = [4,4,4]

解答

出题人语文及格了没!

翔一样的问题描述

得到数组B?数组B是谁,怎么得到的,这题目是哪个国学大师出的,能用凡人的组织方式解释一哈么 --!


py3 当你语文过关后的解答:

  1. class Solution:
  2. def smallestRangeI(self, A: List[int], K: int) -> int:
  3. if max(A) - min(A) > K * 2:
  4. return max(A) - min(A) - K * 2
  5. else:
  6. return 0

他人java, 多了很多边界检查的代码

  1. public int smallestRangeI(int[] A, int k) {
  2. //边界检查的代码
  3. if(A==null){
  4. return 0;
  5. }
  6. //边界检查的代码
  7. int len = A.length;
  8. if(len==1){
  9. return 0;
  10. }
  11. //core 1 排序,能帮助找大小,这么多 也就是py 里的一行 :max(list)
  12. Arrays.sort(A);
  13. int max = A[len-1];
  14. int min = A[0];
  15. // core2 类似上面py 的思路
  16. if(max -k>min+k){
  17. return max-min-2*k;
  18. }
  19. return 0;
  20. }

leetcode 0210的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. 每天进步一点点------Allegro PCB

    Allegro PCB 1.如何在allegro中取消花焊盘(十字焊盘) set up->design parameter ->shape->edit global dynamic ...

  2. Django项目报错: 禁止访问(403),CSRF验证失败,相应中断

    如果想要取消表单的CSRF防护,可以在模板上删除{% csrf_token %}, 并且在相应的视图函数中添加装饰器@csrf_exempt, 代码如下: from django.views.deco ...

  3. C#学习笔记之泛型

    泛型的作用和约定 提高性能 拆箱和装箱 从值类型转换为引用类型为装箱,把引用类型转换为值类型为拆箱 装箱和拆箱很容易使用,但是性能损失比较大,尤其是遍历许多项的时候. List<T>不使用 ...

  4. 「题解」「CF468D」树中的配对

    目录 题目大意 思路 源代码 本博客除代码之外,来自 skylee 大佬. 题目大意 一棵\(n(n\le10^5)\)个编号为\(1\sim n\)的点的带边权的树,求一个排列\(p_{1\sim ...

  5. cookie、session以及中间件

    cookie cookie是保存客户端浏览器上的键值对,是服务端设置在客户端浏览器上的键值对,也就意味着浏览器其实可以拒绝服务端的'命令',默认情况下浏览器都是直接让服务端设置键值对 设置cookie ...

  6. ASA升级

    1.开启TFTP server,并且保证设备和TFTP server可达.2.上传镜像文件到ASA:ciscoasa# copy tftp: disk0: >>>>拷贝镜像到A ...

  7. Go同步等待组/互斥锁/读写锁

    1. 临界资源 package main import ( "fmt" "time" ) func main() { /* 临界资源: */ a := 1 go ...

  8. C语言特点有哪些?

    C语言的特点 : 1.简洁紧凑.灵活方便 C语言一共只有32个关键字,9种控制语句,程序书写自由,主要用小写字母表示.它把高级语言的基本结构和语句与低级语言的实用性结合起来. C 语言可以象汇编语言一 ...

  9. web项目获取路径

    Java获取路径的各种方法:  (1).request.getRealPath("/"); //不推荐使用获取工程的根路径 (2).request.getRealPath(requ ...

  10. Angular的启动过程

    我们知道由命令 ng new project-name,cli将会创建一个基础的angular应用,我们是可以直接运行起来一个应用.这归功与cli已经给我们创建好了一个根模块AppModule,而根模 ...