배열 사이즈를 2배로 늘려서 구함
import java.util.HashSet;
import java.util.Set;
class Solution {
public int solution(int[] elements) {
int[] newElements = new int[elements.length * 2];
for (int i = 0; i < elements.length; i++) {
newElements[i] = elements[i];
newElements[i + elements.length] = elements[i];
}
Set<Integer> set = new HashSet<>();
for (int length = 1; length <= elements.length; length++) {
for (int idx = 0; idx < elements.length; idx++) {
int tempVal = 0;
for (int count = 0; count < length; count++) {
tempVal += newElements[idx + count];
}
set.add(tempVal);
}
}
return set.size();
}
}
나머지 연산자를 사용해서 풀이
import java.util.HashSet;
import java.util.Set;
class Solution {
public int solution(int[] elements) {
Set<Integer> set = new HashSet<>();
for (int length = 1; length <= elements.length; length++) {
for (int idx = 0; idx < elements.length; idx++) {
int tempVal = 0;
for (int count = 0; count < length; count++) {
tempVal += elements[(idx + count) % elements.length];
}
set.add(tempVal);
}
}
return set.size();
}
}