【题解】《算法零基础100例》(第5例) 计数法(java版)

算法小白欢迎加入此社区:https://bbs.csdn.net/forums/hero?category=0
由英雄大佬带领的抱团学算法队伍,从0开始,期待你的加入。

本博文是对此文章习题所作的题解,如有不足,请多指教:https://blog.csdn.net/WhereIsHeroFrom/article/details/120875166

第一题
https://leetcode-cn.com/problems/sum-of-unique-elements/
在这里插入图片描述

题目分析:
只求出现一次的元素,可以先遍历整个数组,然后用一个变量去代表数组的元素是否满足要求,如果满足,我们把他加入到和里面,如果不合理,那么就进行下面的查找。

class Solution {
    public int sumOfUnique(int[] nums) {
        int sum = 0;
        int i,j;
        int temp = 0;
        for (i = 0; i < nums.length; i++) {
            for (j = 0; j < nums.length; j++) {
                if (nums[i] == nums[j] && i != j){
                    temp = 1;
                }
            }
            if(temp == 0){
                sum+=nums[i];
            }
                temp = 0;
        }
        return sum;
    }
}

第二题
https://leetcode-cn.com/problems/first-unique-character-in-a-string/
在这里插入图片描述

题目分析:
题目说了,你只需考虑小写的字母,所以顶i有一个数组:int[] arr = new int[26]; 然后我们从头开始遍历,我们把每一个字母都单独取出来,使用charAt(),将他们转换为ascii码值,并且赋值赠一,然后在用一个循环,去遍历数组,如果遍历出的数组值为1,那么就返回对应的下标。

class Solution {
        public int firstUniqChar(String s) {
            int[] arr = new int[26];
            int n = s.length();
            for (int i = 0; i < n; i++) {
                arr[s.charAt(i) - 'a']++;
 /*计算每个字母的发生次数,它"转移"了ascii/unicode值,使其值达到0-25。因此,更适合作为阵列索引*/
            }
            for (int i = 0; i < n; i++) {
                if (arr[s.charAt(i) - 'a'] == 1) {
                    return i;
                }
            }
            return -1;
        }
}


第三题
https://leetcode-cn.com/problems/check-if-all-characters-have-equal-number-of-occurrences/
在这里插入图片描述

题目分析:
与上面的题解法类似,我先遍历,把每一个字符都存入到一个数组里面,然后进行判断,判断见代码注释。

class Solution {
    public boolean areOccurrencesEqual(String s) {
        int[] array=new int[26];
        int i,j;
        for(i=0; i<s.length(); i++){
            array[s.charAt(i)-'a']++;
        }
        for(i=0; i<26; i++){
            if(array[s.charAt(0)-'a']!=array[i] && array[i]!=0){
/*我每一个字符出现的次数一样,我才可以算作是好字符串,所以array[s.charAt(0)-'a']!=array[i]就代表有一个字符的次数与其他的不一样,
并且array[i]!=0,也就是这个字符确实存在于你的输入里面,那么此时不符合题意。*/
                return false;
                }
            }
        return true;
    }
}

第四题:
https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/
在这里插入图片描述

刚开始没有想出来,去看了一下题解,主要对列表掌握不牢固,参考别人的解法加上自己理解。

class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> res = new ArrayList<>();
        int[] array = new int[nums.length+1];
        int i;
        for(i=0; i<nums.length; i++){
            array[nums[i]-1]++;//把坐标转换到0~n-1
        }
        for(i=0;i<nums.length;i++){
            if(array[i]==0){
                res.add(i+1);//把坐标再变回去
            }
        }
        return res;
    }
}

第五题:
https://leetcode-cn.com/problems/number-of-good-pairs/
在这里插入图片描述

题目分析:
利用嵌套循环,比较前后是否相等,相等且满足坐标的关系,那么就让计数的变量加一。

class Solution {
    public int numIdenticalPairs(int[] nums) {
          int number = 0;
          int i,j;
          for(i = 0 ;i < nums.length; i++){
              for(j = 1; j < nums.length; j++){
                  if(nums[i] == nums[j] && i<j){
                      number++;  
                  }
              }
          }
          return number;
    }
}

第六题(学完HashMap回来更解析):
https://leetcode-cn.com/problems/count-good-meals/
在这里插入图片描述

题目分析:
https://leetcode-cn.com/problems/count-good-meals/solution/da-can-ji-shu-by-leetcode-solution-fvg9/

//官方题解:
class Solution {
    public int countPairs(int[] deliciousness) {
        final int MOD = 1000000007;
        int maxVal = 0;
        for (int val : deliciousness) {
            maxVal = Math.max(maxVal, val);
        }
        int maxSum = maxVal * 2;
        int pairs = 0;
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        int n = deliciousness.length;
        for (int i = 0; i < n; i++) {
            int val = deliciousness[i];
            for (int sum = 1; sum <= maxSum; sum <<= 1) {
                int count = map.getOrDefault(sum - val, 0);
                pairs = (pairs + count) % MOD;
            }
            map.put(val, map.getOrDefault(val, 0) + 1);
        }
        return pairs;
    }
}

有问题欢迎留言,欢迎加入“万人千题”社区,在这里一起努力。

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码的xiaolang

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值