一小时算法

复制代码
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<List<Integer>> subsets(int[] nums){
        List<List<Integer>> res = new ArrayList<>();
        int n = nums.length;
        int num = (int) Math.pow(2,n);
        for (int i=0;i<num;i++){
            List<Integer> ls = new ArrayList<>();
            for (int j=0;j<n;j++){
                if (((i>>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<lists.length;i++){
                if (lists[i]!=null&&lists[i].val<p.val){
                    p=lists[i];
                    index=i;
                }
            }
            //表示没有找到一个合适的
            if (p.val==Integer.MAX_VALUE){
                break;
            }
            q.next=new ListNode(p.val);
            q=q.next;
            lists[index]=lists[index].next;
        }
        return newHead.next;
    }
    public ListNode mergeKLists2(ListNode[] lists) {
        ListNode newHead = new ListNode(-1);
        ListNode tail = newHead;
        while (true){
            ListNode p = null;
            int index = -1;
            for (int i=0;i<lists.length;i++){
                if (lists[i]==null){
                    continue;
                }
                if (p==null||lists[i].val<p.val){
                    p=lists[i];
                    index = i;
                }
            }
            if (index==-1){
                break;
            }
            tail.next=p;
            tail=tail.next;
            lists[index]=lists[index].next;
        }
        return newHead.next;
    }

    //合并两个有序数组
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int i=m-1;
        int j=n-1;
        int k=nums1.length-1;
        while (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<nums.length;i++){
            last = Math.max(nums[i],last+nums[i]);
            max = Math.max(max,last);
        }
        return max;
    }

    //abcde
    //ace
    //最长公共子序列

    public static int longestCommonSubsequence(String text1, String text2) {
        int n = text1.length();
        int m = text2.length();
        int[][] dp = new int[n][m];

        int max = 0;
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                if (i==0&&j==0){
                    if (text1.charAt(i)==text2.charAt(j)){
                        dp[i][j]=1;
                    }
                }else if (i==0){
                    if (text1.charAt(i)==text2.charAt(j)){
                        dp[i][j]=1;
                    }else{
                        dp[i][j]=dp[i][j-1];
                    }
                }else if (j==0){
                    if (text1.charAt(i)==text2.charAt(j)){
                        dp[i][j]=1;
                    }else{
                        dp[i][j]=dp[i-1][j];
                    }
                }else{
                    if (text1.charAt(i)==text2.charAt(j)){
                        dp[i][j]=dp[i-1][j-1]+1;
                    }else{
                        dp[i][j]=Math.max(dp[i-1][j-1],Math.max(dp[i][j-1],dp[i-1][j]));
                    }
                }
                max=Math.max(dp[i][j],max);
            }
        }
        System.out.println(Arrays.deepToString(dp));
        return max;
    }

    //N皇后问题
    public List<List<String>> solveNQueens(int n) {
        String[][] nums = new String[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(nums[i], "."); // 正确初始化每一行
        }
        List<List<String>> res = new ArrayList<>();
        NDfs(n,res,0,nums);
        return res;
    }

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

        for (int i=0;i<n;i++){
            //判断是否能被攻击
            if (!isNHarm(nums, pos, i)){
                nums[pos][i]="Q";
                NDfs(n,res,pos+1,nums);
                nums[pos][i]=".";
            }
        }
    }

    private boolean isNHarm(String[][] nums, int i, int j) {
        for (String[] num : nums) {
            if ("Q".equals(num[j])) {
                return true;
            }
        }
        for (int row=i-1,col=j-1;row>=0&&col>=0;row--,col--){
            if ("Q".equals(nums[row][col])){
                return true;
            }
        }
        for (int row=i-1,col=j+1;row>=0&&col<nums[0].length;row--,col++){
            if ("Q".equals(nums[row][col])){
                return true;
            }
        }
        return false;
    }

    //三数之和
    //1 1 2 2
    public static List<List<Integer>> threeSum(int[] nums) {
        //排序
        Arrays.sort(nums);
        int n = nums.length;
        List<List<Integer>> res = new ArrayList<>();
        for (int i=0;i<nums.length-2;i++){
            if (i>0&&nums[i]==nums[i-1]){
                continue;
            }
            int j=i+1;
            int k=nums.length-1;
            while (j<k){
                if (nums[i]+nums[j]+nums[k]==0){
                    res.add(Arrays.asList(nums[i],nums[j],nums[k]));
                    //去除重复的
                    while (j<k&&nums[j]==nums[j+1]){
                        j++;
                    }
                    while (j<k&&nums[k]==nums[k-1]){
                        k--;
                    }
                    j++;
                    k--;
                }else if (nums[i]+nums[j]+nums[k]>0){
                    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<n;i++){
            for (int j=i;j>=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);
    }
}
相关推荐
bing_15820 分钟前
Spring MVC @CookieValue 注解怎么用?
java·spring·mvc
Aurora_NeAr24 分钟前
Spring MVC设计与实现
java·spring·mvc
Rubypyrrha1 小时前
Spring MVC的工作流程, DispatcherServlet 的工作流程
java·spring·mvc
Themberfue2 小时前
Redis ⑨-Jedis | Spring Redis
java·数据库·redis·sql·spring·缓存
liaokailin2 小时前
Spring AI 实战:第九章、Spring AI MCP之万站直通
java·人工智能·spring
fanTuanye5 小时前
SpringMVC详解
java·spring·mvc
程序员buddha6 小时前
【Spring】idea + maven 从零创建Spring IoC容器示例
spring·maven·intellij-idea
程序员buddha6 小时前
使用 IDEA + Maven 搭建传统 Spring MVC 项目的详细步骤(非Spring Boot)
spring·maven·intellij-idea
magic 2457 小时前
Spring 基于 XML 的自动装配:原理与实战详解
xml·java·spring