LeetCode
夏天captain
记录个人笔记
展开
-
LeetCode:移动零
LeetCode:移动零给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。// 双指针思想 需要优化 public static String moveZeroes(int[] nums) { int j = 0; for (int i = 1; i < nums.length; i++) { if (nums[j] == 0 && nums[i] != 0) {原创 2021-09-07 12:03:56 · 65 阅读 · 0 评论 -
LeetCode:两数之和
LeetCode:两数之和给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。你可以按任意顺序返回答案 // 双层for循环、双指针效率太差 使用哈希表 public static int[] twoSum(int[] nums, int target) { HashMap<Integer, In原创 2021-09-07 12:02:48 · 171 阅读 · 0 评论 -
LeetCode存在重复元素
LeetCode存在重复元素给定一个整数数组,判断是否存在重复元素。如果存在一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false 。public static boolean containsDuplicate(int[] nums){ boolean b = false; HashSet<Integer> hashSet = new HashSet<Integer>(); // 将数原创 2021-08-27 16:58:31 · 53 阅读 · 0 评论 -
LeetCode:计算素数
LeetCode:计算素数统计所有小于非负整数 n 的质数的数量埃拉托斯特尼筛法(埃氏筛):要得到自然数n以内的全部素数,必须把不大于根号n的所有素数的倍数剔除,剩下的就是素数public static Integer getPrimeNumberCounts(int n){ Integer counts = 0; // 初始化数组全为false boolean[] booleans = new boolean[n]; // 从2开始原创 2021-08-26 14:47:52 · 263 阅读 · 0 评论 -
LeetCode:罗马数字转整数
LeetCode:罗马数字转整数charAt() 方法用于返回指定索引处的字符按照对应数字进行相加即可,但是存在IV为4等情况,对次题目本身规避了不合理的形式 如IL等(具体看题目)public static Integer getRomanNumerals(String s){ // 初始化对应关系 Map<Character,Integer> stringIntegerMap = new HashMap<>(); stri原创 2021-08-24 17:31:15 · 89 阅读 · 0 评论