牛客刷题记录5

题1:质数因子

java 复制代码
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        long num = in.nextLong();
        long sq = (long)Math.sqrt(num);

        for (int i = 2; i <= sq; i++) {
            while(num % i == 0) {
                System.out.print(i + " ");
                num = num / i;
            }
        }


        //最后还要判断一下是否能除到1
        System.out.print(num == 1 ? "" : num + "");

        in.close();
    }
}

题2:合并区间

java 复制代码
import java.util.*;

/*
 * public class Interval {
 *   int start;
 *   int end;
 *   public Interval(int start, int end) {
 *     this.start = start;
 *     this.end = end;
 *   }
 * }
 */



public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param intervals Interval类ArrayList 
     * @return Interval类ArrayList
     */
    public ArrayList<Interval> merge (ArrayList<Interval> intervals) {
        // write code here
        if (intervals == null || intervals.size() <= 1) {
            return intervals;
        }

        //处理开始
        Collections.sort(intervals, new Comparator<Interval> () {
            @Override
            public int compare (Interval a, Interval b) {
                return Integer.compare(a.start, b.start);
            }
        });


        ArrayList<Interval> result = new ArrayList<Interval>();

        //对要处理的list进行处理,如果最终result这个list中是空的或者当前的对象起点大于上一个的终点那么直接填充就行
        for (int i = 0; i < intervals.size(); i++) {
            Interval current = intervals.get(i);
            if (result.isEmpty() || result.get(result.size() - 1).end < current.start) {
                result.add(current);
            } else {
                //否则就将上一个的终点与当前终点比较取最大的值即可
                result.get(result.size() - 1).end = Math.max(result.get(result.size() - 1).end, current.end);
            }
        }

        return result;

    }
}

题3:大数加法

  • 难点主要是数值太大会超过Long型数据,所以使用BigInteger数据类型来解决就行
java 复制代码
import java.util.*;
import java.math.*;
 
public class Solution {
    /**
     *代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *计算两个数之和
     * @param s string字符串 表示第一个整数
     * @param t string字符串 表示第二个整数
     * @return string字符串
     */
    public String solve (String s, String t) {

        BigInteger a = new BigInteger(s);
        BigInteger b = new BigInteger(t);

        return a.add(b).toString();
    }
}

题4:二叉树根节点到叶子节点的所有路径和

  • 这种链式存储结构的处理我还是更习惯用c语言来解题
c 复制代码
/**
 * struct TreeNode {
 *	int val;
 *	struct TreeNode *left;
 *	struct TreeNode *right;
 * };
 */
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param root TreeNode类 
 * @return int整型
 */
#include <stdbool.h>
int sumNumbers(struct TreeNode* root ) {
    // write code here
    return preorder(root, 0);
}

int preorder(struct TreeNode* root, int sum) {
    if(root == NULL) {
        return NULL;
    }

    sum = sum *10 + root->val;
    if (root->left == NULL &&  root -> right == NULL) {
        return sum;
    }

    return preorder(root -> left, sum) + preorder(root -> right,  sum);
}

题5: 二叉树中和为某一值的路径(二)

java 复制代码
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {

    ArrayList<ArrayList<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new  LinkedList<>();

    public void dps (TreeNode root, int target) {
        if(root == null) {return;}

        path.add(root.val);

        target -= root.val;

        if(root.left == null && root.right == null && target == 0) {
            result.add(new ArrayList<>(path));
        }

        dps(root.left, target);
        dps(root.right,target);
        path.removeLast();

    }

    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @param target int整型 
     * @return int整型ArrayList<ArrayList<>>
     */
    public ArrayList<ArrayList<Integer>> FindPath (TreeNode root, int target) {
        // write code here
        dps(root,target);
        return result;
    }
}
相关推荐
代码山河7 小时前
Python就业方向全解析:后端、爬虫、数据分析、AI你选哪个?
python·学习·架构·教程·面向对象·项目
学长毕设7 小时前
基于SpringBoot的实验室资产管理系统
java·spring boot·后端
C^h7 小时前
c++ string
开发语言·c++
释厄6237 小时前
AI学习层更新与跨体转移律——人类社会同构·不缺理解缺 U
人工智能·学习
raindayinrain7 小时前
c++高性能--原子操作,无锁编程,操作顺序
开发语言·c++·性能优化
学逆向的7 小时前
C++ 合并命名空间
开发语言·c++·网络安全
Zhao-Jichao7 小时前
VS2026 打包 C++ 程序生成 EXE,借助 Installer Project 扩展工具
开发语言·c++
zzu123zsw8 小时前
从臃肿的模板调用到声明式契约:深入 UniHttp 动态代理与多渠道生命周期扩展
java·spring boot·microsoft·unihttp
学逆向的8 小时前
C 局部静态变量
c语言·开发语言·网络安全
大都督会赢的9 小时前
C++第一节课
开发语言·c++