import java.util.*;
class Solution {
public int[] solution(int[] numlist, int n) {
int size = numlist.length;
for(int i=0; i<size-1; i++){
for(int k=i+1; k<size; k++){
int a = (numlist[i] - n) * (numlist[i] > n ? 1 : -1);
int b = (numlist[k] - n) * (numlist[k] > n ? 1 : -1);
if(a > b || (a == b && numlist[i] < numlist[k])){
int temp = numlist[i];
numlist[i] = numlist[k];
numlist[k] = temp;
}
}
}
return numlist;
}
}
[์คํจ]
class Solution {
public int solution(int[][] lines) {
int answer = 0;
int x1 = lines[0][0], y1 = lines[0][1];
int x2 = lines[1][0], y2 = lines[1][1];
int x3 = lines[2][0], y3 = lines[2][1];
if (y1 > x2){
answer += (y1 - x2);
}else if (y1 > x2 && x1 >= x2){
answer += (y2 - x1);
}
if (y2 > x3){
if (x3 < x1){
answer += (y2 - x1);
} else if ( x3 > x1){
answer = (y2 - x3);
}
}else if (x2 < x3 && x1 >= x3) {
answer +=(y3 - x3);
}
return answer;
}
}
[ํฌ๊ธฐ] ๋ฐฐ์ด์ ๋ฃ์ด์ ํด๋ณด๋ ค๊ณ ํ์ง๋ง ๋๋ฌด ๋ณต์กํด์ ธ์ ํฌ๊ธฐ
import java.lang.Math;
class Solution {
public int solution(int[][] lines) {
int answer = 0;
int[] tmp = new int[200];
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int x1 = lines[0][0], y1 = lines[0][1];
int x2 = lines[1][0], y2 = lines[1][1];
int x3 = lines[2][0], y3 = lines[2][1];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 2; j++){
if (max < lines[i][j]){
max = lines[i][j];
}
else if (min > lines[i][j]){
min = lines[i][j];
}
}
}
for (int i = 0; i < 3; i++){
for (int j = 0; j < 2; j++){
for (int z = min; z < min; z++){
if (lines[i][j] == z){
if ( z < 0){
tmp[Math.abs(z-1)]++;
}else {
tmp[z*2-1]++;
}
}
}
}
}
int F = 0;
int L = 0;
for (int i=0; i<tmp.length; i++){
if (tmp[i] == 2){
F = tmp[i];
break;
}
}
for (int i=0; i<tmp.length; i++){
if (tmp[i] == 2){
L = tmp[i];
}
}
/*
if (y1 > x2){
answer += (y1 - x2);
}else if (y1 > x2 && x1 >= x2){
answer += (y2 - x1);
}
if (y2 > x3){
if (x3 < x1){
answer += (y2 - x1);
} else if ( x3 > x1){
answer = (y2 - x3);
}
}else if (x2 < x3 && x1 >= x3) {
answer +=(y3 - x3);
}
*/
return ;
}
}