File tree Expand file tree Collapse file tree 5 files changed +133
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 5 files changed +133
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int lengthOfLongestSubstring (string s) {
4+ int result = 0 ;
5+ int start = 0 ;
6+ unordered_map<char , int > map;
7+
8+ for (int end = 0 ; end < s.length (); end++){
9+ char ch = s[end];
10+
11+ if (map.count (ch) && map[ch] >= start)
12+ start = map[ch] + 1 ;
13+
14+ map[ch] = end;
15+ result = max (result, end - start + 1 );
16+ }
17+
18+ return result;
19+ }
20+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ void search (int r, int c, vector<vector<char >>& grid){
4+ if (r < 0 || c < 0 || r >= grid.size () || c >= grid[r].size () || grid[r][c] == ' 0' )
5+ return ;
6+
7+ grid[r][c] = ' 0' ;
8+
9+ search (r-1 , c, grid);
10+ search (r+1 , c, grid);
11+ search (r, c-1 , grid);
12+ search (r, c+1 , grid);
13+ }
14+
15+ int numIslands (vector<vector<char >>& grid) {
16+ int cnt = 0 ;
17+
18+ for (int i = 0 ; i < grid.size (); i++){
19+ for (int j = 0 ; j < grid[i].size (); j++){
20+ if (grid[i][j] == ' 1' ){
21+ search (i, j, grid);
22+ cnt++;
23+ }
24+ }
25+ }
26+
27+ return cnt;
28+ }
29+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ ListNode* reverseList (ListNode* head) {
4+ ListNode* new_head = NULL ;
5+
6+ while (head){
7+ ListNode* next = head->next ;
8+
9+ head->next = new_head;
10+ new_head = head;
11+ head = next;
12+ }
13+
14+ return new_head;
15+ }
16+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ void setZeroes (vector<vector<int >>& matrix) {
4+ int rows = matrix.size ();
5+ int cols = matrix[0 ].size ();
6+
7+ bool firstRowZero = false ;
8+ bool firstColZero = false ;
9+
10+ for (int j = 0 ; j < cols; j++){
11+ if (matrix[0 ][j] == 0 )
12+ firstRowZero = true ;
13+ }
14+
15+ for (int i = 0 ; i < rows; i++){
16+ if (matrix[i][0 ] == 0 )
17+ firstColZero = true ;
18+ }
19+
20+ for (int i = 1 ; i < rows; i++){
21+ for (int j = 1 ; j < cols; j++){
22+ if (matrix[i][j] == 0 ){
23+ matrix[i][0 ] = 0 ;
24+ matrix[0 ][j] = 0 ;
25+ }
26+ }
27+ }
28+
29+ for (int i = 1 ; i < matrix.size (); i++){
30+ for (int j = 1 ; j < matrix[0 ].size (); j++){
31+ if (matrix[0 ][j] == 0 || matrix[i][0 ] == 0 )
32+ matrix[i][j] = 0 ;
33+ }
34+ }
35+
36+ if (firstRowZero){
37+ for (int j = 0 ; j < cols; j++)
38+ matrix[0 ][j] = 0 ;
39+ }
40+
41+ if (firstColZero){
42+ for (int i = 0 ; i < rows; i++)
43+ matrix[i][0 ] = 0 ;
44+ }
45+ }
46+ };
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int uniquePaths (int m, int n) {
4+ vector<vector<int >> dp (m, vector<int >(n, 0 ));
5+
6+ dp[0 ][0 ] = 1 ;
7+
8+ for (int i = 1 ; i < m; i++)
9+ dp[i][0 ] = 1 ;
10+
11+ for (int j = 1 ; j < n; j++)
12+ dp[0 ][j] = 1 ;
13+
14+ for (int i = 1 ; i < m; i++){
15+ for (int j = 1 ; j < n; j++){
16+ dp[i][j] = dp[i - 1 ][j] + dp[i][j - 1 ];
17+ }
18+ }
19+
20+ return dp[m - 1 ][n - 1 ];
21+ }
22+ };
You can’t perform that action at this time.
0 commit comments