一小时算法

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class test_05_02 {

    class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    //子集
    public static List> subsets(int[] nums){
        List> res = new ArrayList<>();
        int n = nums.length;
        int num = (int) Math.pow(2,n);
        for (int i=0;i ls = new ArrayList<>();
            for (int j=0;j>j)&1)==1){
                    ls.add(nums[j]);
                }
            }
            res.add(ls);
        }
        return res;
    }
    //合并k个排序链表
    public ListNode mergeKLists(ListNode[] lists) {
        ListNode newHead = new ListNode(-1);
        ListNode q = newHead;
        while (true){
            ListNode p = new ListNode(Integer.MAX_VALUE);
            int index=-1;
            for (int i=0;i=0&&j>=0){
            if (nums1[i]>nums2[j]){
                nums1[k--]=nums1[i--];
            }else{
                nums1[k--]=nums2[j--];
            }
        }
        while (i>=0){
            nums1[k--]=nums1[i--];
        }
        while (j>=0){
            nums1[k--]=nums2[j--];
        }
    }

    //最大子数组和
    //[5,4,-1,7,8]
    public int maxSubArray(int[] nums) {
        int last = nums[0];
        int max = last;
        for (int i=1;i> solveNQueens(int n) {
        String[][] nums = new String[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(nums[i], "."); // 正确初始化每一行
        }
        List> res = new ArrayList<>();
        NDfs(n,res,0,nums);
        return res;
    }

    public void NDfs(int n, List> res, int pos, String[][] nums) {
        if (pos==n){
            // 将当前解加入结果集
            List solution = new ArrayList<>();
            for (String[] row : nums) {
                solution.add(String.join("", row)); // 将每行拼接成字符串
            }
            res.add(solution);
            return;
        }

        for (int i=0;i=0&&col>=0;row--,col--){
            if ("Q".equals(nums[row][col])){
                return true;
            }
        }
        for (int row=i-1,col=j+1;row>=0&&col> threeSum(int[] nums) {
        //排序
        Arrays.sort(nums);
        int n = nums.length;
        List> res = new ArrayList<>();
        for (int i=0;i0&&nums[i]==nums[i-1]){
                continue;
            }
            int j=i+1;
            int k=nums.length-1;
            while (j0){
                    k--;
                }else{
                    j++;
                }
            }
        }
        return res;
    }

    //最长上升子序列
    public static int lengthOfLIS(int[] nums){
        int n = nums.length;
        int[] dp =new int[n];
        Arrays.fill(dp,1);
        dp[0]=1;
        int max = dp[0];
        for (int i=1;i=0;j--){
                if (nums[i]>nums[j]){
                    dp[i]=Math.max(dp[i],dp[j]+1);
                    max = Math.max(dp[i],max);
                }
            }
        }
        return max;
    }

    //100
    //2 4
    public static double myPow(double x, int n){
        if (n<0){
            x=(1/x);
            n=-n;
        }
        double res = 1;
        double z = x;
        while (n!=0){
            if ((n&1)==1){
                res = res * z;
            }
            z *=z;
            n=n>>1;
        }
        return res;
    }

    //二叉搜索树的最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root==null||root==p||root==q){
            return root;
        }
        TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
        TreeNode rightNode = lowestCommonAncestor(root.right, p, q);
        if (leftNode!=null&&rightNode!=null){
            return root;
        }else if (leftNode!=null){
            return leftNode;
        }else return rightNode;
    }

    //两个链表的第一个公共节点
    ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode p = headA;
        ListNode q = headB;
        while (p!=q){
            p = p==null?headB:p.next;
            q = q==null?headA:q.next;
        }
        return p;
    }
    

    public static void main(String[] args) {
        double v = myPow(1, 2147483647);
        System.out.println(v);
    }
}

你可能感兴趣的:(spring)