Skip to content

Commit 0d1c678

Browse files
committed
new solution
1 parent e452870 commit 0d1c678

File tree

6 files changed

+193
-0
lines changed

6 files changed

+193
-0
lines changed

118.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> generate(int numRows) {
4+
vector<vector<int>> mat(numRows);
5+
for (int i = 0; i < numRows; i++){
6+
mat[i].resize(i+1);
7+
mat[i][0] = mat[i][i] = 1;
8+
}
9+
10+
for(int i =1; i< numRows; i++)
11+
for(int j = 1; j< i; j++)
12+
mat[i][j] = mat[i-1][j-1] + mat[i-1][j];
13+
14+
return mat;
15+
16+
}
17+
};

119.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
vector<int> getRow(int rowIndex) {
4+
vector<int> result(rowIndex+1,0);
5+
result[0] = 1;
6+
for(int i = 1; i < rowIndex+1;i++)
7+
for(int j =i ; j >=1 ; j--)
8+
result[j] += result[j-1];
9+
10+
return result;
11+
}
12+
};

34.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
vector<int> searchRange(vector<int>& nums, int target) {
4+
int n = nums.size();
5+
int low = 0;
6+
int high = n - 1;
7+
int mid = 0;
8+
int index = -1;
9+
while (low <= high){
10+
mid = low + (high - low)/2;
11+
if(nums[mid] < target)
12+
low = mid+1;
13+
else if (nums[mid] == target)
14+
{
15+
index = mid;
16+
break;
17+
}
18+
else
19+
high = mid - 1;
20+
}
21+
22+
if (index != -1){
23+
int startIndex = mid;
24+
while(startIndex>0 && nums[startIndex-1] == target)
25+
startIndex--;
26+
int endIndex = mid;
27+
while(endIndex<n-1 && nums[endIndex+1] == target)
28+
endIndex++;
29+
return vector<int>{startIndex,endIndex};
30+
}
31+
else
32+
return vector<int>{-1,-1};
33+
}
34+
};

63.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
4+
int m = obstacleGrid.size();
5+
int n = obstacleGrid[0].size();
6+
vector<int> mat(n,0);
7+
mat[0] = 1;
8+
9+
for(int i = 0 ; i < m ; i++)
10+
for(int j = 0; j<n; j++){
11+
if(obstacleGrid[i][j]==1)
12+
mat[j] = 0;
13+
else if (j > 0)
14+
mat[j] = mat[j] + mat[j-1];
15+
16+
}
17+
18+
19+
return mat[n-1];
20+
21+
}
22+
};

73.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class Solution {
2+
public:
3+
void setZeroes(vector<vector<int>>& matrix) {
4+
int m = matrix.size();
5+
int n = matrix[0].size();
6+
bool row0 = false, col0 = false;
7+
8+
for(int i = 0; i < m; i++)
9+
{
10+
if (matrix[i][0] == 0)
11+
{
12+
col0 = true;
13+
break;
14+
}
15+
}
16+
17+
for(int j =0; j< n; j++)
18+
{
19+
if (matrix[0][j] == 0)
20+
{
21+
row0 = true;
22+
break;
23+
}
24+
}
25+
26+
for(int i =1; i < m; i++)
27+
for(int j=1; j<n; j++)
28+
{
29+
if (matrix[i][j] == 0)
30+
{
31+
matrix[i][0] = 0;
32+
matrix[0][j] = 0;
33+
}
34+
}
35+
36+
for(int i = 1 ; i < m; i ++)
37+
for(int j =1 ; j <n ; j++)
38+
{
39+
if (matrix[i][0] == 0 || matrix[0][j] == 0)
40+
matrix[i][j] = 0;
41+
}
42+
43+
44+
if(row0)
45+
{
46+
for(int j=0;j<n;j++){
47+
matrix[0][j] = 0;
48+
}
49+
}
50+
51+
if(col0)
52+
{
53+
for(int i =0; i<m;i++){
54+
matrix[i][0] = 0;
55+
}
56+
}
57+
58+
59+
60+
}
61+
};

78.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
public:
3+
4+
5+
vector<vector<int>> subsets(vector<int>& nums) {
6+
sort(nums.begin(),nums.end());
7+
vector<vector<int>> result;
8+
result.push_back({});
9+
int n = nums.size();
10+
for (int i = 0 ;i < n; i++){
11+
12+
vector<vector<int>> tempRes = result;
13+
int m = tempRes.size();
14+
15+
for(int j =0; j< m;j++){
16+
17+
tempRes[j].push_back(nums[i]);
18+
result.push_back(tempRes[j]);
19+
20+
}
21+
22+
}
23+
24+
25+
return result;
26+
}
27+
28+
// bit manipulation
29+
vector<vector<int>> subsets(vector<int>& nums) {
30+
int numSize = nums.size();
31+
int allSize = pow(2, numSize);
32+
sort(nums.begin(),nums.end());
33+
34+
vector<vector<int>> result(allSize, vector<int>());
35+
36+
for(int i = 0 ; i < numSize; i++)
37+
for(int j =0;j < allSize; j++){
38+
if ((j >>i)&1)
39+
result[j].push_back(nums[i]);
40+
}
41+
42+
return result;
43+
44+
}
45+
46+
47+
};

0 commit comments

Comments
 (0)