算法总结

package com.daojia.jz.cashierconfig.service.deposit;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author lishujiang
 * @date 2020/5/21 9:15
 */
public class TreeNode {

    public static class Solution1 {
        public boolean hasCycle(ListNode head) {
            Set<ListNode> set = new HashSet();
            while (head != null) {
                if (!set.add(head)) {
                    return true;
                }
                head = head.next;
            }
            return false;
        }
    }

    public ListNode reverseList(ListNode head) {

        ListNode newHead = null;
        while (head != null) {
            ListNode next = head.next;
            head.next = newHead;
            newHead = head;
            head = next;
        }
        return newHead;
    }

    class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public static void midOrderRe(BTreeNode biTree) {//中序遍历递归实现
        if (biTree == null)
            return;
        else {
            midOrderRe(biTree.left);
            System.out.println(biTree.value);
            midOrderRe(biTree.right);
        }
    }


    static class BTreeNode//节点结构
    {
        int value;
        BTreeNode left;
        BTreeNode right;

        BTreeNode(int value) {
            this.value = value;
        }

        BTreeNode(int value, BTreeNode left, BTreeNode right) {
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

    static class Solution {
        // 无重复最长子串
        public int lengthOfLongestSubstring(String s) {
            int n = s.length(), ans = 0;
            Map<Character, Integer> map = new HashMap<>();
            for (int end = 0, start = 0; end < n; end++) {
                char alpha = s.charAt(end);
                if (map.containsKey(alpha)) {
                    start = Math.max(map.get(alpha), start);
                }
                ans = Math.max(ans, end - start + 1);
                map.put(s.charAt(end), end + 1);
            }
            return ans;
        }

        // 升序最长子串
        public int subArrayLength(int[] array) {
            int len = array.length;
            int res = 0;
            Map<Integer, Integer> map = new HashMap<>();
            for (int start = 0, end = 0; end < len; end++) {
                int temp = array[end];
                if (end > 0 && temp < array[end - 1]) {
                    start = Math.max(start, map.get(array[end - 1]));
                }
                map.put(temp, end + 1);
                res = Math.max(res, end - start + 1);

            }
            return res;
        }

        // 快速排序
        public void quickSort(int[] arr, int left, int right) {
            int i, j, base, temp;
            if (left > right) {
                return;
            }
            i = left;
            j = right;
            //base 就是基准位
            base = arr[left];
            while (i < j) {
                //先看右边,依次往左递减
                while (base <= arr[j] && i < j) {
                    j--;
                }
                //再看左边,依次往右递增
                while (base >= arr[i] && i < j) {
                    i++;
                }
                //如果满足条件则交换
                if (i < j) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
            //最后将基准为与i和j相等位置的数字交换
            arr[left] = arr[i];
            arr[i] = base;
            //递归调用左半数组
            quickSort(arr, left, j - 1);
            //递归调用右半数组
            quickSort(arr, j + 1, right);
        }

        // topK
        public int topKthLargest(int[] arr, int k) {
            if (arr == null || arr.length < k) {
                return 0;
            }
            // 逆排序
            return topK(arr, 0, arr.length - 1, k);
        }

        public int topK(int[] arr, int left, int right, int k) {
            int i, j, base, temp;
            i = left;
            j = right;
            base = arr[left];
            while (i < j) {
                //先看右边,依次往左递减
                while (base >= arr[j] && i < j) {
                    j--;
                }
                //再看左边,依次往右递增
                while (base <= arr[i] && i < j) {
                    i++;
                }
                //如果满足条件则交换
                if (i < j) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
            if (k - 1 == i) {
                return base;
            }
            arr[left] = arr[i];
            arr[i] = base;
            if (k - 1 > i) {
                return topK(arr, j + 1, right, k);
            } else {
                return topK(arr, left, j - 1, k);
            }
        }

        /// <summary>
        /// 二分法查找
        /// </summary>
        /// <param name="array">目标数组(已经排序好了)</param>
        /// <param name="a">查找的数</param>
        /// <returns>目标数的索引</returns>
        public int BinarySearch(int[] array, int T) {
            int low, high, mid;
            low = 0;
            high = array.length - 1;
            while (low <= high) {
                mid = (low + high) / 2;
                if (array[mid] < T) {
                    low = mid + 1;
                } else if (array[mid] > T) {
                    high = mid - 1;
                } else {
                    return mid;
                }
            }
            return -1;
        }
    }

    public class BigNumberCountSum {
        public String bigNumberCountSum(String num1, String num2) {

            if (num1 == null || num1.length() == 0 || num2 == null || num2.length() == 0)
                return "";
            StringBuffer sb1 = new StringBuffer(num1);
            StringBuffer sb2 = new StringBuffer(num2);
            sb1.reverse();
            sb2.reverse();

            //对应位执行补0操作
            if (sb2.length() < sb1.length()) {
                for (int i = sb2.length(); i < sb1.length(); i++) {
                    sb2.append('0');
                }
            }
            if (sb1.length() < sb2.length()) {
                for (int i = sb1.length(); i < sb2.length(); i++) {
                    sb1.append('0');
                }
            }

            StringBuffer sb = new StringBuffer();
            int flag = 0;

            for (int i = 0; i < sb1.length(); i++) {
                int number1 = sb1.charAt(i) - '0';
                int number2 = sb2.charAt(i) - '0';
                int temp = number1 + number2 + flag;
                if (temp >= 10) {
                    sb.append(temp - 10);
                    flag = 1;
                } else {
                    sb.append(temp);
                    flag = 0;
                }
            }
            //判断最高位是否有进位
            if (flag != 0) {
                sb.append(flag);
            }
            return sb.reverse().toString();
        }
    }

    public static int maxDepth(BTreeNode root) {
        //如果节点为空,那么深度就是0
        if (root == null) {
            return 0;
        }
        //否则递归的计算  max(左子树的最大深度,右子树的最大深度)
        //不管左子树,右子树是否为空,他们的父节点肯定是不为空
        //所以计算出的总深度要把父节点也要加上,也就是 +1
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }

    public static int minDepth(BTreeNode root) {
        //如果节点为空,那么深度就是0
        if (root == null) {
            return 0;
        }
        //否则递归的计算  max(左子树的最大深度,右子树的最大深度)
        //不管左子树,右子树是否为空,他们的父节点肯定是不为空
        //所以计算出的总深度要把父节点也要加上,也就是 +1
        return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
    }

    // 获取最大宽度
    public static int getMaxWidth(BTreeNode root) {
        if (root == null)
            return 0;

        Queue<BTreeNode> queue = new ArrayDeque<BTreeNode>();
        int maxWitdth = 1; // 最大宽度
        queue.add(root); // 入队

        while (true) {
            int len = queue.size(); // 当前层的节点个数
            if (len == 0)
                break;
            while (len > 0) {// 如果当前层,还有节点
                BTreeNode t = queue.poll();
                len--;
                if (t.left != null)
                    queue.add(t.left); // 下一层节点入队
                if (t.right != null)
                    queue.add(t.right);// 下一层节点入队
            }
            maxWitdth = Math.max(maxWitdth, queue.size());
        }
        return maxWitdth;
    }

    public List<List<Integer>> levelOrder(BTreeNode root) {
        if (root == null) {
            return new ArrayList<List<Integer>>();
        }

        List<List<Integer>> res = new ArrayList<List<Integer>>();
        LinkedList<BTreeNode> queue = new LinkedList<BTreeNode>();
        //将根节点放入队列中,然后不断遍历队列
        queue.add(root);
        while (queue.size() > 0) {
            //获取当前队列的长度,这个长度相当于 当前这一层的节点个数
            int size = queue.size();
            ArrayList<Integer> tmp = new ArrayList<Integer>();
            //将队列中的元素都拿出来(也就是获取这一层的节点),放到临时list中
            //如果节点的左/右子树不为空,也放入队列中
            for (int i = 0; i < size; ++i) {
                BTreeNode t = queue.remove();
                tmp.add(t.value);
                if (t.left != null) {
                    queue.add(t.left);
                }
                if (t.right != null) {
                    queue.add(t.right);
                }
            }
            //将临时list加入最终返回结果中
            res.add(tmp);
        }
        return res;
    }


    public static void main(String[] args) {
//        String str1 = "ABc";
//        String str2 = "5678";
//        StringBuffer sb  = new StringBuffer(str2);
//        System.out.println(sb.length());
        System.out.println((int) str1.charAt(1));
//        System.out.println(Integer.parseInt(str2));
//        System.out.println(parseInteger(str2));
//        int aaa = Runtime.getRuntime().availableProcessors();
//        System.out.println("可用的处理器个数:" + aaa);
        BTreeNode treeNode5 = new BTreeNode(1);
        BTreeNode treeNode4 = new BTreeNode(2, null, treeNode5);
        BTreeNode treeNode3 = new BTreeNode(3);
        BTreeNode treeNode2 = new BTreeNode(4, treeNode4, treeNode5);
        BTreeNode treeNode1 = new BTreeNode(5, treeNode2, treeNode3);

        System.out.println("tree : " + JSON.toJSONString(treeNode1));
        int aaa = minDepth(treeNode1);
        System.out.println("深度:" + aaa);

//        new Thread(new Producer("张三")).start();
//        new Thread(new Consumer("李四")).start();
    }

    // String 转换为 Integer
    public static Integer parseInteger(String str) {
        StringBuffer sb = new StringBuffer(str);
        sb.reverse();
        int len = sb.length();
        List<Integer> list = new LinkedList<>();
        for (int i = 0; i < len; i++) {
            int temp = sb.charAt(i) - '0';
            list.add(temp);
        }
        int res = 0;
        for (int j = 0; j < list.size(); j++) {
            int t = (int) (list.get(j) * Math.pow(10, j));
            res = res + t;
        }
        return res;
    }

    public static void isDigit(String args) {
        String str = "wq8123fvvbvrt78931321";
        String[] strs = str.split("");
        StringBuilder numStr = new StringBuilder();
        StringBuilder alpStr = new StringBuilder();
        for (int i = 0; i < strs.length; i++) {
            // 用char包装类中的判断数字的方法判断每一个字符
            if (Character.isDigit(str.charAt(i))) {
                numStr.append(strs[i]);
            }
            // 用char包装类中的判断字母的方法判断每一个字符
            if (Character.isLetter(str.charAt(i))) {
                alpStr.append(strs[i]);
            }
        }
        System.out.println("数字=" + numStr);
        System.out.println("字母=" + alpStr);

    }

    public static BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(10);

    public static class Producer implements Runnable {
        public String name;

        public Producer(String name) {
            this.name = name;
        }

        public AtomicInteger i = new AtomicInteger(0);

        @Override
        public void run() {
            try {

                while (true) {
                    Thread.sleep(1000);
                    queue.put(i.getAndIncrement());
                    System.out.println(name + "执行线程" + Thread.currentThread() + "| " + JSON.toJSONString(queue));
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static class Consumer implements Runnable {
        public String name;

        public Consumer(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            try {
                while (true) {
                    queue.take();
                    Thread.sleep(2000);
                    System.out.println(name + "执行线程" + Thread.currentThread() + "| " + JSON.toJSONString(queue));
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值