-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10d3e6f
commit d638d75
Showing
4 changed files
with
99 additions
and
26 deletions.
There are no files selected for viewing
30 changes: 12 additions & 18 deletions
30
leetcode/problems/cpp/find-the-index-of-the-first-occurrence-in-a-string.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
// by @codeAbinash | ||
// Time Complexity : O(n) | ||
// Space Complexity : O(1n) | ||
|
||
#include "vector" | ||
#include "string" | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
bool repeatedSubstringPattern(string s) { | ||
int size = s.size(); | ||
string str = s.substr(1, size - 1) + s.substr(0, size - 1); | ||
return str.find(s) != -1; | ||
} | ||
public: | ||
bool repeatedSubstringPattern(string s) { | ||
int n = s.length(); | ||
for (int i = n / 2; i >= 1; i--) | ||
if (n % i == 0) | ||
if (s.substr(0, n - i) == s.substr(i)) | ||
return true; | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// by @codeAbinash | ||
|
||
#include "vector" | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
void setZeroes(vector<vector<int>>& matrix) { | ||
int row = matrix.size(); | ||
int col = matrix[0].size(); | ||
bool isRowZero = false; | ||
|
||
// Check each element and set the first row and col to 0 | ||
for (int i = 0; i < row; i++) | ||
for (int j = 0; j < col; j++) | ||
if (matrix[i][j] == 0) { | ||
matrix[0][j] = 0; | ||
if (i > 0) | ||
matrix[i][0] = 0; | ||
else | ||
isRowZero = true; | ||
} | ||
// Set Zero | ||
for (int i = 1; i < row; i++) | ||
for (int j = 1; j < col; j++) | ||
if (matrix[i][0] == 0 || matrix[0][j] == 0) | ||
matrix[i][j] = 0; | ||
|
||
if (matrix[0][0] == 0) | ||
for (int i = 0; i < row; i++) | ||
matrix[i][0] = 0; | ||
|
||
if (isRowZero) | ||
for (int j = 0; j < col; j++) | ||
matrix[0][j] = 0; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// by @codeAbinash | ||
// Time : O(n) | ||
// Space : O(n) | ||
|
||
#include "vector" | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
vector<int> spiralOrder(vector<vector<int>>& matrix) { | ||
int left = 0, right = matrix[0].size(); | ||
int top = 0, bottom = matrix.size(); | ||
vector<int> ans; | ||
|
||
while (top < bottom && left < right) { | ||
for (int i = left; i < right; i++) | ||
ans.push_back(matrix[top][i]); | ||
top++; | ||
|
||
for (int i = top; i < bottom; i++) | ||
ans.push_back(matrix[i][right - 1]); | ||
right--; | ||
|
||
if (!(left < right && top < bottom)) | ||
break; | ||
|
||
for (int i = right - 1; i >= left; i--) | ||
ans.push_back(matrix[bottom - 1][i]); | ||
bottom--; | ||
|
||
for (int i = bottom - 1; i >= top; i--) | ||
ans.push_back(matrix[i][left]); | ||
left++; | ||
} | ||
return ans; | ||
} | ||
}; |