일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 파이썬
- shiba
- 30. Substring with Concatenation of All Words
- Protocol
- Class
- attribute
- 프로그래머스
- Substring with Concatenation of All Words
- Decorator
- 컴퓨터의 구조
- data science
- 시바견
- Regular Expression
- iterator
- 운영체제
- Convert Sorted List to Binary Search Tree
- 밴픽
- 43. Multiply Strings
- LeetCode
- t1
- 715. Range Module
- 109. Convert Sorted List to Binary Search Tree
- DWG
- Python Implementation
- kaggle
- Python
- Generator
- Python Code
- concurrency
- 315. Count of Smaller Numbers After Self
Archives
- Today
- Total
Scribbling
[C++] LeetCode 238. Product of Array Except Self 본문
https://leetcode.com/problems/product-of-array-except-self/
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
vector<int> zero_idx;
int zero_cnt = 0;
vector<int> dp1(nums.size());
vector<int> dp2(nums.size());
int prod = 1;
for (int i = 0; i < nums.size(); i++) {
int num = nums[i];
if (num == 0) {
zero_idx.push_back(i);
zero_cnt++;
}
else {
prod *= num;
dp1[i] = prod;
}
}
prod = 1;
for (int i = nums.size() - 1; i >= 0; i--) {
int num = nums[i];
if (num != 0) {
prod *= num;
dp2[i] = prod;
}
}
if (zero_cnt >= 2) {
return vector<int>(nums.size());
}
if (zero_cnt == 1) {
vector<int> ret(nums.size());
ret[zero_idx[0]] = prod;
return ret;
}
vector<int> ret(nums.size());
ret[0] = dp2[1];
ret[ret.size() - 1] = dp1[dp1.size() - 2];
for (size_t i = 1; i < nums.size() - 1; i++) {
ret[i] = dp1[i - 1] * dp2[i + 1];
}
return ret;
}
};
'Computer Science > C++' 카테고리의 다른 글
[C++] isalnum, tolower (0) | 2023.08.18 |
---|---|
[C++] LeetCode 128. Longest Consecutive Sequence (0) | 2023.08.18 |
[C++] priority queue (vector<int>) with comparator (0) | 2023.08.18 |
[C++] 49. Group Anagrams (0) | 2023.08.18 |
[C++] LeetCode 242. Valid Anagram (0) | 2023.08.18 |