File tree 4 files changed +122
-0
lines changed 4 files changed +122
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 완전탐색을 통해 모든 경우의 수를 구하기 위해 노력하였지만 시간초과가 발생하였습니다.
2
+ // dfs를 통해 풀이하려고 했지만 O(2^N)의 시간복잡도로 인해 시간초과가 발생하였습니다.
3
+ // 이후 dp로 풀이를 시작하였고 어렵지 않게 풀이하였습니다.
4
+ // dp[i] = dp[i-1] + dp[i-2]로 풀이하였습니다.
5
+ // 이때 i번째 문자열을 1자리로 취급할지 2자리로 취급할지에 따라 경우의 수가 달라집니다.
6
+ // 1자리로 취급할 경우 1~9까지 가능하고
7
+ // 2자리로 취급할 경우 10~26까지 가능합니다.
8
+
9
+ // 시간복잡도 : O(N)
10
+ // 공간복잡도 : O(N)
11
+ class SolutionGotprgmer {
12
+ public int numDecodings (String s ) {
13
+ // 예외 처리: 문자열이 "0"으로 시작하거나 빈 문자열이면
14
+ if (s == null || s .length () == 0 || s .charAt (0 ) == '0' ) {
15
+ return 0 ;
16
+ }
17
+ int [] dp = new int [s .length ()+1 ];
18
+ dp [0 ] = 1 ;
19
+ for (int i =0 ;i <s .length ();i ++){
20
+ int ith = s .charAt (i )-'0' ;
21
+ if (ith != 0 ){
22
+ dp [i +1 ] = dp [i ];
23
+ }
24
+ if (i >0 ){
25
+ String twoDigitStr = s .substring (i -1 ,i +1 );
26
+ int twoDigitNum = Integer .valueOf (twoDigitStr );
27
+ if (twoDigitNum >=10 && twoDigitNum <27 ){
28
+ dp [i +1 ] += dp [i -1 ];
29
+ }
30
+ }
31
+
32
+ }
33
+ return dp [s .length ()];
34
+ }
35
+
36
+
37
+ }
Original file line number Diff line number Diff line change
1
+ // 단순하게 정렬해서 일치하지 않으면 출력하고 리스트를 벗어나면 그대로 checkNum을 출력하는 방식
2
+ // 시간복잡도 : O(NlogN)
3
+ // 공간복잡도 : O(1)
4
+
5
+ class SolutionGotprgmer {
6
+ public int missingNumber (int [] nums ) {
7
+ Arrays .sort (nums );
8
+ int checkNum = 0 ;
9
+ for (int i =0 ;i <nums .length ;i ++){
10
+ if (nums [i ] != checkNum ){
11
+ return checkNum ;
12
+ }
13
+ checkNum += 1 ;
14
+ }
15
+ return checkNum ;
16
+
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ // 처음 문제를 봤을때는 이해가 잘 가지 않았지만,
2
+ // 비트들을 뒤집으라는 설명으로 풀었던 것 같다.
3
+ // Integer.reverse() 메소드를 사용하여 풀었다.
4
+ // 지피티의 도움으로 Integer.reverse()를 사용하라는 힌트를 얻었다.
5
+ // 찾아보니 reverse(N)는 N을 2의 보수 비트로 바꾸고 그것을 뒤집는 방식이었다.
6
+ // 시간복잡도 : O(1) -> Integer가 32비트 고정이라서 O(1)
7
+ // 공간복잡도 : O(1) -> 32비트 고정
8
+ public class Solution {
9
+ // you need treat n as an unsigned value
10
+ public int reverseBits (int n ) {
11
+ return Integer .reverse (n );
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ // 배열을 정렬하여 투포인터로 접근하여 풀었습니다.
2
+ // 정렬된 배열의 인덱스를 찾기 위해 indexOf 메소드를 만들어서 사용했습니다.
3
+
4
+ // 시간복잡도 : O(NlogN) -> 정렬을 위해 O(NlogN) + 투포인터로 O(N)이므로 O(NlogN)
5
+ // 공간복잡도 : O(N) -> 정렬을 위해 복사한 배열이 필요하므로 O(N)
6
+ class SolutionGotprgmer {
7
+ public int [] twoSum (int [] nums , int target ) {
8
+ int [] original = new int [nums .length ];
9
+
10
+ for (int i =0 ;i <nums .length ;i ++){
11
+ original [i ] = nums [i ];
12
+ }
13
+ Arrays .sort (nums );
14
+
15
+ int l = 0 ;
16
+ int r = nums .length -1 ;
17
+ while (l <r ){
18
+ int lV = nums [l ];
19
+ int rV = nums [r ];
20
+ int total = lV + rV ;
21
+ if (total > target ){
22
+ r -= 1 ;
23
+ }
24
+ else if (total < target ){
25
+ l += 1 ;
26
+ }
27
+ else {
28
+ int [] ans = indexOf (lV ,rV ,original );
29
+ l = ans [0 ];
30
+ r = ans [1 ];
31
+ break ;
32
+ }
33
+ }
34
+ return new int [] {l ,r };
35
+ }
36
+
37
+ public int [] indexOf (int l ,int r , int [] nums ){
38
+ int lIdx = -1 ;
39
+ int rIdx = -1 ;
40
+ for (int i = 0 ;i <nums .length ;i ++){
41
+ if (nums [i ] == l ){
42
+ lIdx = i ;
43
+ break ;
44
+ }
45
+ }
46
+ for (int i = nums .length -1 ;i >-1 ;i --){
47
+ if (nums [i ] == r ){
48
+ rIdx = i ;
49
+ break ;
50
+ }
51
+ }
52
+ return new int [] {lIdx ,rIdx };
53
+ }
54
+ }
You can’t perform that action at this time.
0 commit comments