算法练习

数组中重复的数

题目描述
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

public class Solution {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    这里要特别注意~返回任意重复的一个,赋值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        if(numbers==null&length<=0)return false;
        for(int i=0;i<length;i++){
            while(numbers[i]!=i){
                if(numbers[i]==numbers[numbers[i]]){
                    duplication[0]=numbers[i];
                    return true;
                }
                swap(numbers,i,numbers[i]);
            }
        }return false;

    }

    private void swap(int[] nums,int i,int j){
        int temp=nums[i];
        nums[i]=nums[j];
        nums[j]=temp;
    }
}

二维数组中的查找

题目描述
给定一个二维数组,其每一行从左到右递增排序,从上到下也是递增排序。给定一个数,判断这个数是否在该二维数组中。
解法:根据每行从左到右边递增,每列从上到下递增的关系,从右上角开始判断。

public boolean Find(int target, int[][] matrix) {
    if (matrix == null || matrix.length == 0 || matrix[0].length == 0)
        return false;
    int rows = matrix.length, cols = matrix[0].length;
    int r = 0, c = cols - 1; // 从右上角开始
    while (r <= rows - 1 && c >= 0) {
        if (target == matrix[r][c])
            return true;
        else if (target > matrix[r][c])
            r++;
        else
            c--;
    }
    return false;
}

丑数

描述
设计一个算法,找出只含素因子2,3,5 的第 n 小的数。

public class Solution {
    /**
     * @param n: An integer
     * @return: return a  integer as description.
     */
    public int nthUglyNumber(int n) {
        // write your code here
        Queue<Long> choushu = new PriorityQueue<>();
        if (n == 1)
            return 1;
        choushu.offer((long) 1);
        int[] factor = {2, 3, 5};
        for (int i = 2; i <= n; i++) {
            long num = choushu.poll();
            for (int f : factor) {
                long tmp = f * num;
                if (!choushu.contains(tmp))
                    choushu.offer(tmp);
            }
        }
        return choushu.poll().intValue();
    }
}

第k大数

描述
在数组中找到第 k 大的元素。

public class Solution {
    /**
     * @param n:    An integer
     * @param nums: An array
     * @return: the Kth largest element
     */
    public int kthLargestElement(int n, int[] nums) {
        // write your code here
        int length = nums.length;
        int temp = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < length; j++) {
                if (nums[i] < nums[j]) {
                    temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }
            }
        }
        return nums[n - 1];
    }
}

快速排序算法

/**
 * @author lzr
 * @date 2020/7/1 08:35:08
 * @description 快速排序算法
 */
@Component
public class QuickSort implements IAlgorithm {
    public void sort(int[] nums) {
        quickSort(nums, 0, nums.length - 1);
    }

    public void quickSort(int[] nums, int begin, int end) {
        if (begin < end) {
            int temp = nums[begin];
            int i = begin;
            int j = end;

            while (i != j) {
                while (i < j && nums[j] <= temp) {// 从右向左找第一个小于temp的数
                    j--;
                }
                if (i < j) {
                    nums[i++] = nums[j];
                }
                while (i < j && nums[i] > temp) {// 从左向右找第一个大于等于temp的数
                    i++;
                }
                if (i < j) {
                    nums[j--] = nums[i];
                }
            }
            nums[i] = temp;
            quickSort(nums, begin, i - 1);// 递归调用
            quickSort(nums, j + 1, end);
        }
    }
}

两数之和 哈希表解法

class Solution {
    public int[] twoSum(int[] nums, int target) {
        //哈希表解法
        //把元素
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (map.containsKey(complement)) {
                return new int[]{i, map.get(complement)};
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }
}

快速排序二

/**
 * @author lzr
 * @date 2020/9/24 14:19:44
 * @description
 */
public class Test {
    public void quicksort(int[] array, int begin, int end) {
        if (begin >= end) {
            return;
        }
        int pivotIndex = partition(array, begin, end);
        quicksort(array, begin, pivotIndex - 1);
        quicksort(array, pivotIndex + 1, end);
    }

    public int partition(int[] array, int begin, int end) {
        int k = array[begin];
        int b = begin;
        int e = end;
        while (b < e) {
            while (b < e && array[e] >= k) {
                e--;
            }
            if (b < e) {
                array[b] = array[e];
                b++;
            }
            while (b < e && array[b] <= k) {
                b++;
            }
            if (b < e) {
                array[e] = array[b];
                e--;
            }
        }
        array[b] = k;
        return b;
    }

    public static void main(String[] args) {
        Test test = new Test();
        int[] array = {4, 6, 3, 5, 654, 8, 3, 2, 6, 5, 64564, 214, 61, 34};
        test.quicksort(array, 0, array.length - 1);
        for (int i : array) {
            System.out.print(i + "-");
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值