File tree Expand file tree Collapse file tree 5 files changed +98
-0
lines changed Expand file tree Collapse file tree 5 files changed +98
-0
lines changed Original file line number Diff line number Diff line change
1
+ vector<int > replaceElements (vector<int >& arr) {
2
+ int len = arr.size ();
3
+ if (len == 1 )
4
+ return {-1 };
5
+
6
+ vector<int > res (len, -1 );
7
+ int maxnum = -1 ;
8
+ int i = len-1 ;
9
+ while (i > 0 )
10
+ {
11
+ if (arr[i] > maxnum)
12
+ {
13
+ maxnum = arr[i];
14
+ }
15
+ res[i-1 ] = maxnum;
16
+ --i;
17
+ }
18
+
19
+ return res;
20
+ }
Original file line number Diff line number Diff line change
1
+ vector<int > decompressRLElist (vector<int >& nums) {
2
+ int len = nums.size ();
3
+ int total = 0 ;
4
+ for (int i = 0 ; i < len; i += 2 )
5
+ total += nums[i];
6
+ vector<int > res (total, 0 );
7
+ int k = 0 ;
8
+ for (int i = 0 ; i < len; i += 2 )
9
+ {
10
+ int lim = k+nums[i];
11
+ for (; k < lim; ++k)
12
+ res[k] = nums[i+1 ];
13
+ }
14
+
15
+ return res;
16
+ }
Original file line number Diff line number Diff line change
1
+ int maximum69Number (int num) {
2
+ string s = to_string (num);
3
+ int res = 0 ;
4
+ int len = s.size ();
5
+ for (int i = 0 ; i < len; ++i)
6
+ if (s[i] == ' 6' )
7
+ {
8
+ s[i] = ' 9' ;
9
+ break ;
10
+ }
11
+
12
+ for (int i = 0 ; i < len; ++i)
13
+ res = res*10 + (s[i]-' 0' );
14
+
15
+ return res;
16
+ }
Original file line number Diff line number Diff line change
1
+ int findUnsortedSubarray (vector<int >& nums) {
2
+ int len = nums.size ();
3
+ if (len <= 1 )
4
+ return 0 ;
5
+
6
+ vector<int > vec (nums);
7
+ sort (vec.begin (), vec.end ());
8
+ int i = 0 ;
9
+ for (i = 0 ; i < len; ++i)
10
+ if (nums[i] != vec[i])
11
+ break ;
12
+ if (i == len)
13
+ return 0 ;
14
+ int j = len-1 ;
15
+ for (j = len-1 ; j >= 0 ; --j)
16
+ if (nums[j] != vec[j])
17
+ break ;
18
+ return j-i+1 ;
19
+ }
Original file line number Diff line number Diff line change
1
+ bool isValidBST (TreeNode* root) {
2
+ if (root == NULL )
3
+ return true ;
4
+ list<TreeNode*> lst;
5
+ midtravel (root, lst);
6
+ auto lim = lst.end ();
7
+ --lim;
8
+ for (auto iter = lst.begin (); iter != lim; ++iter)
9
+ {
10
+ auto tmp = iter;
11
+ tmp++;
12
+ if ((*iter)->val >= (*tmp)->val )
13
+ return false ;
14
+ }
15
+
16
+ return true ;
17
+ }
18
+
19
+ void midtravel (TreeNode* node, list<TreeNode*>& lst)
20
+ {
21
+ if (node != NULL )
22
+ {
23
+ midtravel (node->left , lst);
24
+ lst.push_back (node);
25
+ midtravel (node->right , lst);
26
+ }
27
+ }
You can’t perform that action at this time.
0 commit comments