File tree Expand file tree Collapse file tree 6 files changed +163
-0
lines changed Expand file tree Collapse file tree 6 files changed +163
-0
lines changed Original file line number Diff line number Diff line change
1
+ string thousandSeparator (int n) {
2
+ string s = to_string (n);
3
+ int len = s.size ();
4
+ int m = len +(len-1 )/3 ;
5
+ string res (m, ' ' );
6
+ int i = 0 , j = m-1 ;
7
+ while (i < len)
8
+ {
9
+ res[j] = s[len-1 -i];
10
+ --j;
11
+ ++i;
12
+ if (j >= 0 && i%3 == 0 )
13
+ res[j--] = ' .' ;
14
+ }
15
+
16
+ return res;
17
+ }
Original file line number Diff line number Diff line change
1
+ string reorderSpaces (string text) {
2
+ int len = text.size ();
3
+ vector<pair<int ,int >> wordpos;
4
+ bool flag = false ;
5
+ int i = 0 ;
6
+ int lh = 0 , rh = 0 ;
7
+ int word = 0 , space = 0 ;
8
+ while (i < len)
9
+ {
10
+ if (text[i] == ' ' )
11
+ {
12
+ if (flag)
13
+ {
14
+ rh = i-1 ;
15
+ flag = false ;
16
+ wordpos.push_back (make_pair (lh, rh));
17
+ }
18
+ ++space;
19
+ }
20
+ else
21
+ {
22
+ if (!flag)
23
+ {
24
+ lh = i;
25
+ ++word;
26
+ flag = true ;
27
+ }
28
+ rh = i;
29
+ }
30
+ ++i;
31
+ }
32
+ if (text[len-1 ] != ' ' )
33
+ wordpos.push_back (make_pair (lh, rh));
34
+
35
+ int n = word > 1 ? space/(word-1 ) : 0 ;
36
+ int remain = space - n*(word-1 );
37
+ int k = 0 ;
38
+
39
+ string res (len, ' ' );
40
+ for (int i = 0 ; i < word-1 ; ++i)
41
+ {
42
+ for (int j = wordpos[i].first ; j <= wordpos[i].second ; ++j)
43
+ res[k++] = text[j];
44
+ k += n;
45
+ }
46
+
47
+ for (int j = wordpos[word-1 ].first ; j <= wordpos[word-1 ].second ; ++j)
48
+ res[k++] = text[j];
49
+
50
+ return res;
51
+ }
Original file line number Diff line number Diff line change
1
+ int largestAltitude (vector<int >& gain) {
2
+ int high = 0 ;
3
+ int highest = 0 ;
4
+ int len = gain.size ();
5
+ for (int i = 0 ; i < len; ++i)
6
+ {
7
+ high += gain[i];
8
+ if (high > highest)
9
+ highest = high;
10
+ }
11
+
12
+ return highest;
13
+ }
Original file line number Diff line number Diff line change
1
+ string maximumTime (string time) {
2
+ if (time[0 ] == ' ?' )
3
+ {
4
+ if (time[1 ] == ' ?' || time[1 ] < ' 4' )
5
+ time[0 ] = ' 2' ;
6
+ else
7
+ time[0 ] = ' 1' ;
8
+ }
9
+ if (time[1 ] == ' ?' )
10
+ {
11
+ if (time[0 ] == ' 0' || time[0 ] == ' 1' )
12
+ time[1 ] = ' 9' ;
13
+ else
14
+ time[1 ] = ' 3' ;
15
+ }
16
+ if (time[3 ] == ' ?' )
17
+ {
18
+ time[3 ] = ' 5' ;
19
+ }
20
+ if (time[4 ] == ' ?' )
21
+ {
22
+ time[4 ] = ' 9' ;
23
+ }
24
+
25
+ return time;
26
+ }
Original file line number Diff line number Diff line change
1
+ int countBalls (int lowLimit, int highLimit) {
2
+ int box[50 ] = {0 };
3
+ int i = lowLimit;
4
+ int sum = 0 ;
5
+ while (i > 0 )
6
+ {
7
+ sum += i%10 ;
8
+ i /= 10 ;
9
+ }
10
+ box[sum]++;
11
+
12
+ int last = lowLimit%10 ;
13
+ i = lowLimit+1 ;
14
+ while (i <= highLimit)
15
+ {
16
+ if (last < 9 )
17
+ {
18
+ sum++;
19
+ box[sum]++;
20
+ ++last;
21
+ }
22
+ else
23
+ {
24
+ sum = 0 ;
25
+ int t = i;
26
+ while (t > 0 )
27
+ {
28
+ sum += t%10 ;
29
+ t /= 10 ;
30
+ }
31
+ box[sum]++;
32
+ last = i%10 ;
33
+ }
34
+ ++i;
35
+ }
36
+
37
+ int maxballs = 0 ;
38
+ for (int j = 0 ; j < 50 ; ++j)
39
+ if (box[j] > maxballs)
40
+ maxballs = box[j];
41
+
42
+ return maxballs;
43
+ }
Original file line number Diff line number Diff line change
1
+ int sumOfUnique (vector<int >& nums) {
2
+ int arr[101 ] = {0 };
3
+ int len = nums.size ();
4
+ for (int i = 0 ; i < len; ++i)
5
+ arr[nums[i]]++;
6
+
7
+ int sum = 0 ;
8
+ for (int i = 1 ; i <= 100 ; ++i)
9
+ if (arr[i] == 1 )
10
+ sum += i;
11
+
12
+ return sum;
13
+ }
You can’t perform that action at this time.
0 commit comments