Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7077d9f

Browse files
authored
Merge pull requestneetcode-gh#1575 from kabirdhillon7/734-C++
Create 724-Find-Pivot-Index.cpp
2 parentsa68908e +eb2c7c1 commit7077d9f

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

‎cpp/605-Can-Place-Flowers.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
classSolution {
2+
public:
3+
boolcanPlaceFlowers(vector<int>& flowerbed,int n) {
4+
// pre-check, if n is 0 ... return true
5+
if(n ==0){
6+
returntrue;
7+
}
8+
9+
// add a zero to the front and end of FB
10+
flowerbed.insert (flowerbed.begin(),0);
11+
flowerbed.push_back(0);
12+
13+
// iterate through vector (1, vector -1) (for)
14+
// if prev, curr, and next are 0
15+
// plant flower (1)
16+
// decrement n
17+
for(int i =1; i < flowerbed.size() -1; i++){
18+
if (flowerbed[i -1] ==0 && flowerbed[i] ==0 && flowerbed[i+1] ==0){
19+
flowerbed[i] =1;
20+
n--;
21+
}
22+
if (n ==0){
23+
returntrue;
24+
}
25+
26+
}
27+
28+
// return false as else
29+
returnfalse;
30+
31+
}
32+
};

‎cpp/724-Find-Pivot-Index.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
classSolution {
2+
public:
3+
intpivotIndex(vector<int>& nums) {
4+
int total;
5+
for(int x: nums){
6+
total += x;
7+
}
8+
9+
int leftSum =0;
10+
int rightSum;
11+
12+
for(int i =0; i < nums.size(); i++){
13+
rightSum = total - nums[i] - leftSum;
14+
15+
if(leftSum == rightSum){
16+
return i;
17+
}
18+
19+
leftSum += nums[i];
20+
}
21+
22+
return -1;
23+
}
24+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp