java/javaAlgorithm(16)
-
[Leetcode]1004. Max Consecutive Ones III(x)
1. problem :https://leetcode.com/problems/max-consecutive-ones-iii/?envType=study-plan-v2&envId=leetcode-75 2. solution 1 :class Solution { public int longestOnes(int[] nums, int k) { int left = 0, right = 0; int maxOnes = 0; int zeroCount = 0; while (right k) { if (nums[left] == 0) { zeroCount -= 1; } ..
2024.07.31 -
[Leetcode]49. Group Anagrams(x)
1. problem :https://leetcode.com/problems/group-anagrams/description/ 2. solution 1 :public class Solution { public List> groupAnagrams(String[] strs) { if (strs == null || strs.length == 0) return new ArrayList(); Map> map = new HashMap(); for (String str : strs) { char[] charArray = str.toCharArray(); Arrays.sort(charArray); String sorte..
2024.07.31 -
[Leetcode]15. 3Sum(x)
1. problem : https://leetcode.com/problems/3sum/description/ 2. solution 1 :public class Solution { public List> threeSum(int[] nums) { Arrays.sort(nums); List> ans = new ArrayList(); for (int i = 0; i 0 && nums[i - 1] == nums[i]) { continue; } int left = i + 1; int right = nums.length - 1; while (left 이 문제는 chat..
2024.07.30 -
[Leetcode]1. Two Sum
1. problem : https://leetcode.com/problems/two-sum/description/ 2. solution 1 :public class Solution { public int[] twoSum(int[] nums, int target) { HashMap hashMap = new HashMap(); for (int i = 0; i 시간복잡도는 O(n)이다. hashMap을 이용하는데, target - char가 없다면 nums [i] 등록, diff가 있다면 index 반환
2024.07.30 -
[Leetcode]242. Valid Anagram
1. problem : https://leetcode.com/problems/valid-anagram/description/ 2. solution 1 :public class Solution { public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { return false; } HashMap hashMap = new HashMap(); for (char c : s.toCharArray()) { hashMap.put(c,hashMap.getOrDefault(c,0) + 1); } for (char c :..
2024.07.30 -
[Leetcode]217. Contains Duplicate
1. problem : https://leetcode.com/problems/contains-duplicate/ 2. solution 1 :public class Solution { public boolean containsDuplicate(int[] nums) { HashMap hashMap = new HashMap(); for (int i=0; i HashMap을 써서, 값이 들어있다면, true 반환, 없다면 추가, 루프가 정상적으로 끝난다면, false 반환
2024.07.30