-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
41 changed files
with
3,552 additions
and
0 deletions.
There are no files selected for viewing
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,88 @@ | ||
/*228. Summary Ranges QuestionEditorial Solution My Submissions | ||
Total Accepted: 58494 Total Submissions: 219243 Difficulty: Medium | ||
Given a sorted integer array without duplicates, return the summary of its ranges. | ||
For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. | ||
Credits: | ||
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. | ||
Hide Company Tags Google | ||
Hide Tags Array | ||
Hide Similar Problems (M) Missing Ranges (H) Data Stream as Disjoint Intervals | ||
*/ | ||
|
||
|
||
|
||
|
||
/*for each element, initialize the step as 1 | ||
check how large it can range, and put this range into the ret array*/ | ||
|
||
///////////////////////////////////////////////////////////////////////////////////// | ||
//C++ | ||
class Solution { | ||
public: | ||
vector<string> summaryRanges(vector<int>& nums) { | ||
vector<string> ret; | ||
int i = 0; | ||
int n = nums.size(); | ||
while(i<n){ | ||
int j = 1; //initialize the step | ||
while(i+j<n && nums[i+j]-nums[i]==j) ++j;//set how large this element can go | ||
ret.push_back(j<=1 ? to_string(nums[i]) : to_string(nums[i])+"->"+to_string(nums[i+j-1])); //remember -1, before is ++j | ||
i += j; | ||
} | ||
|
||
return ret; | ||
} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
vector<string> summaryRanges(vector<int>& nums) { | ||
int nsize = nums.size(); | ||
vector<string> nrange; //the vector is a string vector | ||
|
||
for(int i = 0; i < nsize;){ | ||
int start = i, end = i; | ||
while(end+1<nsize && nums[end+1]==nums[end]+1 ) end++; | ||
if(end > start) nrange.push_back(to_string(nums[start])+"->"+to_string(nums[end])); //nums[start] not start | ||
else nrange.push_back(to_string(nums[start])); | ||
i = end +1; //through this to control the next value | ||
} | ||
return nrange; | ||
} | ||
}; | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
///////////////////////////////////////////////////////////////////////////////////// | ||
//Java | ||
public class Solution { | ||
public List<String> summaryRanges(int[] nums) { | ||
List<String> ret = new ArrayList<>(); | ||
if(nums==null || nums.length==0) return ret; | ||
|
||
int i = 0; | ||
int n = nums.length; | ||
while(i<n){ | ||
int j = 1; | ||
while(i+j<n && nums[i+j]-nums[i]==j) ++j; | ||
ret.add(j<=1 ? Integer.toString(nums[i]) : | ||
Integer.toString(nums[i])+"->"+Integer.toString(nums[i+j-1])); | ||
/*if(step>=2){ | ||
ret.add(Integer.toString(nums[start]) + "->" + | ||
Integer.toString(nums[start+step-1])); | ||
} else if(step==1){ | ||
ret.add(Integer.toString(nums[start])); | ||
}*/ | ||
i += j; | ||
} | ||
|
||
return ret; | ||
} | ||
} |
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,143 @@ | ||
/*130. Surrounded Regions QuestionEditorial Solution My Submissions | ||
Total Accepted: 60534 | ||
Total Submissions: 362069 | ||
Difficulty: Medium | ||
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. | ||
A region is captured by flipping all 'O's into 'X's in that surrounded region. | ||
For example, | ||
X X X X | ||
X O O X | ||
X X O X | ||
X O X X | ||
After running your function, the board should be: | ||
X X X X | ||
X X X X | ||
X X X X | ||
X O X X | ||
Subscribe to see which companies asked this question | ||
Show Tags | ||
Show Similar Problems | ||
*/ | ||
|
||
|
||
/*First, check the four border of the matrix. If there is a element is | ||
'O', alter it and all its neighbor 'O' elements to '1'. | ||
Then ,alter all the 'O' to 'X' | ||
At last,alter all the '1' to 'O'*/ | ||
|
||
///////////////////////////////////////////////////////////////////////////////////// | ||
//C++ | ||
class Solution { | ||
public: | ||
void solve(vector<vector<char>>& board) { | ||
int i, j; | ||
int row = board.size(); | ||
if(!row){ | ||
return; | ||
} | ||
|
||
int col = board[0].size(); | ||
for(i = 0; i < row; i++){ | ||
check(board, i, 0, row, col); | ||
if(col>1){ | ||
check(board, i, col-1, row, col); | ||
} | ||
} | ||
|
||
for(j = 1; j + 1 < col; j++){ | ||
check(board, 0, j, row, col); | ||
if(row>1){ | ||
check(board, row-1, j, row, col); | ||
} | ||
} | ||
|
||
for(i = 0; i < row; i++){ | ||
for(j = 0; j < col; j++){ | ||
if(board[i][j] == 'O'){ | ||
board[i][j] = 'X'; | ||
} | ||
} | ||
} | ||
|
||
for(i = 0; i < row; i++){ | ||
for(j = 0; j < col; j++){ | ||
if(board[i][j] == '1'){ | ||
board[i][j] = 'O'; | ||
} | ||
} | ||
} | ||
} | ||
|
||
void check(vector<vector<char> > &board, int i, int j, int row, int col){ | ||
if(board[i][j]=='O'){ | ||
board[i][j] = '1'; | ||
if(i>1) check(board, i-1, j, row, col); | ||
if(j>1) check(board, i, j-1, row, col); | ||
if(i+1<row) check(board, i+1, j, row, col); | ||
if(j+1<col) check(board, i, j+1, row, col); | ||
} | ||
} | ||
}; | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
///////////////////////////////////////////////////////////////////////////////////// | ||
//Java | ||
public class Solution { | ||
public void solve(char[][] board) { | ||
int i, j; | ||
int row = board.length; | ||
if(row == 0){ | ||
return; | ||
} | ||
|
||
int col = board[0].length; | ||
for(i = 0; i < row; i++){ | ||
check(board, i, 0, row, col); | ||
if(col>1){ | ||
check(board, i, col-1, row, col); | ||
} | ||
} | ||
|
||
for(j = 1; j + 1 < col; j++){ | ||
check(board, 0, j, row, col); | ||
if(row>1){ | ||
check(board, row-1, j, row, col); | ||
} | ||
} | ||
|
||
for(i = 0; i < row; i++){ | ||
for(j = 0; j < col; j++){ | ||
if(board[i][j] == 'O'){ | ||
board[i][j] = 'X'; | ||
} | ||
} | ||
} | ||
|
||
for(i = 0; i < row; i++){ | ||
for(j = 0; j < col; j++){ | ||
if(board[i][j] == '1'){ | ||
board[i][j] = 'O'; | ||
} | ||
} | ||
} | ||
} | ||
|
||
public void check(char[][] board, int i, int j, int row, int col){ | ||
if(board[i][j] == 'O'){ | ||
board[i][j] = '1'; | ||
if(i>1) check(board, i-1, j, row, col); | ||
if(j>1) check(board, i, j-1, row, col); | ||
if(i+1<row) check(board, i+1, j, row, col); | ||
if(j+1<col) check(board, i, j+1, row, col); | ||
} | ||
} | ||
} |
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,101 @@ | ||
/* | ||
Explanation | ||
The switch statement evaluates an expression, matches the expression's value to a case clause, and executes the statements associated with that particular case. | ||
Switch Statements | ||
Here's a useful video on the topic: | ||
A switch statement first evaluates its expression. | ||
Then, it looks for the first case clause whose expression matches the same value as the result of the input expression (using strict comparison, ===). | ||
Finally, it transfers control to that clause. | ||
Syntax | ||
switch (expression) { | ||
case value1: | ||
//Statements executed when the result of expression matches value1 | ||
[break;] | ||
case value2: | ||
//Statements executed when the result of expression matches value2 | ||
[break;] | ||
... | ||
case valueN: | ||
//Statements executed when the result of expression matches valueN | ||
[break;] | ||
default: | ||
//Statements executed when none of the values match the value of the expression | ||
[break;] | ||
} | ||
With each case label, there is an optional break statement to ensure that the program breaks out of the switch once the matching statement is performed and continues execution at the statement following the switch. | ||
If the break is omitted, the program continues execution at the next statement within the switch statement. | ||
Example | ||
switch (day) { //day is an integer varying from 0 to 6. | ||
case 6: | ||
text = "Today is Saturday"; | ||
break; | ||
case 0: | ||
text = "Today is Sunday"; | ||
break; | ||
default: | ||
text = "Looking forward to the Weekend"; | ||
} | ||
Task | ||
You are given a variable num. Your task is to print: | ||
- ONE, if num is equal to 11. | ||
- TWO, if num is equal to 22. | ||
- THREE, if num is equal to 33. | ||
- FOUR, if num is equal to 44. | ||
- FIVE, if num is equal to 55. | ||
- SIX, if num is equal to 66. | ||
- SEVEN, if num is equal to 77. | ||
- EIGHT, if num is equal to 88. | ||
- NINE, if num is equal to 99. | ||
- PLEASE TRY AGAIN, if num is none of the above. | ||
Note | ||
Do not declare the variable num. It is declared inside our code checker. | ||
Use console.log for printing statements to the console. | ||
*/ | ||
|
||
//Do not declare variable num. | ||
//Write your code below this line. | ||
switch(num){ | ||
case 1: | ||
console.log("ONE"); | ||
break; | ||
case 2: | ||
console.log("TWO"); | ||
break; | ||
case 3: | ||
console.log("THREE"); | ||
break; | ||
case 4: | ||
console.log("FOUR"); | ||
break; | ||
case 5: | ||
console.log("FIVE"); | ||
break; | ||
case 6: | ||
console.log("SIX"); | ||
break; | ||
case 7: | ||
console.log("SEVEN"); | ||
break; | ||
case 8: | ||
console.log("EIGHT"); | ||
break; | ||
case 9: | ||
console.log("NINE"); | ||
break; | ||
default: | ||
console.log("PLEASE TRY AGAIN"); | ||
} |
Oops, something went wrong.