力扣题:统计数组中的元素-9.27

力扣题-9.27

[力扣刷题攻略] Re:从零开始的力扣刷题生活

力扣题1:645. 错误的集合

解题思想:遍历数组对数组中出现的数字进行统计,出现两次的即为重复的数字,没有出现的是丢失的数字

力扣题:统计数组中的元素-9.27_第1张图片

class Solution(object):
    def findErrorNums(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        count = [0 for i in range(len(nums))]
        for i in range(0,len(nums)):
            count[nums[i]-1] = count[nums[i]-1]+1
        lost = 0
        repeat = 0
        for i in range(len(count)):
            if count[i]==2:
                repeat = i+1
            if count[i]==0:
                lost = i+1
        return [repeat,lost]
class Solution {
public:
    vector<int> findErrorNums(vector<int>& nums) {
        int repeat = 0,lost = 0;
        vector<int> count(nums.size(), 0);
        for(int i =0;i<nums.size();i++){
            count[nums[i]-1]++;
        }
        for(int i =0;i<count.size();i++){
            if(count[i]==0){
                lost = i+1;
            }
            if(count[i]==2){
                repeat = i+1;
            }
        }
        vector<int> result = {repeat, lost};
        return result;
    }
};

力扣题2:697. 数组的度

解题思想:遍历数组用字典记录该数字的相关信息,其中字典的key为数字,value为[重复的次数,开始序号,结束序号],最后遍历字典找到最大重复次数中最短的字串即可

力扣题:统计数组中的元素-9.27_第2张图片

class Solution(object):
    def findShortestSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        result_dict={}
        for i in range(len(nums)):
            if nums[i] not in result_dict.keys():
                temp = [1,i,i]
                result_dict[nums[i]]=temp
            else:
                temp = result_dict.get(nums[i])
                temp[0] = temp[0]+1
                temp[2] = i
                result_dict[nums[i]]=temp
        max_de = 0
        result_len =0
        for i in result_dict.values():
            if i[0]>max_de:
                result_len = i[2]-i[1]+1
                max_de = i[0]
            if i[0]==max_de:
                result_len =min(result_len,i[2]-i[1]+1)
        return result_len
class Solution {
public:
    int findShortestSubArray(vector<int>& nums) {
        std::unordered_map<int, std::vector<int>> result_dict;
        for (int i = 0; i < nums.size(); i++) {
            if (result_dict.find(nums[i]) == result_dict.end()) {
                std::vector<int> temp = {1, i, i};
                result_dict[nums[i]] = temp;
            }
            else {
                std::vector<int>& temp = result_dict[nums[i]];
                temp[0]++;
                temp[2] = i;
                result_dict[nums[i]] = temp;
            }
        }
        int max_de = 0;
        int result_len = 0;

        for (auto& entry : result_dict) {
            std::vector<int>& i = entry.second;
            if (i[0] > max_de) {
                result_len = i[2] - i[1] + 1;
                max_de = i[0];
            } 
            else if (i[0] == max_de) {
                result_len = std::min(result_len, i[2] - i[1] + 1);
            }
        }
        return result_len;
    }
};

力扣题3:448. 找到所有数组中消失的数字

解题思想:与第一题类似,用一个数组记录下数字出现的次数即可

力扣题:统计数组中的元素-9.27_第3张图片

class Solution(object):
    def findDisappearedNumbers(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        count = [0 for i in range(len(nums))]
        for i in range(0,len(nums)):
            count[nums[i]-1] = count[nums[i]-1]+1
        result=[]
        for i in range(len(count)):
            if count[i]==0:
                result.append(i+1)
        return result
class Solution {
public:
    vector<int> findDisappearedNumbers(vector<int>& nums) {
        std::vector<int> count(nums.size(), 0);
        for (int i = 0; i < nums.size(); ++i) {
            count[nums[i] - 1] = count[nums[i] - 1] + 1;
        }
        
        std::vector<int> result;
        for (int i = 0; i < count.size(); ++i) {
            if (count[i] == 0) {
                result.push_back(i + 1);
            }
        }
        return result;
    }
};

你可能感兴趣的:(leetcode)