(题解)《算法零基础100讲》(第38讲) 排序进阶 - 希尔排序

1、排序数组(leetcode912

尝试了一下希尔排序

class Solution {
    public int[] sortArray(int[] nums) {
        sher(nums);
        return nums;
    }
    public void sher(int []nums)
    {
        int len = nums.length;
        for(int gap = len / 2 ; gap > 0 ; gap /=2)
        {
            for(int i = gap ; i < len ; i ++)
            {
                int temp = nums[i];
                int j;
                for( j = i ; j >= gap ; j -= gap)
                {
                    if(temp < nums[j - gap])
                    {
                        nums[j] = nums[j - gap];
                    }
                    else
                    {
                        break;
                    }
                }
                nums[j] = temp;
            }
        }
    }
}

2、多数元素(leetcode169

还是向往日一样重新敲了一遍,使用HashMap,更好的做法就是使用找出众数,就是大于n/2对应下标的数,因为你一个数出现的次数要大于n / 2 ,下标就是n / 2 的那个数。所以可以先排序(采用了希尔排序),然后找对应下标。

class Solution {
    public int majorityElement(int[] nums) {
        int len = nums.length;
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0 ; i < len ; i ++)
        {
            if(map.containsKey(nums[i]))
            {
                int num1 = map.get(nums[i]);
                map.put(nums[i], ++num1);
                if(map.get(nums[i]) > len / 2)return nums[i];
            }
            else map.put(nums[i], 1);
        }
        return nums[0];
    }
}
class Solution {
    public int majorityElement(int[] nums) {
        sher(nums);
        return nums[nums.length / 2];
    }
    public void sher(int []nums)
    {
        int len = nums.length;
        for(int gap = len / 2 ; gap > 0 ; gap /=2)
        {
            for(int i = gap ; i < len ; i ++)
            {
                int temp = nums[i];
                int j;
                for( j = i ; j >= gap ; j -= gap)
                {
                    if(temp < nums[j - gap])
                    {
                        nums[j] = nums[j - gap];
                    }
                    else
                    {
                        break;
                    }
                }
                nums[j] = temp;
            }
        }
    }
}

3、存在重复元素(leetcode217

可以使用哈希表记录,然后遍历哈希表看是否有大于等于2的元素。又或者可以使用排序,然后再遍历一遍看相邻元素是否相等。

class Solution {
    public boolean containsDuplicate(int[] nums) {
        Set<Integer> set = new HashSet<>();
        int len = nums.length;
        for(int i = 0 ; i < len ; i ++)
        {
            if(set.contains(nums[i]))return true;
            else set.add(nums[i]);
        }
        return false;

    }
}

4、最大间距(leetcode164

排序后遍历找出相邻元素的最大差值

class Solution {
        public  int maximumGap(int[] nums) {
        int len = nums.length, max = 0;
        if(len < 2)return 0;
        sher(nums);
        for(int i = 0, j = i + 1 ; i < len && j < len; i ++, j ++)
        {
            int num = Math.abs(nums[i] - nums[j]);
            if(num > max )max = num;
        }
        return max;
    }
    public void sher(int []nums)
    {
        int len = nums.length;
        int i,j,gap;
        for(gap = len / 2 ; gap > 0 ; gap /= 2)
        {
            for( i = gap ; i < len ; i ++)
            {
                int tem = nums[i];
                for( j = i ; j >= gap ; j -= gap)
                {
                    if(tem <= nums[j - gap])nums[j] = nums[j - gap];
                    else
                    {
                        break;
                    }
                }
                nums[j] = tem;
            }
        }
    }
}

5、按奇偶排序数组(leetcode905

可以使用改进的快排,将奇数和偶数分边。

class Solution {
    public int[] sortArrayByParity(int[] nums)
    {
        int len = nums.length;
        int l = -1 , r = len;
        while( l < r)
        {
            do l ++;while(l < len && nums[l] % 2 == 0);
            do r --;while(r >= 0 && nums[r] % 2 != 0) ;
            if(l < r)
            {
                int num = nums[l];
                nums[l] = nums[r];
                nums[r] = num;
            }
        }
        return nums;
    }
}

6、三角形的最大周长(leetcode976

将数组排序,然后从后往前找三个数满足 a + b > c的,就直接有结果了,因为排序后,a + c >b , b + c > a这两个条件是肯定成立的,所以不用判断了。

class Solution {
    public int largestPerimeter(int[] nums) {
        int len = nums.length;
        sher(nums);
        // Arrays.sort(nums);
        int max = 0;
        for(int i = len - 1 ; i >= 0 ; i --)
        {
            if(i - 1 >=0 && i -2 >= 0)
            if(nums[i - 1] + nums[i - 2] > nums[i])return nums[i - 1] + nums[i - 2] + nums[i];
        }
        return 0;
    }
    public void sher(int []nums)
    {
        int len = nums.length;
        int i, j, gap;
        for(gap = len / 2 ; gap > 0 ; gap /= 2)
        {
            for(i = gap ; i < len ; i ++)
            {
                int tem = nums[i];
                for( j = i ; j >= gap ; j -= gap) {
                    if (tem < nums[j - gap]) nums[j] = nums[j - gap];
                    else break;
                }
                nums[j] = tem;
            }
        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值