Algorithmic Xiaobai welcome to this community: https://bbs.csdn.net/forums/hero?category=0

The group learning algorithm team led by the hero, starting from 0, looks forward to your joining.

This blog post is the solution to the exercise of this article. If there are deficiencies, please give more advice: https://blog.csdn.net/WhereIsHeroFrom/article/details/120875166

Question 1:

https://leetcode-cn.com/problems/sum-of-unique-elements/

Topic analysis:

For elements that only appear once, you can first traverse the entire array, and then use a variable to represent whether the elements of the array meet the requirements. If they meet the requirements, we will add them to and. If it is unreasonable, we will perform the following search.

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; } }

Question 2:

https://leetcode-cn.com/problems/first-unique-character-in-a-string/

Topic analysis:

The title says that you only need to consider lowercase letters, so there is an array at the top i: int[] arr = new int[26]; Then we traverse from the beginning. We take out each letter separately, use charAt() to convert them into ascii code values, and assign a value of one. Then we use a loop to traverse the array. If the traversed array value is 1, then the corresponding subscript is returned.

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']++; /*Calculate the number of occurrences of each letter, which "shifts" the ascii/unicode value to 0-25. Therefore, it is more suitable as an array index*/ } for (int i = 0; i < n; i++) { if (arr[s.charAt(i) - 'a'] == 1) { return i; } } return -1; } }

Question 3:

https://leetcode-cn.com/problems/check-if-all-characters-have-equal-number-of-occurrences/

Topic analysis:

Similar to the above problem solving method, I traverse first, store each character in an array, and then make a judgment. See the code comments for the judgment.

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){ /*I can count each character as a good string only if it appears the same number of times, so array [s.charat (0) - 'a']= Array [i] means that the number of times of one character is different from others, And array [i]= 0, that is, this character does exist in your input, so it does not meet the meaning of the question at this time.*/ return false; } } return true; } }

Question 4:

https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/

At the beginning, I didn't think of it. I went to have a look at the problem solution. I mainly didn't grasp the list firmly. I referred to other people's solutions and my own understanding.

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]++;//Convert coordinates to 0~n-1 } for(i=0;i<nums.length;i++){ if(array[i]==0){ res.add(i+1);//Change the coordinates back } } return res; } }

Question 5:

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

Topic analysis:

Using nested loops, compare whether the front and back are equal and meet the coordinate relationship, then add one to the counted variables.

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; } }

Question 6 (more analysis after learning HashMap):

https://leetcode-cn.com/problems/count-good-meals/

Topic analysis:

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

//Official explanation: 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; } }

If you have any questions, please leave a message. Welcome to join the "ten thousand questions" community and work together here.