File tree Expand file tree Collapse file tree 5 files changed +128
-0
lines changed Expand file tree Collapse file tree 5 files changed +128
-0
lines changed Original file line number Diff line number Diff line change
1
+ TreeNode* sortedArrayToBST (vector<int >& nums) {
2
+ int len = nums.size ();
3
+ return generate (nums, 0 , len-1 );
4
+ }
5
+
6
+ TreeNode* generate (vector<int >& nums, int p, int q)
7
+ {
8
+ TreeNode* node = nullptr ;
9
+ if (p <= q)
10
+ {
11
+ int lim = (p+q)/2 ;
12
+ node = new TreeNode (nums[lim]);
13
+ node->left = generate (nums, p, lim-1 );
14
+ node->right = generate (nums, lim+1 , q);
15
+ }
16
+
17
+ return node;
18
+ }
Original file line number Diff line number Diff line change
1
+ bool checkString (string s) {
2
+ int len = s.size ();
3
+ int i = 0 ;
4
+ while (i < len && s[i] == ' a' )
5
+ ++i;
6
+ if (i >= len-1 )
7
+ return true ;
8
+ for (; i < len; ++i)
9
+ {
10
+ if (s[i] == ' a' )
11
+ return false ;
12
+ }
13
+
14
+ return true ;
15
+ }
Original file line number Diff line number Diff line change
1
+ int numberOfBeams (vector<string>& bank) {
2
+ int row = bank.size ();
3
+ if (row == 1 )
4
+ return 0 ;
5
+ int col = bank[0 ].size ();
6
+ vector<int > devices (row, 0 );
7
+ for (int i = 0 ; i < row; ++i)
8
+ {
9
+ for (int j = 0 ; j < col; ++j)
10
+ {
11
+ if (bank[i][j] == ' 1' )
12
+ devices[i]++;
13
+ }
14
+ }
15
+
16
+ int i = 0 , j = 1 ;
17
+ int res = 0 ;
18
+ while (i < row-1 && devices[i] == 0 )
19
+ ++i;
20
+ while (i < row-1 )
21
+ {
22
+ j = i+1 ;
23
+ while (j < row && devices[j] == 0 )
24
+ ++j;
25
+ if (j == row)
26
+ break ;
27
+ res += devices[i]*devices[j];
28
+ i = j;
29
+ }
30
+
31
+ return res;
32
+ }
Original file line number Diff line number Diff line change
1
+ string capitalizeTitle (string title) {
2
+ int len = title.size ();
3
+ int i = 0 , pos = 0 ;
4
+ int n = 0 ;
5
+ while (i < len)
6
+ {
7
+ if (title[i] != ' ' )
8
+ {
9
+ title[i] = tolower (title[i]);
10
+ ++n;
11
+ }
12
+ else
13
+ {
14
+ if (n > 2 )
15
+ title[pos] = toupper (title[pos]);
16
+ n = 0 ;
17
+ pos = i+1 ;
18
+ }
19
+ ++i;
20
+ }
21
+
22
+ if (n > 2 )
23
+ title[pos] = toupper (title[pos]);
24
+
25
+ return title;
26
+ }
Original file line number Diff line number Diff line change
1
+ bool checkValid (vector<vector<int >>& matrix) {
2
+ int n = matrix.size ();
3
+ vector<int > flag (n, 0 );
4
+ for (int i = 0 ; i < n; ++i)
5
+ {
6
+ flag.assign (n, 0 );
7
+ int ct = 0 ;
8
+ for (int j = 0 ; j < n; ++j)
9
+ {
10
+ if (flag[matrix[i][j]-1 ] == 0 )
11
+ {
12
+ flag[matrix[i][j]-1 ] = 1 ;
13
+ ++ct;
14
+ }
15
+ }
16
+ if (ct != n)
17
+ return false ;
18
+ }
19
+
20
+ for (int i = 0 ; i < n; ++i)
21
+ {
22
+ flag.assign (n, 0 );
23
+ int ct = 0 ;
24
+ for (int j = 0 ; j < n; ++j)
25
+ {
26
+ if (flag[matrix[j][i]-1 ] == 0 )
27
+ {
28
+ flag[matrix[j][i]-1 ] = 1 ;
29
+ ++ct;
30
+ }
31
+ }
32
+ if (ct != n)
33
+ return false ;
34
+ }
35
+
36
+ return true ;
37
+ }
You can’t perform that action at this time.
0 commit comments