File tree Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Expand file tree Collapse file tree 3 files changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int numDecodings (String s ) {
3+ // 0 μΌλ‘ μμνκ±°λ λΉ λ¬Έμμ΄μ΄λ©΄ ν΄λ
λΆκ°
4+ int sLen = s .length ();
5+ if (sLen == 0 || s .charAt (0 ) == '0' ) return 0 ;
6+
7+ int [] dp = new int [sLen +1 ];
8+
9+ dp [0 ] = 1 ;
10+ dp [1 ] = 1 ;
11+
12+ for (int i = 2 ; i <= sLen ; i ++) {
13+
14+ int num = Integer .parseInt (s .substring (i -2 , i ));
15+ // 1μ리 μ«μλ‘ ν΄λ
κ°λ₯ν κ²½μ°
16+ if (s .charAt (i -1 ) != '0' ) {
17+ dp [i ] += dp [i -1 ];
18+ }
19+ // 2μ리 μ«μλ‘ ν΄λ
κ°λ₯ν κ²½μ°
20+ if (num >= 10 && num <= 26 ) {
21+ dp [i ] += dp [i -2 ];
22+ }
23+ }
24+ return dp [sLen ];
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int hammingWeight (int n ) {
3+ int count = 0 ;
4+ while (n > 0 ) {
5+ if ((n & 1 ) == 1 ) count ++;
6+ n >>= 1 ;
7+ }
8+ return count ;
9+ }
10+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public boolean isPalindrome (String s ) {
3+ String alphabetOnly = s .replaceAll ("[^a-zA-Z0-9]" , "" ).toLowerCase ();
4+ return new StringBuilder (alphabetOnly ).reverse ().toString ().equals (alphabetOnly );
5+ }
6+ }
You canβt perform that action at this time.
0 commit comments