1748. Sum of unique elements
Title Description
Original question link
Train of thought
The value of each element in the num array is in [1100], so we can create one with a size of 101
An array arr[101] of spaces is used to store the number of each value in nums. The value of nums is used as the subscript of arr, and then a loop is used to traverse nums to initialize arr. Finally, the subscript of the value of arr array is accumulated (the subscript of arr array is the value in nums)
Source code analysis
class Solution { public: int sumOfUnique(vector<int>& nums) { int sum=0; int arr[101]={};//Initialize each element of the array to 0 for(int i=0;i<nums.size();++i)// { ++arr[nums[i]];//Record the number of each number in nums } for(int i=0;i<101;++i) { if(arr[i]==1)//When the number is 1, it is the number we need to accumulate sum+=i; } return sum; } };
387. First unique character in string
Title Description
Original question link
387. First unique character in string
Train of thought
The title only requires lowercase letters to be tested, so we create an array of 26 spaces, which is similar to the previous title. The only thing to note is that when counting, each letter needs to be reduced by 97, such as' a '- 97 = = 0, so the number of' a 'is stored in the position with subscript 0, and the same is true for other initials
Source code analysis
class Solution { public: int firstUniqChar(string s) { int pos=-1;//The initialization position is - 1, which is convenient. If it is not found, it will return - 1 int arr[26]={};//26 spaces are requested to store the number of 26 lowercase letters, and the initialization number is 0 for(int i=0;i<s.size();++i) { ++arr[s[i]-97];//Circularly calculate the number of each letter. The ACKII code value of 'a' is 97, so - 97 is required for each character } for(int i=0;i<s.size();++i) { if(arr[s[i]-97]==1)//When the number of letters is 1, the position is returned { pos=i; return pos; } } return pos;//If there is no qualified, return - 1 } };
1941. Check whether all characters appear the same number of times
Title Description
Original question link
1941. Check whether all characters appear the same number of times
Train of thought
This question is basically the same as the previous one. The subtle changes can be seen from the source code analysis
Source code analysis
class Solution { public: bool areOccurrencesEqual(string s) { int arr[26]={};//26 spaces are requested to store the number of 26 lowercase letters, and the initialization number is 0 for(int i=0;i<s.size();++i) { ++arr[s[i]-97];//Circularly calculate the number of each letter. The ACKII code value of 'a' is 97, so - 97 is required for each character } int count=arr[s[0]-97];//Take the number of the first letter as the standard for(int i=0;i<s.size();++i) { if(arr[s[i]-97]!=count)//When the number of one letter is not equal to the standard, false is returned return false; } return true; } };
448. Find the missing numbers in all arrays
Title Description
Original question link
448. Find the missing numbers in all arrays
Train of thought
We found that the maximum value of array nums counts the length of nums. Therefore, we can take nums itself as a hash table, corresponding the array elements to the index position plus N, and traverse the array after n. if the array element value is less than or equal to N, it means that the array subscript value does not exist, that is, the number that disappears
Source code analysis
class Solution { public: vector<int> findDisappearedNumbers(vector<int>& nums) { int n=nums.size(); for(int i=0;i<n;++i) { //The interval of nums value is [1,n]. Only by subtracting it by 1 can it match its own subscript //The remainder n is to restore the number that has been added to n int x=(nums[i]-1)%n; nums[x]+=n; } vector<int> ret;//Open up an array to store the number to be returned for(int i=0;i<n;++i) { if(nums[i]<=n) //What is needed is the number that does not appear in the range, that is, the number less than or equal to n ret.push_back(i+1); } return ret; } };
1512. Number of good pairs
Title Description
Original question link
Train of thought
The only thing to note is the definition and inference of good number pairs. If n numbers are the same, the number of good number pairs is n*(n-1)/2
Source code analysis
class Solution { public: int numIdenticalPairs(vector<int>& nums) { int count=0; int arr[100]={};//Create a hash array and initialize to 0 for(int i=0;i<nums.size();++i) { ++arr[nums[i]-1];//Initialize hash table } for(int i=0;i<100;++i) { count+=arr[i]*(arr[i]-1)/2;//Count, if a number appears n times, its number of good pairs is n*(n-1)/2 } return count; } };