'알고리즘 > 리트코드' 카테고리의 다른 글
5. Longest Palindromic Substring (0) | 2023.03.18 |
---|---|
392. Is Subsequence (0) | 2023.03.14 |
1480. Running Sum of 1d Array (0) | 2023.03.08 |
724. Find Pivot Index (0) | 2023.03.08 |
409. Longest Palindrome (0) | 2023.02.28 |
5. Longest Palindromic Substring (0) | 2023.03.18 |
---|---|
392. Is Subsequence (0) | 2023.03.14 |
1480. Running Sum of 1d Array (0) | 2023.03.08 |
724. Find Pivot Index (0) | 2023.03.08 |
409. Longest Palindrome (0) | 2023.02.28 |
basic_string substr(size_type pos = 0, size_type count = npos) const;
문자열의 일부를 리턴한다.
문자열의 pos 번째 문자 부터 count 길이 만큼의 문자열을 리턴한다. 만약에, 인자로 전달된 부분 문자열의 길이가 문자열 보다 길다면, 그 이상을 반환하지 않고 문자열의 끝 까지만 리턴한다.
또한, count 로 npos 를 전달한다면, 자동으로 pos 부터 원래 문자열의 끝 까지 리턴한다.
Tip
가지고 있는 Substring 에서 현재의 문자를 추가한것과 Substring의 앞의 문자도 추가한 것을 조건문 두개로 하여,
이렇게만 해도 되는 이유는 긴 문자 가운데 Palidromic 이 있다면 앞에 문자를 더해가면서 반드시 찾을 수 있게 된다
dasfdasf123321dsafasdf
3
33, 233
332, 2332,
23321, 123321
글자 끝까지 돌아 제일 긴 Palindromic 문자를 반환한다.
For 문 첫번째 b
For 문 두번째 ba ,
For 문 세번째 ab, bab
For 문 세번째 baba
For 문 네번째 babad
49. Group Anagrams (0) | 2023.03.20 |
---|---|
392. Is Subsequence (0) | 2023.03.14 |
1480. Running Sum of 1d Array (0) | 2023.03.08 |
724. Find Pivot Index (0) | 2023.03.08 |
409. Longest Palindrome (0) | 2023.02.28 |
49. Group Anagrams (0) | 2023.03.20 |
---|---|
5. Longest Palindromic Substring (0) | 2023.03.18 |
1480. Running Sum of 1d Array (0) | 2023.03.08 |
724. Find Pivot Index (0) | 2023.03.08 |
409. Longest Palindrome (0) | 2023.02.28 |
5. Longest Palindromic Substring (0) | 2023.03.18 |
---|---|
392. Is Subsequence (0) | 2023.03.14 |
724. Find Pivot Index (0) | 2023.03.08 |
409. Longest Palindrome (0) | 2023.02.28 |
231.Power of Two (0) | 2023.02.28 |
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int total = 0;
for (int num : nums) {
total += num;
}
int leftSum = 0;
for (int i = 0; i < nums.size(); i++) {
int rightSum = total - leftSum - nums[i];
if (leftSum == rightSum) {
return i;
}
leftSum += nums[i];
}
return -1;
}
};
rightSum 이 total - leftSum - nums[i]; 라는 사실이 중요하다
[1,7,3,6,5,6]
#1
leftSum = 0 , num[0] = 1, rightSum = total - leftSum - num[0] = num[1] + num[2] + num[3] + num[4] + num[5];
#2
[1] [7] [3,6,5,6]
leftSum = 0 + num[0], num[1] = 7, rightSum = total - leftSum - num[1] = num[2] + num[3] + num[4] + num[5];
392. Is Subsequence (0) | 2023.03.14 |
---|---|
1480. Running Sum of 1d Array (0) | 2023.03.08 |
409. Longest Palindrome (0) | 2023.02.28 |
231.Power of Two (0) | 2023.02.28 |
122. Best Time to Buy and Sell Stock II (0) | 2021.09.06 |
392. Is Subsequence (0) | 2023.03.14 |
---|---|
1480. Running Sum of 1d Array (0) | 2023.03.08 |
724. Find Pivot Index (0) | 2023.03.08 |
231.Power of Two (0) | 2023.02.28 |
122. Best Time to Buy and Sell Stock II (0) | 2021.09.06 |
n 이 2의 지수로 표현이 되는 지 확인.
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
if n <= 0:
return False
## 전제 조건 2의 지수는 2로 계속 정확히 나눠진다.
while n > 1: ## 몫이 1 보다 클때
if n % 2 == 1: ##몫이 홀수라면 False
return False
n = n // 2 ## 몫을 다시 대입
return True
392. Is Subsequence (0) | 2023.03.14 |
---|---|
1480. Running Sum of 1d Array (0) | 2023.03.08 |
724. Find Pivot Index (0) | 2023.03.08 |
409. Longest Palindrome (0) | 2023.02.28 |
122. Best Time to Buy and Sell Stock II (0) | 2021.09.06 |
You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
Example 1:
Input: prices = [7,1,5,3,6,4] Output: 7 Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. Total profit is 4 + 3 = 7.
Example 2:
Input: prices = [1,2,3,4,5] Output: 4 Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Total profit is 4.
Example 3:
Input: prices = [7,6,4,3,1] Output: 0 Explanation: There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
Constraints:
392. Is Subsequence (0) | 2023.03.14 |
---|---|
1480. Running Sum of 1d Array (0) | 2023.03.08 |
724. Find Pivot Index (0) | 2023.03.08 |
409. Longest Palindrome (0) | 2023.02.28 |
231.Power of Two (0) | 2023.02.28 |