File tree Expand file tree Collapse file tree
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ // Definition for a Node.
3+ class Node {
4+ public int val;
5+ public List<Node> neighbors;
6+ public Node() {
7+ val = 0;
8+ neighbors = new ArrayList<Node>();
9+ }
10+ public Node(int _val) {
11+ val = _val;
12+ neighbors = new ArrayList<Node>();
13+ }
14+ public Node(int _val, ArrayList<Node> _neighbors) {
15+ val = _val;
16+ neighbors = _neighbors;
17+ }
18+ }
19+ */
20+
21+ class Solution {
22+ //key: original node, value: clone node
23+ private Map <Node , Node > map = new HashMap <>();
24+
25+ public Node cloneGraph (Node node ) {
26+ if (node == null ) {
27+ return null ;
28+ }
29+ //이미 존재하면 return
30+ if (map .containsKey (node )) {
31+ return map .get (node );
32+ }
33+
34+ //현재 노드 복제
35+ Node clone = new Node (node .val );
36+ map .put (node , clone );
37+
38+ //이웃 노드들 복제해서 연결
39+ for (Node neighbor : node .neighbors ) {
40+ clone .neighbors .add (cloneGraph (neighbor ));
41+ }
42+ return clone ;
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int longestCommonSubsequence (String text1 , String text2 ) {
3+ /**
4+ 1.문제: 가장 긴 common subsequence length return, 없으면 0 return
5+ 2.constraints
6+ - length min = 1, max = 1000
7+ - text1, text2 모두 lowercase로 구성
8+ 3.풀이
9+ - dp[i][j] = text1의 앞 i개, text2의 앞 j개를 비교했을 때 LCS 길이
10+ - 문자가 같으면: dp[i][j] = dp[i-1][j-1] + 1
11+ - 문자가 다르면: dp[i][j] = max(dp[i-1][j], dp[i][j-1])
12+ */
13+ int t1Size = text1 .length (); //row
14+ int t2Size = text2 .length (); //col
15+ int [][] dp = new int [t1Size + 1 ][t2Size + 1 ];
16+
17+ for (int i = 1 ; i <= t1Size ; i ++) {
18+ for (int j = 1 ; j <= t2Size ; j ++) {
19+ if (text1 .charAt (i -1 ) == text2 .charAt (j -1 )) {
20+ dp [i ][j ] = dp [i -1 ][j -1 ] + 1 ;
21+ } else {
22+ dp [i ][j ] = Math .max (dp [i -1 ][j ], dp [i ][j -1 ]);
23+ }
24+ }
25+ }
26+ return dp [t1Size ][t2Size ];
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int characterReplacement (String s , int k ) {
3+ /**
4+ 1.problem: k번 특정 문자를 골라서 바꿀 수 있다. 이때 same letter 로 이루어진 가장 긴 substring 의 길이을 구하라.
5+ 2.constraints
6+ - s.length min = 1, max = 10^5
7+ - k.length min = 0, max = s.length
8+ 3.solution
9+ - sliding window: time O(n), space O(1)
10+ */
11+
12+ int [] table = new int [26 ];
13+ int n = s .length ();
14+ int maxFreq = 0 ;
15+ int maxLen = 0 ;
16+ int left = 0 ;
17+
18+ for (int right = 0 ; right < n ; right ++) {
19+ //1.right 확장 -> count 증가
20+ int currentChar = s .charAt (right ) - 'A' ;
21+ table [currentChar ]++;
22+
23+ //2.maxFreq update
24+ maxFreq = Math .max (maxFreq , table [currentChar ]);
25+
26+ //3.조건 깨지면 left 이동하면서 count--
27+ //바꿔야하는 문자 개수가 k 보다 큰 경우
28+ if ((right - left + 1 - maxFreq ) > k ) {
29+ int idx = s .charAt (left ) - 'A' ;
30+ table [idx ]--;
31+ left ++;
32+ }
33+
34+ //4.maxLen update
35+ maxLen = Math .max (maxLen , right - left + 1 );
36+ }
37+ return maxLen ;
38+ }
39+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int countSubstrings (String s ) {
3+ /**
4+ 1.문제: s 에서 palindromoic substring 개수 return
5+ 2.constraints:
6+ - 연속적인 문자
7+ - s.length min = 1, max = 1000
8+ 3.풀이
9+ - bruteforce: time:O(n^3), space:O(n)
10+ - expand around : time(On^2), space:O(1)
11+ */
12+
13+ int n = s .length ();
14+ if (n == 1 ) return 1 ;
15+ int count = 0 ;
16+
17+ for (int i = 0 ; i < n ; i ++) {
18+ //odd
19+ count += checkPalindrome (s , i , i );
20+
21+ //even
22+ count += checkPalindrome (s , i , i +1 );
23+ }
24+ return count ;
25+
26+ }
27+
28+ private int checkPalindrome (String s , int left , int right ) {
29+ int count = 0 ;
30+ while (left >= 0 && right < s .length () && s .charAt (right ) == s .charAt (left )) {
31+ count += 1 ;
32+ left -= 1 ;
33+ right += 1 ;
34+ }
35+ return count ;
36+ }
37+
38+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int reverseBits (int n ) {
3+ int result = 0 ;
4+ for (int i = 0 ; i < 32 ; i ++) {
5+ result <<= 1 ; //왼쪽 이동
6+ result |= (n & 1 ); //마지막 비트 붙이기
7+ n >>= 1 ; // 오른쪽 이동
8+ }
9+ return result ;
10+ }
11+ }
You can’t perform that action at this time.
0 commit comments