File tree Expand file tree Collapse file tree 5 files changed +151
-0
lines changed Expand file tree Collapse file tree 5 files changed +151
-0
lines changed Original file line number Diff line number Diff line change
1
+ int numSplits (string s) {
2
+ int charNum1[26 ] = {0 };
3
+ int charNum2[26 ] = {0 };
4
+ int len = s.size (), ct = 0 ;
5
+ for (int i = 0 ; i < len; ++i)
6
+ {
7
+ int idx = s[i]-' a' ;
8
+ if (charNum1[idx] == 0 )
9
+ {
10
+ ++ct;
11
+ }
12
+ charNum1[idx]++;
13
+ }
14
+
15
+ int n = 0 , sum = 0 ;
16
+ for (int i = 0 ; i < len; ++i)
17
+ {
18
+ int idx = s[i]-' a' ;
19
+ if (charNum2[idx] == 0 )
20
+ {
21
+ charNum2[idx] = 1 ;
22
+ ++n;
23
+ }
24
+ if (charNum1[idx] > 0 )
25
+ {
26
+ --charNum1[idx];
27
+ if (charNum1[idx] == 0 )
28
+ --ct;
29
+ }
30
+
31
+ if (n == ct)
32
+ ++sum;
33
+ }
34
+
35
+ return sum;
36
+ }
Original file line number Diff line number Diff line change
1
+ vector<vector<char >> rotateTheBox (vector<vector<char >>& box) {
2
+ int m = box.size ();
3
+ int n = box[0 ].size ();
4
+ vector<vector<char >> res (n, vector<char >(m, ' .' ));
5
+
6
+ for (int i = 0 ; i < m; ++i)
7
+ {
8
+ int col = m-1 -i;
9
+ int j = n-1 , k = n-1 , idx = n;
10
+ bool flag = false ;
11
+ while (j >= 0 )
12
+ {
13
+ if (box[i][j] == ' .' )
14
+ {
15
+ if (!flag)
16
+ {
17
+ idx = j;
18
+ flag = true ;
19
+ }
20
+ }
21
+ else if (box[i][j] == ' *' )
22
+ {
23
+ idx = n;
24
+ flag = false ;
25
+ res[k][col] = ' *' ;
26
+ }
27
+ else
28
+ {
29
+ if (idx != n)
30
+ {
31
+ res[idx][col] = ' #' ;
32
+ --idx;
33
+ }
34
+ else
35
+ res[k][col] = ' #' ;
36
+ }
37
+ --j;
38
+ --k;
39
+ }
40
+ }
41
+
42
+ return res;
43
+ }
Original file line number Diff line number Diff line change
1
+ vector<int > findKDistantIndices (vector<int >& nums, int key, int k) {
2
+ int len = nums.size ();
3
+ vector<int > pos;
4
+ int ct = 0 ;
5
+ for (int i = 0 ; i < len; ++i)
6
+ {
7
+ if (nums[i] == key)
8
+ {
9
+ pos.push_back (i);
10
+ ++ct;
11
+ }
12
+ }
13
+
14
+ vector<int > res;
15
+ int lh = -1 , rh = -1 ;
16
+ for (int i = 0 ; i < ct; ++i)
17
+ {
18
+ int l = pos[i]-k > 0 ? pos[i]-k : 0 ;
19
+ int r = pos[i]+k < len ? pos[i]+k : len-1 ;
20
+ lh = l >= rh+1 ? l : rh+1 ;
21
+ rh = r;
22
+ for (int j = lh; j <= rh; ++j)
23
+ res.push_back (j);
24
+
25
+ if (rh == len-1 )
26
+ break ;
27
+ }
28
+
29
+ return res;
30
+ }
Original file line number Diff line number Diff line change
1
+ bool divideArray (vector<int >& nums) {
2
+ int arr[501 ] = {0 };
3
+ int len = nums.size ();
4
+ for (int i = 0 ; i < len; ++i)
5
+ {
6
+ arr[nums[i]]++;
7
+ }
8
+
9
+ for (int i = 0 ; i < 501 ; ++i)
10
+ {
11
+ if (arr[i]%2 == 1 )
12
+ return false ;
13
+ }
14
+
15
+ return true ;
16
+ }
Original file line number Diff line number Diff line change
1
+ int countHillValley (vector<int >& nums) {
2
+ int len = nums.size ();
3
+ int i = 1 ;
4
+ while (i < len-1 && nums[i] == nums[i-1 ])
5
+ ++i;
6
+ if (i == len-1 )
7
+ return 0 ;
8
+
9
+ int prepos = i-1 ;
10
+ int ct = 0 ;
11
+ while (i < len)
12
+ {
13
+ int j = i;
14
+ ++i;
15
+ while (i < len && nums[i] == nums[i-1 ])
16
+ ++i;
17
+ if (i == len)
18
+ break ;
19
+ if ((nums[j] < nums[prepos] && nums[j] < nums[i])
20
+ || (nums[j] > nums[prepos] && nums[j] > nums[i]))
21
+ ++ct;
22
+ prepos = j;
23
+ }
24
+
25
+ return ct;
26
+ }
You can’t perform that action at this time.
0 commit comments