【九日集训】

  1. 排序数组
 vector<int> sortArray(vector<int>& nums) {
        int n=nums.size();
        for(int i=0;i<n;i++){
            bool flag=false;
            for(int j=n-2;j>=i;j--){
                if(nums[j]>nums[j+1]){
                    swap(nums[j],nums[j+1]);
                    flag=true;
                }
            }
            if(flag==false)break;
        }
        return nums;
    }
  1. 多数元素
  int majorityElement(vector<int>& nums) {
        unordered_map<int, int> counts;
        int majority = 0, cnt = 0;
        for (int num: nums) {
            ++counts[num];
            if (counts[num] > cnt) {
                majority = num;
                cnt = counts[num];
            }
        }
        return majority;
    }
  1. 存在重复元素
   bool containsDuplicate(vector<int>& nums) {
        unordered_set<int> s;
        for (int x: nums) {
            if (s.find(x) != s.end()) {
                return true;
            }
            s.insert(x);
        }
        return false;
    }
  1. 最大间距
int maximumGap(vector<int>& nums) {
        int n = nums.size();
        if (n < 2) {
            return 0;
        }
        int exp = 1;
        vector<int> buf(n);
        int maxVal = *max_element(nums.begin(), nums.end());

        while (maxVal >= exp) {
            vector<int> cnt(10);
            for (int i = 0; i < n; i++) {
                int digit = (nums[i] / exp) % 10;
                cnt[digit]++;
            }
            for (int i = 1; i < 10; i++) {
                cnt[i] += cnt[i - 1];
            }
            for (int i = n - 1; i >= 0; i--) {
                int digit = (nums[i] / exp) % 10;
                buf[cnt[digit] - 1] = nums[i];
                cnt[digit]--;
            }
            copy(buf.begin(), buf.end(), nums.begin());
            exp *= 10;
        }

        int ret = 0;
        for (int i = 1; i < n; i++) {
            ret = max(ret, nums[i] - nums[i - 1]);
        }
        return ret;
    }
  1. 按奇偶排序数组
  vector<int> sortArrayByParity(vector<int>& nums) 
    {
        int n=nums.size();
        vector<int> res(n);
        int fast=0,slow=0;
        while(fast<=n-1)
        {
            if(nums[slow]%2!=1)slow=fast;
            if(nums[slow]%2-nums[fast]%2>0)
            {
                swap(nums[slow],nums[fast]);
                slow++;
            }
            fast++;
        }
        return nums;
    }
  1. 最小时间差
 int findMinDifference(vector<string>& timePoints) {
        int n = timePoints.size();
        int times[n];
        for (int i = 0; i < n; ++i)
        {
            // times[i] = stoi(timePoints[i].substr(0,2))*60 + stoi(timePoints[i].substr(3,2));
            times[i] = ((timePoints[i][0]-'0')*10 + (timePoints[i][1]-'0'))*60 + 
                    (timePoints[i][3]-'0')*10 + (timePoints[i][4]-'0');
        }

        sort(times, times+n);
        int res = INT_MAX;
        for (int i = 0; i < n-1; ++i)
        {
            res = min(res, times[i+1]-times[i]);
        }
        // 最后一个还要和第一个比较
        res = min(res, 24*60+times[0] - times[n-1]);

        return res;
    }
  1. 三角形的最大周长
 int largestPerimeter(vector<int>& A) {
        sort(A.begin(), A.end());
        for (int i = (int)A.size() - 1; i >= 2; --i) {
            if (A[i - 2] + A[i - 1] > A[i]) {
                return A[i - 2] + A[i - 1] + A[i];
            }
        }
        return 0;
    }
  1. 救生艇
int numRescueBoats(vector<int> &people, int limit) {
        int ans = 0;
        sort(people.begin(), people.end());
        int light = 0, heavy = people.size() - 1;
        while (light <= heavy) {
            if (people[light] + people[heavy] > limit) {
                --heavy;
            } else {
                ++light;
                --heavy;
            }
            ++ans;
        }
        return ans;
    }

你可能感兴趣的:(leetcode,排序算法,算法)