File tree Expand file tree Collapse file tree 5 files changed +104
-0
lines changed
Expand file tree Collapse file tree 5 files changed +104
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * 각각 다른 정수 배열 candidates와, 목표 정수 target이 주어진다.
3+ * 반환하라, 타겟과 값이 같은 합을 보유한 고유한 조합을.
4+ * 조합은 어떤 순서(any order)로든 반환할 수 있습니다.
5+ * 같은 숫자를 여러 조합에 무제한(unlimited number of times) 쓸 수 있습니다.
6+ * 선택한 숫자 중 적어도 하나의 빈도가 다르다면 두 조합은 고유합니다.
7+ * target은 150보다 낮은 숫자입니다.
8+ *
9+ * @param {number[] } candidates
10+ * @param {number } target
11+ * @return {number[][] }
12+ */
13+ var combinationSum = function ( candidates , target ) {
14+ var result = [ ] ;
15+ var nums = [ ] ;
16+ function dfs ( start , total ) {
17+ if ( total > target ) return ;
18+ if ( total === target ) result . push ( nums . slice ( ) ) ;
19+
20+
21+ for ( let i = start ; i < candidates . length ; i ++ ) {
22+ let num = candidates [ i ] ;
23+ nums . push ( num ) ;
24+ dfs ( i , total + num ) ;
25+ nums . pop ( ) ;
26+ }
27+
28+ }
29+ dfs ( 0 , 0 ) ;
30+
31+ return result ;
32+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 인코드 된 메세지를 디코드(decode)하려면, 맵핑의 규칙을 반대로 적용하여 숫자를 다시 알파벳 대문자로 돌려놔야 한다.
3+ * 예를 들어, 숫자 11106은 1 1 10 6으로 분할하면 AAJF로 디코드할 수도 있고, 11 10 6으로 분할하면 KJF로도 디코드할
4+ 수 있다.
5+ * 문자열 타입의 숫자가 주어졌을 때 알파벳 문자열로 디코드할 수 있는 방법의 개수를 구하라.
6+ * @param {string } s
7+ * @return {number }
8+ */
9+ var numDecodings = function ( s ) {
10+ const memo = new Map ( ) ;
11+ memo . set ( s . length , 1 ) ;
12+
13+ function dfs ( start ) {
14+ if ( memo . has ( start ) ) {
15+ return memo . get ( start ) ;
16+ }
17+
18+ // 0으로 시작하면 해석 불가
19+ if ( s [ start ] === "0" ) {
20+ memo . set ( start , 0 ) ;
21+ return 0 ;
22+ }
23+
24+ let count = dfs ( start + 1 ) ; // 한 글자 해석
25+
26+ // 두 글자 해석 가능한 경우
27+ if ( start + 1 < s . length && parseInt ( s . substring ( start , start + 2 ) ) <= 26 ) {
28+ count += dfs ( start + 2 ) ;
29+ }
30+
31+ memo . set ( start , count ) ;
32+ return count ;
33+ }
34+
35+ return dfs ( 0 ) ;
36+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number }
4+ */
5+ var maxSubArray = function ( nums ) {
6+ let maxSum = nums [ 0 ] ;
7+ let sum = 0 ;
8+ nums . forEach ( ( num ) => {
9+ sum = Math . max ( sum + num , num ) ;
10+ maxSum = Math . max ( sum , maxSum ) ;
11+ } ) ;
12+ return maxSum ;
13+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number } n
3+ * @return {number }
4+ */
5+ var hammingWeight = function ( n ) {
6+ var answer = 0 ;
7+ while ( n >= 1 ) {
8+ if ( n % 2 === 1 ) answer ++ ;
9+ n = Math . floor ( n / 2 ) ;
10+ }
11+ return answer ;
12+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @return {boolean }
4+ */
5+ var isPalindrome = function ( s ) {
6+ const regexp = / [ ^ a - z 0 - 9 ] / g;
7+ s = s . toLowerCase ( ) ;
8+ s = s . replaceAll ( regexp , "" ) ;
9+
10+ return s === [ ...s ] . reverse ( ) . join ( '' ) ;
11+ } ;
You can’t perform that action at this time.
0 commit comments