Skip to content

Commit 8ed86ef

Browse files
committed
revising basic cconcepts
1 parent 6d44386 commit 8ed86ef

5 files changed

+149
-19
lines changed

234. Palindrome Linked List.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
11+
ListNode* reverselist(ListNode *head, ListNode *end) {
12+
if(!head || !head->next)
13+
return head;
14+
15+
ListNode *cur = head;
16+
ListNode *next = head;
17+
ListNode *prev = nullptr;
18+
ListNode *last = end->next;
19+
20+
while(cur && cur!=last) {
21+
next = cur->next;
22+
cur->next = prev;
23+
prev = cur;
24+
cur = next;
25+
}
26+
27+
return prev;
28+
}
29+
30+
31+
public:
32+
bool isPalindrome(ListNode* head) {
33+
34+
if(!head || !head->next)
35+
return true;
36+
37+
ListNode* slow = head;
38+
ListNode* fast = head;
39+
ListNode* prev = nullptr;
40+
ListNode* right = nullptr;
41+
42+
while(fast && fast->next) {
43+
prev = slow;
44+
slow = slow->next;
45+
fast = fast->next->next;
46+
}
47+
48+
if(!fast) { //even linked list
49+
right = slow;
50+
slow = prev;
51+
}
52+
else { // odd linked list
53+
right = slow->next;
54+
slow = prev;
55+
}
56+
57+
reverselist(head, slow);
58+
59+
while(slow && right) {
60+
if(slow->val != right->val)
61+
return false;
62+
slow = slow->next;
63+
right = right->next;
64+
}
65+
66+
if(slow || right)
67+
return false;
68+
69+
return true;
70+
}
71+
};

451. Sort Characters By Frequency.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,35 @@ class Solution {
3030

3131
}
3232
};
33+
34+
35+
----------
36+
37+
class Solution {
38+
public:
39+
string frequencySort(string s) {
40+
41+
unordered_map<char, int> m;
42+
string res = "";
43+
44+
for(int i = 0;i<s.length(); ++i) {
45+
m[s[i]]++;
46+
}
47+
48+
vector<pair<char, int> > v;
49+
50+
for(auto i:m) {
51+
v.push_back(make_pair(i.first, i.second));
52+
}
53+
54+
sort(v.begin(), v.end(), [] (const pair<char, int> &i1, const pair<char, int> &i2) {return i1.second > i2.second;});
55+
56+
for(auto i: v) {
57+
while(i.second--)
58+
res += i.first;
59+
}
60+
61+
return res;
62+
63+
}
64+
};

643. Maximum Average Subarray I.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
double findMaxAverage(vector<int>& nums, int k) {
4+
5+
deque<int> window;
6+
double ans = INT_MIN;
7+
double avg = 0.0;
8+
int sum = 0;
9+
for(auto i:nums) {
10+
11+
window.push_back(i);
12+
sum+=i;
13+
14+
if(window.size() > k) {
15+
sum -= window.front();
16+
window.pop_front();
17+
}
18+
19+
if(window.size() == k) {
20+
avg = double(sum)/double(k);
21+
ans = max(avg, ans);
22+
}
23+
}
24+
25+
return ans;
26+
27+
}
28+
};

9. Palindrome Number.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
bool isPalindrome(int x) {
4+
5+
if (x < 0 || (x!=0 && x%10 == 0))
6+
return false;
7+
8+
int rev = 0;
9+
10+
while(x > rev) {
11+
rev = 10*rev + x%10;
12+
x = x/10;
13+
}
14+
15+
return x == rev || x == rev/10;
16+
17+
}
18+
};

Palindrome Number.cpp

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)