常用算法(java)

这篇博客详细介绍了各种算法在Java中的实现,包括排序(冒泡、快速、选择、插入、二分插入排序)、链表操作(判断相交、找到交点、环判断、环入口、删除重复节点等)、二叉树问题(子结构判断、镜像、层序遍历等)、多线程技术及一些常见的编程技巧题目,如数组中超过一半的数字、连续子数组最大和等。
摘要由CSDN通过智能技术生成

排序

1. 冒泡排序(O(n2))

把最大的沉到最后,两两交换

public void bubbleSort(int[] nums) {
//      外层循环控制排序的轮数
        for (int i = nums.length - 1 ; i > 0; i--) {
            boolean isSorted = true;
//            内层循环每次将未排序序列中最大的数冒泡到最后端
            for (int j = 0; j < i;j++) {
//                升序
                if (nums[j] > nums[j+1]) {
                    swap(nums,j,j+1);
                    isSorted = false;
                }
            }
            if (isSorted) {
                break;
            }
        }
    }

 

2. 快速排序(O(nlogn))

哨兵

public void quickSort(int[] nums,int l, int r) {
        if (l < r) {
            int q = partition(nums,l,r);
            quickSort(nums,l,q-1);
            quickSort(nums,q+1,r);
        }
    }

    private int partition(int[] nums, int l, int r) {
//        临界值,哨兵
        int tmp = nums[l];
        int i = l, j = r;
        while (i != j) {
//            都带等号
            while (i < j && tmp <= nums[j]) {
                j--;
            }
            while (i < j && tmp >= nums[i]) {
                i++;
            }

            if (i < j) {
                swap(nums,i,j);
            }
        }
//        将基准数归位,放在中间位置
        nums[l] = nums[i];
        nums[i] = tmp;

        return i;
    }

 

3. 选择排序(O(n2))

每次选出最小的放到端点处

public void selectSort(int[] nums) {
        for (int i = 0 ; i < nums.length; i++) {
            int index = i;
            for (int j = i+1; j < nums.length;j++) {
//                找出最小的,放在端点处
                if (nums[j] < nums[index]) {
                    index = j;
                }
            }
            swap(nums,i,index);
        }
    }

 

4. 插入排序(O(n2))

把当前的数(nums[i])插入到之前已经排好序的数组中

public void insertSort(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
            int tmp = nums[i];
            int j = i;
            while (j > 0 && nums[j-1] > tmp) {
                nums[j] = nums[j-1];
                j--;
            }
            nums[j] = tmp;
        }
    }

 

5. 二分插入排序(O(nlogn))

public void binaryInsertSort(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
            int tmp = nums[i];
            int l = 0, r = i-1;
//        l <= r
            while (l <= r) {
                int mid = l + (r-l)/2;
                if (nums[mid] < tmp) {
                    l = mid + 1;
                }
                else {
                    r = mid - 1;
                }
            }
//            要插入的位置之后的都后移
            for (int j = i - 1;j >= l; j--) {
                nums[j+1] = nums[j];
            }
//            插入
            nums[l] = tmp;
        }
    }

 

 

链表:

1. 判断相交

相同的尾节点

2. 找到交点

也可以:假设第一个链表长度为len1,第二个问len2,然后找出长度较长的,让长度较长的链表指针向后移动|len1 - len2| (len1-len2的绝对值),然后在开始遍历两个链表,判断节点是否相同即可。

public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode l1 = pHead1, l2 = pHead2;
        while (l1 != l2) {
            l1 = (l1 == null) ? pHead2 : l1.next;
            l2 = (l2 == null) ? pHead1 : l1.next;
        }
        return l1;
    }

3. 判断环

快慢指针

4. 找到环的入口

public ListNode EntryNodeOfLoop(ListNode pHead)
    {
        if (pHead == null || pHead.next == null || pHead.next.next ==null) {
            return null;
        }
        ListNode fast = pHead.next.next;
        ListNode slow = pHead.next;

//        判断有环
        while (slow != fast) {
            if (fast.next != null && fast.next.next != null) {
                fast = fast.next.next;
                slow = slow.next;
            }
            else {
               return null;
            }
        }

        fast = pHead;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        
        return slow;
    }

5. 链表反转

public ListNode ReverseList(ListNode head) {
    ListNode pre = null, next = null, reverseHead = null;
    ListNode pNode = head;

    while (pNode != null) {
        next = pNode.next;
        pNode.next = pre;
        pre = pNode;
        pNode = next;
        if (pNode == null) {
            reverseHead = pre;
        }
    }

    return reverseHead;
}

6. 删除链表中的重复节点

新建一个头结点,防止第一个数就重复;用pre指针保存前一个节点;直接比较pnode.next.val == pnode.val,直到找到最后一个相等的值

public ListNode deleteDuplication(ListNode pHead)
    {
        if (pHead == null || pHead.next == null) {
            return pHead;
        }

        ListNode newHead = new ListNode(-1);
        newHead.next = pHead;
        ListNode pre = newHead;
        ListNode pNode = newHead.next;

        while (pNode != null) {
            if (pNode.next != null && pNode.val == pNode.next.val) {
//                找到最后一个相同的节点
                while (pNode.next != null && pNode.val == pNode.next.val) {
                    pNode = pNode.next;
                }
                pre.next = pNode.next;
                pNode = pNode.next;
            }
            else {
    
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值