๐ป Coding Problems Solving/Array | String | Loop
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 [3์ฐจ] ํ์ผ๋ช ์ ๋ ฌ
Kim_dev
2022. 4. 24. 17:47
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 [3์ฐจ] ํ์ผ๋ช ์ ๋ ฌ
1. ๋ฌธ์ : Link
ํ์ผ๋ช ์ ๋ ฌํ๊ธฐ
2. ํ์ด
lamdaํ์ฉ
3. ์ฝ๋
def solution(files):
answer = []
real = []
for j in range(len(files)):
s = ''
tmp = []
file = files[j]
H = False
for i in range(len(file)):
f = file[i]
if H == False:
if f.isnumeric() == False:
if f.isalpha():
s += f.lower()
else:
s += f
else:
tmp.append(s)
s = ''
H = True
if H == True:
if f.isnumeric() == True:
s += f
if i == len(file)-1:
tmp.append(int(s))
break
else:
tmp.append(int(s))
break
tmp.append(j)
answer.append(tmp)
answer = sorted(answer, key = lambda x : (x[0], x[1]))
for an in answer:
real.append(files[an[2]])
return real
import java.util.*;
class Solution {
public String[] solution(String[] files) {
Arrays.sort(files, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String h1 = o1.split("[0-9]")[0];
String h2 = o2.split("[0-9]")[0];
int result = h1.toLowerCase().compareTo(h2.toLowerCase());
if ( result == 0 ) {
// ๋ฌธ์์ด์ด ๊ฐ์ ๊ฒฝ์ฐ ์ซ์๋ฅผ ๋น๊ตํ๋ค.
result = findNum(o1,h1)-findNum(o2,h2);
}
return result;
}
});
return files;
}
private int findNum( String s, String h ) {
s = s.replace(h, "");
String result ="";
for( char c : s.toCharArray()) {
if( Character.isDigit(c) && result.length() < 5 ) {
result+=c;
}else
break;
}
return Integer.valueOf(result);
}
}