File tree 3 files changed +103
-0
lines changed
3 files changed +103
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < vector>
3
+ #include < climits>
4
+
5
+ using namespace std ;
6
+
7
+
8
+ /*
9
+ TC: O(n)
10
+ SC: O(1)
11
+ ํ์ด ๋ฐฉ๋ฒ:
12
+ - sum์ ํ์ฌ ๊ฐ์ ๋ํด๊ฐ๋ฉด์ ์ต๋๊ฐ์ ๊ฐฑ์ ํ๋ค
13
+ - ๋์ ํฉ์ด ์์๊ฐ ๋์ ๋ 0์ผ๋ก ๋ฆฌ์
ํ๋ค
14
+
15
+ ๊ณ ๋ฏผํ๋ ์ผ์ด์ค(left์ right ํฌ์ธํฐ๋ฅผ ๋๊ณ ํ์์ ๋):
16
+ [-2, -1]
17
+ [-1, -2]
18
+ [-2, 1]
19
+ [-1, 1, 2, 1]
20
+ */
21
+ class Solution {
22
+ public:
23
+ int maxSubArray (vector<int >& nums) {
24
+
25
+ int max = nums[0 ];
26
+ int sum = 0 ;
27
+
28
+ for (int i = 0 ; i < nums.size (); i++)
29
+ {
30
+ sum += nums[i];
31
+ if (sum > max)
32
+ max = sum;
33
+ if (sum < 0 )
34
+ sum = 0 ;
35
+ }
36
+ return (max);
37
+ }
38
+ };
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+
3
+ using namespace std ;
4
+
5
+ /*
6
+ TC: O(1)
7
+ SC: O(1)
8
+ ํ์ด๋ฐฉ๋ฒ:
9
+ - n์ ๋นํธ๋ฅผ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋์ํค๋ฉด์ ์ตํ์ ๋นํธ๊ฐ 1์ธ์ง ํ์ธํ๋ค
10
+ - n์ด intํ์ด๋ฏ๋ก cnt๋ 32๋ฅผ ๋์ ์ ์๋ค
11
+ */
12
+
13
+ class Solution {
14
+ public:
15
+ int hammingWeight (int n) {
16
+ int cnt = 0 ;
17
+
18
+ while (n && cnt <= 31 )
19
+ {
20
+ if (n & 1 )
21
+ cnt++;
22
+ n >>= 1 ;
23
+ }
24
+ return (cnt);
25
+ }
26
+ };
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < string>
3
+ #include < cctype>
4
+
5
+ using namespace std ;
6
+
7
+ /*
8
+ TC: O(n)
9
+ start, end ํฌ์ธํฐ๊ฐ ๊ฐ๊ฐ ํ ๋ฒ์ฉ ์ ์ฒด ๋ฌธ์์ด์ ์ค์บํ๊ธฐ ๋๋ฌธ์
10
+ ๋ชจ๋ ๋ฌธ์๋ฅผ ์ต๋ ํ ๋ฒ์ฉ๋ง ๊ฒ์ฌํ๋ค
11
+ SC: O(1)
12
+ ํ์ด๋ฐฉ๋ฒ:
13
+ - ์ ์ชฝ์์ ํฌ์ธํฐ๊ฐ ์ด๋ํ๋ฉด์ ๋ ํฌ์ธํฐ๊ฐ ๋ง๋ ๋๊น์ง ๋ฐ๋ณตํ๋ฉฐ ๋ ๋ฌธ์๊ฐ ์ผ์นํ๋์ง ํ์ธํ๋ค
14
+ ๊ณ ๋ฏผํ๋ ์ผ์ด์ค:
15
+ - 0P
16
+ */
17
+
18
+ class Solution {
19
+ public:
20
+ bool isPalindrome (string s) {
21
+ int start = 0 ;
22
+ int end = s.size () - 1 ;
23
+
24
+ while (start < end)
25
+ {
26
+ // ascii ๋ฌธ์๊ฐ ์๋ ๊ตฌ๊ฐ ๊ฑด๋๋ฐ๊ธฐ
27
+ while (start < end && !isalnum (s[start]))
28
+ start++;
29
+ while (start < end && !isalnum (s[end]))
30
+ end--;
31
+
32
+ if (tolower (s[start]) != tolower (s[end]))
33
+ return (false );
34
+ start++;
35
+ end--;
36
+ }
37
+ return (true );
38
+ }
39
+ };
You canโt perform that action at this time.
0 commit comments