File tree Expand file tree Collapse file tree 5 files changed +127
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree 5 files changed +127
-0
lines changed 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+ Map <Node , Node > visited = new HashMap <>();
23+
24+ public Node cloneGraph (Node node ) {
25+ if (node == null ) return null ;
26+
27+ if (visited .containsKey (node )) {
28+ return visited .get (node );
29+ }
30+
31+ Node clone = new Node (node .val );
32+ visited .put (node , clone );
33+
34+ for (Node neighbor : node .neighbors ) {
35+ clone .neighbors .add (cloneGraph (neighbor ));
36+ }
37+
38+ return clone ;
39+ }
40+ }
41+
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int longestCommonSubsequence (String text1 , String text2 ) {
3+ int M = text1 .length ();
4+ int N = text2 .length ();
5+
6+ int [][] dp = new int [M + 1 ][N + 1 ];
7+
8+ for (int i = 1 ; i <= M ; i ++) {
9+ for (int j = 1 ; j <= N ; j ++) {
10+ if (text1 .charAt (i - 1 ) == text2 .charAt (j - 1 )) {
11+ // 문자가 일치하면 대각선 값 + 1
12+ dp [i ][j ] = dp [i - 1 ][j - 1 ] + 1 ;
13+ } else {
14+ // 일치하지 않으면 왼쪽 또는 위쪽 값 중 큰 값 선택
15+ dp [i ][j ] = Math .max (dp [i - 1 ][j ], dp [i ][j - 1 ]);
16+ }
17+ }
18+ }
19+
20+ // 최종 결과
21+ return dp [M ][N ];
22+ }
23+ }
24+
Original file line number Diff line number Diff line change 1+ import java .util .HashMap ;
2+ import java .util .Map ;
3+
4+ public class Solution {
5+ public int characterReplacement (String s , int k ) {
6+ int maxLen = 0 ;
7+ int maxCount = 0 ;
8+ Map <Character , Integer > counter = new HashMap <>();
9+ int start = 0 ;
10+
11+ for (int end = 0 ; end < s .length (); end ++) {
12+ char endChar = s .charAt (end );
13+ counter .put (endChar , counter .getOrDefault (endChar , 0 ) + 1 );
14+ maxCount = Math .max (maxCount , counter .get (endChar ));
15+
16+ while (end - start + 1 - maxCount > k ) {
17+ char startChar = s .charAt (start );
18+ counter .put (startChar , counter .get (startChar ) - 1 );
19+ start ++;
20+ }
21+
22+ maxLen = Math .max (maxLen , end - start + 1 );
23+ }
24+
25+ return maxLen ;
26+ }
27+ }
28+
29+
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int countSubstrings (String s ) {
3+ int count = 0 ;
4+
5+ for (int i = 0 ; i < 2 * s .length () - 1 ; i ++) {
6+ int left = i / 2 ;
7+ int right = left + i % 2 ;
8+
9+ while (left >= 0 && right < s .length () && s .charAt (left ) == s .charAt (right )) {
10+ count ++;
11+ left --;
12+ right ++;
13+ }
14+ }
15+
16+ return count ;
17+ }
18+ }
19+
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ // you need treat n as an unsigned value
3+ public int reverseBits (int n ) {
4+ String binary = String .format ("%32s" , Integer .toBinaryString (n )).replace (' ' , '0' );
5+ System .out .println (binary );
6+
7+ StringBuilder sb = new StringBuilder (binary );
8+ sb .reverse ();
9+
10+
11+ return Integer .parseUnsignedInt (sb .toString (),2 );
12+ }
13+ }
14+
You can’t perform that action at this time.
0 commit comments