File tree Expand file tree Collapse file tree 10 files changed +263
-0
lines changed Expand file tree Collapse file tree 10 files changed +263
-0
lines changed Original file line number Diff line number Diff line change
1
+ string longestCommonPrefix (vector<string>& strs) {
2
+ string prefix;
3
+ int sz = strs.size ();
4
+ if (sz == 0 )
5
+ return prefix;
6
+ else if (sz == 1 )
7
+ return strs[0 ];
8
+ else
9
+ {
10
+ int minlen = strs[0 ].size ();
11
+ for (int i = 1 ; i < sz; ++i)
12
+ {
13
+ int len = strs[i].size ();
14
+ if (len < minlen)
15
+ minlen = len;
16
+ }
17
+
18
+ int i, j = 0 ;
19
+ while (j < minlen)
20
+ {
21
+ char c = strs[0 ][j];
22
+ for (i = 1 ; i < sz; ++i)
23
+ {
24
+ if (strs[i][j] != c)
25
+ break ;
26
+ }
27
+ if (i != sz)
28
+ break ;
29
+ else
30
+ {
31
+ prefix.push_back (c);
32
+ ++j;
33
+ }
34
+ }
35
+
36
+ return prefix;
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ ListNode* removeElements (ListNode* head, int val) {
2
+ ListNode* p = head, *q;
3
+ while (p != NULL && p->val == val)
4
+ {
5
+ q = p->next ;
6
+ delete p;
7
+ p = q;
8
+ }
9
+ if (p == NULL )
10
+ return NULL ;
11
+ head = p;
12
+ q = p->next ;
13
+ while (q != NULL )
14
+ {
15
+ while (q != NULL && q->val == val)
16
+ {
17
+ ListNode* k = q->next ;
18
+ delete q;
19
+ q = k;
20
+ }
21
+ if (q != NULL )
22
+ {
23
+ p->next = q;
24
+ p = q;
25
+ q = q->next ;
26
+ }
27
+ else
28
+ p->next = q;
29
+ }
30
+
31
+ return head;
32
+ }
Original file line number Diff line number Diff line change
1
+ bool containsNearbyDuplicate (vector<int >& nums, int k) {
2
+ int sz = nums.size ();
3
+ vector<Elem> vec;
4
+ for (int i = 0 ; i != sz; ++i)
5
+ vec.push_back (Elem (nums[i], i));
6
+ sort (vec.begin (), vec.end (), cmp);
7
+
8
+ int i = 0 , j = 0 ;
9
+ while (i < sz)
10
+ {
11
+ int val = vec[i].val ;
12
+ j = i + 1 ;
13
+ while (j < sz && vec[j].val == val)
14
+ {
15
+ if (vec[j].pos - vec[i].pos <= k)
16
+ return true ;
17
+ else
18
+ {
19
+ i = j;
20
+ ++j;
21
+ }
22
+ }
23
+ if (j == sz)
24
+ return false ;
25
+ else
26
+ i = j;
27
+ }
28
+ return false ;
29
+ }
30
+ struct Elem {
31
+ int val;
32
+ int pos;
33
+ Elem (int v, int p) : val(v), pos(p){}
34
+ };
35
+ static bool cmp (Elem a, Elem b)
36
+ {
37
+ return a.val < b.val || a.pos < b.pos ;
38
+ }
Original file line number Diff line number Diff line change
1
+ bool isPalindrome (ListNode* head) {
2
+ ListNode* p = head;
3
+ int num = 0 ;
4
+ while (p != NULL )
5
+ {
6
+ p = p->next ;
7
+ num++;
8
+ }
9
+ int lim = num / 2 ;
10
+ int n = 0 ;
11
+ if (lim > 0 )
12
+ {
13
+ ListNode* h = head;
14
+ ListNode* q = h->next ;
15
+ h->next = NULL ;
16
+ n++;
17
+ while (n < lim)
18
+ {
19
+ p = q->next ;
20
+ q->next = h;
21
+ h = q;
22
+ q = p;
23
+ n++;
24
+ }
25
+ if (num % 2 == 1 )
26
+ q = q->next ;
27
+ while (h != NULL && q != NULL )
28
+ {
29
+ if (h->val != q->val )
30
+ return false ;
31
+ h = h->next ;
32
+ q = q->next ;
33
+ }
34
+ }
35
+ return true ;
36
+ }
Original file line number Diff line number Diff line change
1
+ bool wordPattern (string pattern, string str) {
2
+ vector<string> bijection (26 , " " );
3
+ set<string> stjection;
4
+ int sz = pattern.size ();
5
+
6
+ if (sz == 0 )
7
+ return false ;
8
+ char c;
9
+ int index = 0 , pos = 0 ;
10
+ for (int i = 0 ; i != sz; ++i)
11
+ {
12
+ c = pattern[i];
13
+ if (i < sz - 1 )
14
+ pos = str.find (' ' , index);
15
+ else
16
+ pos = sz;
17
+ if (pos == -1 )
18
+ return false ;
19
+ else
20
+ {
21
+ string s = str.substr (index, pos - index);
22
+ string bi = bijection[c - ' a' ];
23
+ if (bi != " " )
24
+ {
25
+ if (bi != s)
26
+ return false ;
27
+ }
28
+ else
29
+ {
30
+ if (stjection.find (s) != stjection.end ())
31
+ return false ;
32
+ bijection[c - ' a' ] = s;
33
+ stjection.insert (s);
34
+ }
35
+ }
36
+ index = pos + 1 ;
37
+ }
38
+
39
+ return true ;
40
+ }
Original file line number Diff line number Diff line change
1
+ public:
2
+ NumArray (vector<int > nums) {
3
+ int len = nums.size ();
4
+ arry.assign (len, 0 );
5
+ for (int i = 0 ; i < len; ++i)
6
+ arry[i] = nums[i];
7
+ }
8
+
9
+ int sumRange (int i, int j) {
10
+ int sum = arry[i];
11
+ for (int m = i + 1 ; m <= j; ++m)
12
+ sum += arry[m];
13
+ return sum;
14
+ }
15
+ private:
16
+ vector<int > arry;
Original file line number Diff line number Diff line change
1
+ bool checkPerfectNumber (int num) {
2
+ if (num <= 1 )
3
+ return false ;
4
+ int lim = sqrt (num);
5
+ int sum = 1 ;
6
+ for (int i = 2 ; i <= lim; ++i)
7
+ if (num % i == 0 )
8
+ sum += i + num/i;
9
+
10
+ return sum == num;
11
+ }
Original file line number Diff line number Diff line change
1
+ int lengthOfLastWord (string s) {
2
+ int sz = s.size ();
3
+ int len = 0 ;
4
+ int p = 0 , q = 0 ;
5
+ while (p < sz)
6
+ {
7
+ p = s.find_first_not_of (' ' , q);
8
+ if (p == string::npos)
9
+ break ;
10
+ q = s.find (' ' , p);
11
+ if (q == string::npos)
12
+ break ;
13
+ len = q - p;
14
+ }
15
+ if (q == string::npos)
16
+ len = sz - p;
17
+ return len;
18
+ }
Original file line number Diff line number Diff line change
1
+ bool judgeSquareSum (int c) {
2
+ int lim = sqrt (c/2 );
3
+ for (int i = 0 ; i <= lim; ++i)
4
+ {
5
+ int remain = c - i*i;
6
+ int tmp = sqrt (remain);
7
+ if (tmp * tmp == remain)
8
+ return true ;
9
+ }
10
+ return false ;
11
+ }
Original file line number Diff line number Diff line change
1
+ void merge (vector<int >& nums1, int m, vector<int >& nums2, int n) {
2
+ int * arr = new int [m + n];
3
+ int i = 0 , j = 0 , k = 0 ;
4
+ while (i < m && j < n)
5
+ {
6
+ if (nums1[i] <= nums2[j])
7
+ {
8
+ arr[k++] = nums1[i];
9
+ i++;
10
+ }
11
+ else
12
+ {
13
+ arr[k++] = nums2[j];
14
+ j++;
15
+ }
16
+ }
17
+ while (i < m)
18
+ arr[k++] = nums1[i++];
19
+ while (j < n)
20
+ arr[k++] = nums2[j++];
21
+ nums1.assign (arr, arr + m + n);
22
+ delete[] arr;
23
+ }
You can’t perform that action at this time.
0 commit comments