用ACM模式模板刷hot100

面试手撕给的模板基础上写

给的模板一般是下面这样

把while内容删除(一般刷hot100题目输入不需要同时输入几组)

第一个方法里写处理输入输出

自己再写一个方法,就是力扣里的核心代码(加上static)

第一个处理输入输出的方法里面调用第二块的方法

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

public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        while(in.hasNextInt()){
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(a+b);
        }
    }
}

答题模板

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

public class Main {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        ...
    }

    public static int[] method(int[] nums,int target){
        ...
    }
}

举例"两数之和"

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

public class Main {
    public static void main(String[] args) {
        // 数据输入
        Scanner in = new Scanner(System.in);

        // 读取数组长度
        int n = in.nextInt();
        in.nextLine();

        // 读取数组元素
        int[] nums = new int[n];
        String[] inputNums = in.nextLine().split(" ");
        for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(inputNums[i]);
        }

        // 读取目标值
        int target = in.nextInt();

        // 处理 输出
        int[] result = twoSum(nums, target);
        System.out.println(result[0] + " " + result[1]);
    }

    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int[] res = new int[2];
        for(int i = 0; i<nums.length; i++){
            int temp = target - nums[i];
            if(map.containsKey(temp)){
                res[0] = map.get(temp);
                res[1] = i;
            }
            map.put(nums[i], i);
        }
        return res;
    }
}

还有一种写法是

第二块完整写力扣代码,第一段先实例化再调用

这个方法可以不用,记第一个就行

java 复制代码
public class Main {

    public static void main(String[] args) {
        //1.数据输入
        Scanner in = new Scanner(System.in);
        //读数字
        int numLen = in.nextInt();
        int[] numArr = new int[numLen];
        int i = 0;
        while(in.hasNextInt() && i < numLen){
            numArr[i] = in.nextInt();
            i++;
        }
        //读字符串
        int strLen = in.nextInt();
        in.nextLine(); //数字到字符串要换行
        String[] strArr = new String[strLen];
        //或者 strArr[] = in.nextLine().split(" ");
        int j = 0;
        while(in.hasNextLine() && j < strLen){
            strArr[j] = in.nextLine();
            j++;
        }
        
        //2. 处理
        Solution solution = new Solution();
        String result = solution.process(numArr, strArr);
        
        //3. 输出
        System.out.println(result);
        //四舍五入输出小数
        String str = String.format("%.2f",3.555);
        System.out.println(str);
    }
}

//下面类似 LeetCode 的核心代码模式
class Solution {
    public String process(int[] nums, String[] strs) {
        StringBuilder sb = new StringBuilder();
        sb.append(Arrays.toString(nums));
        sb.append(" && ");
        sb.append(Arrays.toString(strs));
        return sb.toString();
    }
}
相关推荐
阿维的博客日记1 天前
Hippo4j 线程池监控平台部署手册
java·spring boot·后端
C+++Python1 天前
详细介绍一下Java泛型的通配符
java·windows·python
JosieBook1 天前
【数据库】时序预测能力的分级进化:TimechoAI如何让每一类用户都能精准预见未来
java·开发语言·数据库
一生了无挂1 天前
Java处理JSON技巧教学(从基础到高阶实战全覆盖)
java·开发语言·json
李白的天不白1 天前
使用 SmartAdmin 进行前后端开发
java·前端
swordbob1 天前
Spring 单例 Bean 是线程安全的吗?
java·开发语言
2601_951643771 天前
Python第一,Java跌出前三,C语言杀回来了
java·c语言·python·编程语言排行·技术趋势
IT 行者1 天前
GitHub Spec Kit 实战(五):/speckit.tasks 怎么拆——Spec Kit 五部曲收官
java·ai编程·claude
(Charon)1 天前
【C++ 面试高频基础:指针、引用、const、static、new/delete 总结】
java·开发语言
Yeats_Liao2 天前
Feed流系统设计(三):数据模型与存储设计,从表结构到Redis收件箱
java·javascript·redis