문제풀이 2021 02 15
2021.02.15 연습
- 사용언어 : java
class Solution {
public int findNumbers(int[] nums) {
int count = 0;
for (int i : nums) {
if ( ((int)Math.log10(i)+1)%2 == 0) {
count++;
}
}
return count;
}
}
Runtime: 1 ms, faster than 94.54% of Java online submissions for Find Numbers with Even Number of Digits. Memory Usage: 38.3 MB, less than 95.86% of Java online submissions for Find Numbers with Even Number of Digits.
2021.02.12 연습 (설연휴 본가서 푼것)
class Solution {
public int numberOfMatches(int n) {
int result = 0;
while ( n > 1) {
if (n%2 == 0) { //even
n = n/2;
result += n;
} else { //odd
result += (n-1)/2;
n = (n-1)/2 + 1;
}
}
return result;
}
}
class Solution {
public int largestAltitude(int[] gain) {
int result = 0;
int nextPoint = 0;
for (int i : gain) {
nextPoint = nextPoint + i;
result = Math.max(result, nextPoint);
}
return result;
}
}
2021.02.13 연습
class Solution {
public int countGoodTriplets(int[] arr, int a, int b, int c) {
int result = 0;
for (int i =0; i < arr.length; i ++) {
for (int j=i+1; j < arr.length; j++) {
for (int k=j+1; k<arr.length; k++) {
if ( Math.abs(arr[i]-arr[j]) <=a
&& Math.abs(arr[j]-arr[k]) <=b
&& Math.abs(arr[i]-arr[k]) <=c ) {
result++;
}
}
}
}
return result;
}
}
Runtime: 13 ms, faster than 69.06% of Java online submissions for Count Good Triplets. Memory Usage: 36.7 MB, less than 54.24% of Java online submissions for Count Good Triplets.
- 다른 코드 참고
class Solution {
public int countGoodTriplets(int[] arr, int a, int b, int c) {
int output = 0;
for(int i = 0; i < arr.length -2; i++)
for(int j = i+1; j < arr.length -1; j++)
if(Math.abs(arr[i] - arr[j]) <= a)
for(int k = j+1; k < arr.length; k++)
if(Math.abs(arr[j] - arr[k]) <= b)
if(Math.abs(arr[i] - arr[k]) <= c)
output++;
return output;
}
}