class Solution {
public List<String> subdomainVisits(String[] cpdomains) {
Map<String, Integer> m = new HashMap<String, Integer>();
for (String s : cpdomains) {
String[] num = s.split("\\s+");
int c = Integer.valueOf(num[0]);
String[] parts = num[1].split("\\.");
String site = "";
for (int i = parts.length - 1; i >= 0; i--) {
if (i < parts.length - 1) {
site = parts[i] + "." + site;
} else {
site = parts[i] + site;
}
m.put(site, m.getOrDefault(site, 0) + c);
}
}
List<String> result = new ArrayList<String>();
for (String key : m.keySet()) {
result.add(m.get(key) + " " + key);
}
return result;
}
}
Runtime: 32 ms, faster than 27.58% of Java online submissions for Subdomain Visit Count.
Memory Usage: 41.5 MB, less than 19.52% of Java online submissions for Subdomain Visit Count.
class Solution {
public int repeatedNTimes(int[] nums) {
boolean found = false;
int i = 0;
int length = (nums.length-1) / 2;
Arrays.sort(nums);
while (! found) {
if (nums[i] == nums[i+length]) {
return nums[i];
} else {
i++;
}
}
return -1;
// Arrays.sort(nums);
// if (nums.length % 2 == 0) {
// int firsthalf = nums[nums.length / 4];
// int lasthalf = nums[3 * nums.length / 4];
// int middle = nums[nums.length / 2];
// return (middle == firsthalf) ? firsthalf : lasthalf;
// } else {
// return nums[nums.length / 2];
// }
}
}
Runtime: 5 ms, faster than 38.49% of Java online submissions for N-Repeated Element in Size 2N Array.
Memory Usage: 40 MB, less than 38.23% of Java online submissions for N-Repeated Element in Size 2N Array.
처음에는 반값 찾기 법칙을 쓰려고 했는데
짧은거는 안된다는 걸 꺠닫고 n개 후 같은게 나올때까지 돌렸읍니다
class Solution {
public int repeatedNTimes(int[] A) {
for (int k = 1; k <= 3; ++k)
for (int i = 0; i < A.length - k; ++i)
if (A[i] == A[i+k])
return A[i];
throw null;
}
}
Runtime: 0 ms, faster than 100.00% of Java online submissions for N-Repeated Element in Size 2N Array.
Memory Usage: 39.8 MB, less than 55.46% of Java online submissions for N-Repeated Element in Size 2N Array.
제꺼랑 아이디어는 같은거 같은데.. 이렇게 하면 훨씬 빨라지네요~