day5: 排序算法

day5: 排序算法,第1张

排序数组
func sortArray(nums []int) []int {
    quickSort(nums, 0, len(nums)-1)
    return nums
}

func quickSort(nums []int, start, end int) {
    if start < end {
        head, tail := start, end
        x := nums[start]
        for head < tail {
            for head < tail && nums[tail] >= x {
                tail--
            }
            if head < tail {
                nums[head] = nums[tail]
                head++
            }

            for head < tail && nums[head] < x {
                head++
            }
            if head < tail {
                nums[tail] = nums[head]
                tail--
            }
        }
        nums[head] = x
        quickSort(nums, start, head-1)
        quickSort(nums, head+1, end)
    }
}
多数元素
func majorityElement(nums []int) int {
   // 摩尔枚举
   moor_num := 1
   index_num := nums[0]
   for i:=1;i<len(nums);i++ {
        if nums[i] == index_num {
           moor_num++
        } else {
            moor_num--
            if moor_num == 0 {
                index_num = nums[i]
                moor_num = 1
            }
        }
   }
   return index_num
}
存在重复元素
func containsDuplicate(nums []int) bool {
    mm := make(map[int]bool)

    for _, value := range nums {
        if _, ok := mm[value]; ok {
            return mm[value]
        }
        mm[value] = true
    }
    return false
}
按奇偶排序数组
func sortArrayByParity(nums []int) []int {
    head, tail := 0, len(nums)-1

    for head < tail {
        if nums[head]%2 == 1 && nums[tail]%2 == 0 {
            nums[head] = nums[head]^nums[tail]
            nums[tail] = nums[head]^nums[tail]
            nums[head] = nums[head]^nums[tail]
        }
        if nums[head]%2 == 0 {
            head++
        }
        if nums[tail]%2 == 1 {
            tail--
        }
    }
    return nums
}
三角形最大周长
class Solution {
public:
    int largestPerimeter(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        for (int i=nums.size()-1; i>=2;i--) {
            if (nums[i-2] + nums[i-1] > nums[i]) {
                return nums[i-2]+nums[i-1]+nums[i];
            }
        }
        return 0;
    }
};
救生艇
class Solution {
public:
    int numRescueBoats(vector<int>& people, int limit) {
        int start= 0, end = people.size()-1;
        int count=0;
        sort(people.begin(), people.end());
        while(start <= end){
            if (people[start] + people[end] > limit) {
                end--;
            }else {
                start++;
                end--;
            }
            count++;
        }
        return count;
    }
};
最大间距
func maximumGap(nums []int) int {
    if len(nums) < 2 {
        return 0
    }
    sort.Ints(nums);
    i, j := 0, 1
    ans := -1
    for i < len(nums) && j < len(nums) {
        ans = max(ans, nums[j]-nums[i])
        i++
        j++
    }
    return ans
}

func max(x , y int) int {
    if x < y {
        return y
    }
    return x
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/568755.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存