day5: 排序算法

排序数组

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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值