File tree Expand file tree Collapse file tree 5 files changed +187
-0
lines changed Expand file tree Collapse file tree 5 files changed +187
-0
lines changed Original file line number Diff line number Diff line change
1
+ int countTriples (int n) {
2
+ int ct = 0 ;
3
+ vector<int > vec (n, 0 );
4
+ for (int i = 1 ; i <= n; ++i)
5
+ vec[i-1 ] = i*i;
6
+
7
+ int lim = int (n/sqrt (2 ))+1 ;
8
+ for (int i = 0 ; i < lim; ++i)
9
+ {
10
+ for (int j = i; j < n; ++j)
11
+ {
12
+ for (int k = j+1 ; k < n; ++k)
13
+ {
14
+ if (vec[k] == vec[i]+vec[j])
15
+ {
16
+ if (i != j)
17
+ ct += 2 ;
18
+ else
19
+ ++ct;
20
+ }
21
+ }
22
+ }
23
+ }
24
+
25
+ return ct;
26
+ }
Original file line number Diff line number Diff line change
1
+ bool checkAlmostEquivalent (string word1, string word2) {
2
+ int len = word1.size ();
3
+ vector<int > vec1 (26 , 0 );
4
+ vector<int > vec2 (26 , 0 );
5
+
6
+ for (int i = 0 ; i < len; ++i)
7
+ {
8
+ vec1[word1[i]-' a' ]++;
9
+ vec2[word2[i]-' a' ]++;
10
+ }
11
+
12
+ for (int i = 0 ; i < 26 ; ++i)
13
+ {
14
+ if (abs (vec1[i]-vec2[i]) > 3 )
15
+ return false ;
16
+ }
17
+
18
+ return true ;
19
+ }
Original file line number Diff line number Diff line change
1
+ ListNode* reverseEvenLengthGroups (ListNode* head) {
2
+ if (head == nullptr || head->next == nullptr || head->next ->next == nullptr )
3
+ return head;
4
+
5
+ int ct = 0 ;
6
+ ListNode* p = head;
7
+ while (p != nullptr )
8
+ {
9
+ ++ct;
10
+ p = p->next ;
11
+ }
12
+
13
+ int lim = 2 *ct;
14
+ int n = sqrt (lim);
15
+ int tmp = n*(n+1 );
16
+ int r = 0 ;
17
+ if (tmp == lim)
18
+ ;
19
+ else if (tmp < lim)
20
+ r = ct-tmp/2 ;
21
+ else
22
+ {
23
+ r = ct-n*(n-1 )/2 ;
24
+ --n;
25
+ }
26
+
27
+ ListNode* pre = head;
28
+ ListNode* post = pre->next ;
29
+ ListNode* cur = post->next ;
30
+ int i = 2 ;
31
+ bool flag = true ;
32
+ while (i <= n)
33
+ {
34
+ if (flag)
35
+ {
36
+ p = post;
37
+ for (int j = 1 ; j < i; ++j)
38
+ {
39
+ pre->next = cur;
40
+ ListNode* tmp = cur->next ;
41
+ cur->next = p;
42
+ p = cur;
43
+ cur = tmp;
44
+ }
45
+ post->next = cur;
46
+ }
47
+ else
48
+ {
49
+ for (int j = 0 ; j < i; ++j)
50
+ {
51
+ pre = cur;
52
+ cur = cur->next ;
53
+ }
54
+ post = cur;
55
+ if (cur != nullptr )
56
+ cur = cur->next ;
57
+ }
58
+ flag = !flag;
59
+ ++i;
60
+ }
61
+
62
+ if (r > 0 )
63
+ {
64
+ if (!flag)
65
+ {
66
+ pre = post;
67
+ post = post->next ;
68
+ if (cur != nullptr )
69
+ cur = cur->next ;
70
+ }
71
+ if (r%2 == 0 )
72
+ {
73
+ p = post;
74
+ for (int j = 1 ; j < r; ++j)
75
+ {
76
+ pre->next = cur;
77
+ ListNode* tmp = cur->next ;
78
+ cur->next = p;
79
+ p = cur;
80
+ cur = tmp;
81
+ }
82
+ post->next = cur;
83
+ }
84
+ }
85
+
86
+ return head;
87
+ }
Original file line number Diff line number Diff line change
1
+ vector<string> summaryRanges (vector<int >& nums) {
2
+ int len = nums.size ();
3
+ if (len == 0 )
4
+ return {};
5
+ vector<string> res;
6
+
7
+ int i = 0 , j = 0 ;
8
+ while (j < len-1 )
9
+ {
10
+ if (nums[j] == nums[j+1 ]-1 )
11
+ ++j;
12
+ else
13
+ {
14
+ if (i == j)
15
+ res.push_back (to_string (nums[i]));
16
+ else
17
+ res.push_back (to_string (nums[i]) + " ->" + to_string (nums[j]));
18
+ i = j+1 ;
19
+ j = i;
20
+ }
21
+ }
22
+
23
+ if (i == j)
24
+ res.push_back (to_string (nums[i]));
25
+ else
26
+ res.push_back (to_string (nums[i]) + " ->" + to_string (nums[j]));
27
+
28
+ return res;
29
+ }
Original file line number Diff line number Diff line change
1
+ vector<int > countBits (int num) {
2
+ int binarr[32 ] = {0 };
3
+ int ct = 0 ;
4
+ vector<int > res (num+1 , 0 );
5
+
6
+ for (int i = 0 ; i < num; ++i)
7
+ {
8
+ for (int j = 0 ; j < 32 ; ++j)
9
+ {
10
+ if (binarr[j] == 0 )
11
+ {
12
+ ++ct;
13
+ res[i+1 ] = ct;
14
+ binarr[j] = 1 ;
15
+ break ;
16
+ }
17
+ else
18
+ {
19
+ binarr[j] = 0 ;
20
+ --ct;
21
+ }
22
+ }
23
+ }
24
+
25
+ return res;
26
+ }
You can’t perform that action at this time.
0 commit comments