Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions leetcode3/곽현지/15. 3Sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
[문제내용]
Example 1:

Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
Example 2:

Input: nums = [0,1,1]
Output: []
Explanation: The only possible triplet does not sum up to 0.
Example 3:

Input: nums = [0,0,0]
Output: [[0,0,0]]
Explanation: The only possible triplet sums up to 0.


[작성한 답안]
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();

Arrays.sort(nums);

for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1;
int right = nums.length - 1;

while (left < right) {
int sum = nums[i] + nums[left] + nums[right];

// 세 수의 합이 0일 때 출력
if (sum == 0) {
result.add(Arrays.sort(nums[i], nums[left], nums[right]));
} else {
break;
}
}
}
}
}
42 changes: 42 additions & 0 deletions leetcode3/곽현지/49. Group Anagrams
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[문제내용]
Example 1:

Input: strs = ["eat","tea","tan","ate","nat","bat"]

Output: [["bat"],["nat","tan"],["ate","eat","tea"]]

Explanation:

There is no string in strs that can be rearranged to form "bat".
The strings "nat" and "tan" are anagrams as they can be rearranged to form each other.
The strings "ate", "eat", and "tea" are anagrams as they can be rearranged to form each other.
Example 2:

Input: strs = [""]

Output: [[""]]

Example 3:

Input: strs = ["a"]

Output: [["a"]]


[작성한 코드답안]
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> answer = new HashMap<>();
for(String str : strs) {
// 문자 나열
char[] c = str.toCharArray();
Arrays.sort(c); // 나열한 문자 정렬
String s = String.valueOf(c);

if(!answer.containsKey(s))
answer.put(s, new ArrayList<>());
answer.get(s).add(str);
}
return new ArrayList<>(answer.values());
}
}
Loading