SortedSet<Integer> combinations = new TreeSet<>();
μ‘°ν©(combination) κ²½μ°μ μλ₯Ό λ°λ³΅νλ 루νμμ
ꡬν체μ addνμ¬ μ€λ¦μ°¨μμΌλ‘ λ΄λ λ°λ³΅λμ§ μλλ‘ ν¨.
μμλ³Ό μ μλ μλ₯Ό 0μΌλ‘ νκΈ°ν λ
μ΅κ³ μμ = 0μ΄ λ¬΄μ‘°κ±΄ λ§λ μ«μλ‘
μ΅μ μμ = 0μ΄ λ¬΄μ‘°κ±΄ νλ¦° μ«μλ‘ λλ―λ‘
λ§μΆ μ«μ κ°μμ 0μ κ°μλ₯Ό νμ©ν΄ 쑰건 μμ±ν¨.
HashMapμ μ¬μ©ν κΉ κ³ λ―Όνλ€κ° λ°°μ΄λ§ μ¬μ©ν¨
Comparator
μΈν°νμ΄μ€λ₯Ό ꡬννλ ν΄λμ€λ₯Ό λ§λ€κ³ compare()
λ₯Ό @Override
class CharComparator implements Comparator<String>{
private int index;
public CharComparator(int index){
this.index = index;
}
@Override
public int compare(String s1, String s2){
char c1 = s1.charAt(index);
char c2 = s2.charAt(index);
if (c1 == c2) {
return s1.compareTo(s2);
// Sort lexicographically
}
return Character.compare(c1,c2);
}
}
Arrays.sort(strings, new CharComparator(n))
Arrays.sort(arr, Comparator.reverseOrder());
static boolean isPrime(int n){
if(n == 1)
return false;
else if(n == 2 || n == 3)
return true;
else {
for(int i = 2; i<= Math.sqrt(n); i++){
if(n%i==0)
return false;
}
return true;
}
}
μ λ ₯ λ¬Έμμ΄μ λ¬Έμ λ°°μ΄μ λ£κ³ μ 체 루ννλ©΄μ κ²μ¬
7κ³Ό κ°μ λ°©λ²μΌλ‘ μ κ·Όν¨.
"zλ₯Ό 1λ§νΌ λ°μ΄μ aλ‘ λ§λ€κΈ°"λ μν νλ₯Ό μ¬μ©ν κΉ κ³ λ―Όνλ€κ° κ·Έλ₯ λ°°μ΄λ‘ ASCII κ° κ΄μ°°νλ©΄μ ꡬνν¨
Character.toChars(64+diff)
: diffκ° 1μΈ κ²½μ° λλ¬Έμ A
Character.toChars(96+diff)
: diffκ° 1μΈ κ²½μ° μλ¬Έμ a
λ§μΉ¨ν(.)κ° 2λ² μ΄μ μ°μλ λΆλΆμ νλμ λ§μΉ¨ν(.)λ‘ μΉν
μ κ·ννμμ .replaceAll("(.)\1+", ".")
μ΄κ±Έλ‘ ν΄μ κ³μ νλ¦Ό...
(.)
<- μ΄λ€ λ¬ΈμλΌλ
\1+
<- μ°μλλ κ²½μ°
μ¦ μ°μλλ λ¬Έμλ€μ νκ°μ .
μΌλ‘ λ¬Άμ
μλ₯Ό λ€μ΄ "abbcccdddd" μ κ²½μ° "a..."μ΄ λ¨
κ·Όλ° μ΄κ±΄ λ¬Έμ μꡬμ¬νμ΄λμ λ€λ¦...
μλ§μ ννμ .replaceAll("\.+",".")
"\.+"
<- μ¬λ¬ κ°μ .
μ΄ λ°λ³΅λλ κ²½μ°
if(num%j==0) count++;
10κ³Ό κ°μ
μ΄μ μ λ°±μ€μμ νμλ λ΄μ©
int gcd = 1;
for(int j = 1; j<=a && j<=b; j++){
if(a%j==0 && b%j==0)
gcd = j;
}
int lcm = a*b/gcd;
if(n%i==1){
ans = i;
break;
}
μκ°λ³΅μ‘λλ₯Ό μ€μ¬λ³΄μ
- μ§μλ 2λ₯Ό μ μΈνλ©΄ μ λΆ μμκ° μλλ―λ‘ λ°λ³΅μμ μ μΈ κ°λ₯
- (λ°λ³΅λ³μ) <= Math.sqrt(n)
μ μ νΌ λ΄μ©μ΄ λμμ΄ λμλ€
HashMap
μ΄λ μν ν κ°μ μλ£κ΅¬μ‘°λ₯Ό λ¬Έμ νλ μ¬μ©νκΈ°κ° μ΄λ ΅κ² λκ»΄μ‘λ€.
μλ£κ΅¬μ‘° 곡λΆκ° λ νμνλ€...
μ΄κ²λ ν΄μΌ ν λ―