Skip to content

Commit 2e109d8

Browse files
committed
10 problems solved.
1 parent 8ddbe46 commit 2e109d8

File tree

10 files changed

+435
-0
lines changed

10 files changed

+435
-0
lines changed

src/143.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
void reorderList(ListNode* head) {
2+
if (head == nullptr || head->next == nullptr || head->next->next == nullptr)
3+
return;
4+
5+
ListNode* h = head;
6+
int i = 0;
7+
while (h != nullptr)
8+
{
9+
h = h->next;
10+
++i;
11+
}
12+
int n = i%2 == 0 ? i/2 : i/2+1;
13+
h = head;
14+
ListNode* fronthalf = h;
15+
ListNode* node = h;
16+
while (n-- > 1)
17+
node = node->next;
18+
ListNode* s = node->next;
19+
node->next = nullptr;
20+
21+
node = s;
22+
ListNode* backhalf = node;
23+
node = node->next;
24+
backhalf->next = nullptr;
25+
s = nullptr;
26+
while (node != nullptr)
27+
{
28+
s = node->next;
29+
node->next = backhalf;
30+
backhalf = node;
31+
node = s;
32+
}
33+
head = fronthalf;
34+
node = fronthalf;
35+
s = backhalf;
36+
while (node != nullptr && s != nullptr)
37+
{
38+
ListNode* tmp = node->next;
39+
node->next = s;
40+
node = tmp;
41+
ListNode* tmp2 = s->next;
42+
s->next = tmp;
43+
s = tmp2;
44+
}
45+
}

src/144.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
vector<int> preorderTraversal(TreeNode* root) {
2+
vector<int> res;
3+
if (root == nullptr)
4+
return res;
5+
deque<TreeNode*> dq;
6+
dq.push_back(root);
7+
8+
while (!dq.empty())
9+
{
10+
TreeNode* node = dq.back();
11+
dq.pop_back();
12+
res.push_back(node->val);
13+
if (node->right != nullptr)
14+
dq.push_back(node->right);
15+
if (node->left != nullptr)
16+
dq.push_back(node->left);
17+
}
18+
19+
return res;
20+
}

src/477.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
int totalHammingDistance(vector<int>& nums) {
2+
int len = nums.size();
3+
if (len < 2)
4+
return 0;
5+
6+
vector<bitset<32>> bs(len, bitset<32>(0));
7+
for (int i = 0; i < len; ++i)
8+
bs[i] = nums[i];
9+
10+
int ct = 0;
11+
int flag[32][2] = {0};
12+
for (int i = 0; i < len; ++i)
13+
{
14+
for (int j = 0; j < 32; ++j)
15+
{
16+
flag[j][bs[i][j]]++;
17+
}
18+
}
19+
20+
for (int i = 0; i < 32; ++i)
21+
ct += flag[i][0] * flag[i][1];
22+
return ct;
23+
}

src/503.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
vector<int> nextGreaterElements(vector<int>& nums) {
2+
vector<int> res;
3+
int sz = nums.size();
4+
5+
for (int i = 0; i < sz; ++i)
6+
{
7+
if (!findNextGreater(nums, res, nums[i], (i+1)%sz, (i+sz-1)%sz, sz))
8+
res.push_back(-1);
9+
}
10+
11+
return res;
12+
}
13+
14+
bool findNextGreater(const vector<int>& nums, vector<int>& res, int val, int p, int q, int sz)
15+
{
16+
if (p == q)
17+
{
18+
if (nums[p] > val)
19+
{
20+
res.push_back(nums[p]);
21+
return true;
22+
}
23+
else
24+
return false;
25+
}
26+
else if (p == q - 1)
27+
{
28+
if (nums[p] > val)
29+
{
30+
res.push_back(nums[p]);
31+
return true;
32+
}
33+
if (nums[q] > val)
34+
{
35+
res.push_back(nums[q]);
36+
return true;
37+
}
38+
return false;
39+
}
40+
else if (p < q)
41+
{
42+
int mid = (p + q)/2;
43+
if (findNextGreater(nums, res, val, p, mid-1, sz))
44+
return true;
45+
if (nums[mid] > val)
46+
{
47+
res.push_back(nums[mid]);
48+
return true;
49+
}
50+
return findNextGreater(nums, res, val, mid+1, q, sz);
51+
}
52+
else
53+
{
54+
return findNextGreater(nums, res, val, p, sz - 1, sz) || findNextGreater(nums, res, val, 0, q, sz);
55+
}
56+
57+
}

src/547.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
int findCircleNum(vector<vector<int>>& M) {
2+
int num = M.size();
3+
int count = 0;
4+
for (int i = 0; i < num; ++i)
5+
{
6+
if (M[i][i] == 1)
7+
{
8+
M[i][i] = -1;
9+
for (int j = 0; j < num; ++j)
10+
{
11+
if (j != i && M[i][j] == 1)
12+
{
13+
M[i][j] = M[j][i] = -1;
14+
doSubFind(M, j, num);
15+
}
16+
}
17+
count++;
18+
}
19+
}
20+
21+
return count;
22+
}
23+
24+
void doSubFind(vector<vector<int>>& M, int row, const int num)
25+
{
26+
if (M[row][row] == 1)
27+
{
28+
M[row][row] = -1;
29+
for (int i = 0; i < num; ++i)
30+
{
31+
if (i != row && M[row][i] == 1)
32+
{
33+
M[row][i] = M[i][row] = -1;
34+
doSubFind(M, i, num);
35+
}
36+
}
37+
}
38+
}

src/648.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
string replaceWords(vector<string>& dict, string sentence) {
2+
auto cmp = [](string& a, string& b) {
3+
return a.size() < b.size();
4+
};
5+
sort(dict.begin(), dict.end(), cmp);
6+
7+
string res;
8+
int index = 0;
9+
int len = sentence.size();
10+
int num = dict.size();
11+
while (index < len)
12+
{
13+
string replacestr;
14+
int pos = sentence.find(' ', index);
15+
if (pos == string::npos)
16+
{
17+
replacestr = sentence.substr(index);
18+
findRoot(dict, num, replacestr);
19+
res += " " + replacestr;
20+
break;
21+
}
22+
else
23+
{
24+
replacestr = sentence.substr(index, pos - index);
25+
findRoot(dict, num, replacestr);
26+
res += " " + replacestr;
27+
index = pos + 1;
28+
}
29+
}
30+
31+
return res.substr(1);
32+
}
33+
34+
void findRoot(const vector<string>& dict, int num, string& replacestr)
35+
{
36+
int len = replacestr.size();
37+
for (int i = 0; i < num; ++i)
38+
{
39+
int sz = dict[i].size();
40+
if (sz < len && dict[i] == replacestr.substr(0, sz))
41+
{
42+
replacestr = dict[i];
43+
break;
44+
}
45+
}
46+
}

src/676.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public:
2+
/** Initialize your data structure here. */
3+
MagicDictionary() {
4+
if (!mdict.empty())
5+
mdict.clear();
6+
}
7+
8+
/** Build a dictionary through a list of words */
9+
void buildDict(vector<string> dict) {
10+
int len = dict.size();
11+
for (int i = 0; i < len; ++i)
12+
{
13+
int sz = dict[i].size();
14+
if (mdict.find(sz) == mdict.end())
15+
mdict.insert(pair<int, list<string>>(sz, list<string>(1, dict[i])));
16+
else
17+
mdict[sz].push_back(dict[i]);
18+
}
19+
}
20+
21+
/** Returns if there is any word in the trie that equals to the given word after modifying exactly one character */
22+
bool search(string word) {
23+
int len = word.size();
24+
if (mdict.find(len) == mdict.end())
25+
return false;
26+
else
27+
{
28+
for (auto iter = mdict[len].begin(); iter != mdict[len].end(); ++iter)
29+
if (check(word, *iter, len))
30+
return true;
31+
return false;
32+
}
33+
}
34+
35+
private:
36+
map<int, list<string>> mdict;
37+
bool check(const string& a, const string& b, int len)
38+
{
39+
int ct = 0;
40+
for (int i = 0; i < len; ++i)
41+
if (a[i] != b[i])
42+
{
43+
++ct;
44+
if (ct == 2)
45+
return false;
46+
}
47+
return ct == 1;
48+
}

src/725.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
vector<ListNode*> splitListToParts(ListNode* root, int k) {
2+
vector<ListNode*> res;
3+
if (root == nullptr)
4+
{
5+
res.insert(res.end(), k, nullptr);
6+
return res;
7+
}
8+
9+
int len = 0;
10+
ListNode* h = root;
11+
while (h != nullptr)
12+
{
13+
++len;
14+
h = h->next;
15+
}
16+
int each = len/k;
17+
int remain = len%k;
18+
int i = 0;
19+
20+
h = root;
21+
while (i < k)
22+
{
23+
if (i < remain)
24+
{
25+
ListNode* node = h;
26+
ListNode* pre = h;
27+
for (int j = 0; j < each+1; ++j)
28+
{
29+
pre = node;
30+
node = node->next;
31+
}
32+
pre->next = nullptr;
33+
res.push_back(h);
34+
h = node;
35+
}
36+
else
37+
{
38+
if (h == nullptr)
39+
{
40+
res.push_back(nullptr);
41+
}
42+
else
43+
{
44+
ListNode* node = h;
45+
ListNode* pre = h;
46+
for (int j = 0; j < each; ++j)
47+
{
48+
pre = node;
49+
node = node->next;
50+
}
51+
pre->next = nullptr;
52+
res.push_back(h);
53+
h = node;
54+
}
55+
}
56+
++i;
57+
}
58+
return res;
59+
}

0 commit comments

Comments
 (0)