문제풀이 2021 01 09
2021.01.09 연습
- 사용언어 : java
1.
class Solution {
public int[] shuffle(int[] nums, int n) {
int[] arr1 = Arrays.copyOfRange(nums, 0, n);
int[] arr2 = Arrays.copyOfRange(nums, n, nums.length);
for (int i = 0 ; i < n ; i ++) {
nums[(2*i)] = arr1[i];
nums[(2*i)+1] = arr2[i];
}
return nums;
}
}
Runtime: 0 ms, faster than 100.00% of Java online submissions for Shuffle the Array. Memory Usage: 39.5 MB, less than 13.85% of Java online submissions for Shuffle the Array.
- 다른 코드 참고
class Solution {
public int[] shuffle(int[] nums, int n) {
int [] res = new int [n*2];
int y=0; //two pointers to get data from given array
for(int i =0;i<nums.length-1;i+=2){
res[i]=nums[y++];
res[i+1]=nums[n++]; //filling result array
}
return res;
}
}
2.
class Solution {
public String defangIPaddr(String address) {
return address.replaceAll("\\.","\\[.\\]");
}
}
/*
replace와 replaceAll의 차이
replaceAll은 정규표현식에서 사용하는 기호(특수문자)가 들어간 경우 정규표현식으로 인식하고
replace는 문자 그대로 인식한다.
따라서 . 의 경우 '전체'라고 인식하기 때문에 \\로 escape해준다.
[,],(,),{,},^ 의 경우도 \\로 escape 해준다.
*,+,$,| 등은 []로 감싸주면 문자 자체로 인식한다고 함
*/
Runtime: 5 ms, faster than 10.16% of Java online submissions for Defanging an IP Address. Memory Usage: 38.9 MB, less than 5.02% of Java online submissions for Defanging an IP Address.
3.
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
for ( int i =0; i < nums.length; i ++ ) {
for (int j = i+1 ; j < nums.length; j ++) {
if (target - nums[i] == nums[j]) {
result[0] = i;
result[1] = j;
break;
}
}
}
return result;
}
}
/*
Brute Force
Time complexity : O(n^2)O(n^2).
For each element, we try to find its complement by looping through the rest of array which takes O(n)O(n) time.
Therefore, the time complexity is O(n^2)O(n^2).
Space complexity : O(1)O(1).
*/
Runtime: 10 ms, faster than 21.62% of Java online submissions for Two Sum. Memory Usage: 40.9 MB, less than 14.66% of Java online submissions for Two Sum.
- 다른 코드 참고
//Approach 2: Two-pass Hash Table
/*
Time complexity : O(n)O(n). We traverse the list containing nn elements exactly twice.
Since the hash table reduces the look up time to O(1)O(1), the time complexity is O(n)O(n).
Space complexity : O(n)O(n).
The extra space required depends on the number of items stored in the hash table,
which stores exactly nn elements.
*/
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement) && map.get(complement) != i) {
return new int[] { i, map.get(complement) };
}
}
throw new IllegalArgumentException("No two sum solution");
}
//Approach 3: One-pass Hash Table
/*
Time complexity : O(n)O(n). We traverse the list containing nn elements only once.
Each look up in the table costs only O(1)O(1) time.
Space complexity : O(n)O(n).
The extra space required depends on the number of items stored in the hash table,
which stores at most nn elements.
*/
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}