在IDEA上快乐刷力扣
在IDEA上快乐刷力扣
为即将到来的秋招准备刷题,网上找了很多教程,整理一下,方便以后换电脑快速上手
一、安装力扣插件
在IDEA内部直接install即可
File-->settings-->plugins
二、配置力扣插件
File-->settings-->tools
$!velocityTool.camelCaseName(${question.titleSlug})
package leetcode.editor.cn;
${question.content}
public class $!velocityTool.camelCaseName(${question.titleSlug}){
public static void main(String[] args) {
Solution solution = new $!velocityTool.camelCaseName(${question.titleSlug})().new Solution();
}
${question.code}
}
三、配置账户
右下角打开安装好的力扣
登陆力扣,按F12
四、调试本地样例
附代码:
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class TwoSum{
public static void main(String[] args) {
Solution solution = new TwoSum().new Solution();
Scanner sc=new Scanner(System.in);
System.out.println("请输入数组:");
int[] nums=new int[3];
for (int i = 0; i <nums.length; i++) {
int b=sc.nextInt();
nums[i]=b;
}
System.out.println("请输入目标数字:");
int target=sc.nextInt();
int[] ints=solution.twoSum(nums,target);
System.out.println(Arrays.toString(ints));
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int[] twoSum(int[] nums, int target) {
//hash映射,创建集合对象,作为键的对象整数,值得对象存储整数
//用接口Map引用对象会使程序更加灵活
//key:nums[i];value:i
Map<Integer,Integer> map =new HashMap<>();
//遍历数组nums,i为当前下标
for(int i=0;i<nums.length;i++){
//每个值都判断map中是否存在target-nums[i]的key值
if(map.containsKey(target-nums[i])){
//如果存在则找到了这两个值
return new int[]{ map.get(target-nums[i]),i};
}
//如果不存在则将当前的(nums[i],i)存入map继续遍历
map.put(nums[i],i);
}
//如果最后都没有结果,则抛出异常
throw new IllegalArgumentException("No two sum solution");
}
}
//leetcode submit region end(Prohibit modification and deletion)
}