Skip to content

Commit 997c62a

Browse files
committed
10 problems solved.
1 parent 5fac3f7 commit 997c62a

File tree

10 files changed

+298
-0
lines changed

10 files changed

+298
-0
lines changed

src/202.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
bool isHappy(int n) {
2+
set<int> st;
3+
int sum = 0;
4+
while (true)
5+
{
6+
sum = 0;
7+
while (n != 0)
8+
{
9+
sum += (n % 10) * (n % 10);
10+
n /= 10;
11+
}
12+
if (sum == 1)
13+
break;
14+
if (st.find(sum) == st.end())
15+
{
16+
st.insert(sum);
17+
n = sum;
18+
}
19+
else
20+
break;
21+
}
22+
return sum == 1;
23+
}

src/21.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
2+
if (l1 == NULL)
3+
return l2;
4+
if (l2 == NULL)
5+
return l1;
6+
ListNode* h = l1->val < l2->val ? l1 : l2;
7+
ListNode* p = h;
8+
ListNode* q = l1, *t = l2;
9+
if (l1->val < l2->val)
10+
q = q->next;
11+
else
12+
t = t->next;
13+
while (q != NULL && t != NULL)
14+
{
15+
if (q->val < t->val)
16+
{
17+
p->next = q;
18+
p = q;
19+
q = q->next;
20+
}
21+
else
22+
{
23+
p->next = t;
24+
p = t;
25+
t = t->next;
26+
}
27+
}
28+
if (q != NULL)
29+
p->next = q;
30+
else
31+
p->next = t;
32+
return h;
33+
}

src/231.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
bool isPowerOfTwo(int n) {
2+
if (n <= 0)
3+
return false;
4+
int limit = sizeof(int) * 8;
5+
int ct = 0;
6+
for (int i = 0; i < limit; ++i)
7+
{
8+
if (((n >> i) & 1) == 1)
9+
{
10+
++ct;
11+
if (ct > 1)
12+
return false;
13+
}
14+
}
15+
return true;
16+
}

src/257.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
vector<string> binaryTreePaths(TreeNode* root) {
2+
vector<string> paths;
3+
if (root == NULL)
4+
return paths;
5+
else
6+
{
7+
string path(num2str(root->val));
8+
if (root->left != NULL || root->right != NULL)
9+
{
10+
if (root->left != NULL)
11+
collectpath(paths, path, root->left);
12+
if (root->right != NULL)
13+
collectpath(paths, path, root->right);
14+
}
15+
else
16+
paths.push_back(path);
17+
return paths;
18+
}
19+
}
20+
21+
void collectpath(vector<string>& paths, const string& path, TreeNode* node)
22+
{
23+
string str(path + "->" + num2str(node->val));
24+
if (node->left != NULL || node->right != NULL)
25+
{
26+
string subpath(str);
27+
if (node->left != NULL)
28+
collectpath(paths, subpath, node->left);
29+
if (node->right != NULL)
30+
collectpath(paths, subpath, node->right);
31+
}
32+
else
33+
paths.push_back(str);
34+
}
35+
36+
string num2str(int num)
37+
{
38+
if (num == 0)
39+
return "0";
40+
41+
string str;
42+
int flag = 0;
43+
if (num < 0)
44+
{
45+
flag = 1;
46+
num = -num;
47+
}
48+
while (num > 0)
49+
{
50+
str.push_back(num % 10 + '0');
51+
num /= 10;
52+
}
53+
if (flag)
54+
str.push_back('-');
55+
reverse(str.begin(), str.end());
56+
57+
return str;
58+
}

src/27.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
int removeElement(vector<int>& nums, int val) {
2+
int sz = nums.size();
3+
int i = 0, j = sz - 1;
4+
int sum = 0;
5+
while (i <= j)
6+
{
7+
while (i <= j && nums[i] != val)
8+
++i;
9+
while (i <= j && nums[j] == val)
10+
{
11+
++sum;
12+
--j;
13+
}
14+
if (i < j)
15+
{
16+
++sum;
17+
int tmp = nums[i];
18+
nums[i] = nums[j];
19+
nums[j] = tmp;
20+
++i;
21+
--j;
22+
}
23+
}
24+
25+
return sz - sum;
26+
}

src/326.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
bool isPowerOfThree(int n) {
2+
if (n <= 0)
3+
return false;
4+
int max3power = pow(3, (int)(log(INT_MAX) / log(3)));
5+
return max3power % n == 0;
6+
}

src/405.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
string toHex(int num) {
2+
if (num == 0)
3+
return "0";
4+
bitset<32> bs(num);
5+
int sum = 0;
6+
bool flag = false;
7+
string res;
8+
for (int i = 1; i <= 32; ++i)
9+
{
10+
sum = sum * 2 + bs[32 - i];
11+
if (i % 4 == 0)
12+
{
13+
if (sum != 0)
14+
{
15+
char c = sum <= 9 ? sum + '0' : sum - 10 + 'a';
16+
res.push_back(c);
17+
flag = true;
18+
}
19+
else
20+
{
21+
if (flag)
22+
res.push_back('0');
23+
}
24+
sum = 0;
25+
}
26+
}
27+
return res;
28+
}

src/415.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
string addStrings(string num1, string num2) {
2+
int len1 = num1.size();
3+
int len2 = num2.size();
4+
int sum = 0, ct = 0;
5+
int i = len1 - 1, j = len2 - 1;
6+
string res;
7+
while (i >= 0 && j >= 0)
8+
{
9+
sum = num1[i] - '0' + num2[j] - '0' + ct;
10+
res.push_back(char(sum % 10 + '0'));
11+
ct = sum / 10;
12+
--i;
13+
--j;
14+
}
15+
if (i >= 0)
16+
{
17+
while (i >= 0)
18+
{
19+
sum = num1[i] - '0' + ct;
20+
res.push_back(char(sum % 10 + '0'));
21+
ct = sum / 10;
22+
--i;
23+
}
24+
if (ct > 0)
25+
{
26+
res.push_back(char(ct + '0'));
27+
ct = 0;
28+
}
29+
}
30+
if (j >= 0)
31+
{
32+
while (j >= 0)
33+
{
34+
sum = num2[j] - '0' + ct;
35+
res.push_back(char(sum % 10 + '0'));
36+
ct = sum / 10;
37+
--j;
38+
}
39+
if (ct > 0)
40+
{
41+
res.push_back(char(ct + '0'));
42+
ct = 0;
43+
}
44+
}
45+
if (ct > 0)
46+
res.push_back(char(ct + '0'));
47+
reverse(res.begin(), res.end());
48+
return res;
49+
}

src/70.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
int climbStairs(int n) {
2+
if (n <= 1)
3+
return 1;
4+
if (n == 2)
5+
return 2;
6+
int* step = new int[n + 1];
7+
step[1] = 1;
8+
step[2] = 2;
9+
int i = 3;
10+
while (i <= n)
11+
{
12+
step[i] = step[i - 1] + step[i - 2];
13+
++i;
14+
}
15+
int res = step[n];
16+
delete step;
17+
return res;
18+
}

src/747.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
int dominantIndex(vector<int>& nums) {
2+
int len = nums.size();
3+
if (len == 0)
4+
return -1;
5+
if (len == 1)
6+
return 0;
7+
int maxnum = 0, submaxnum = 0, pos = 0;
8+
if (nums[0] > nums[1])
9+
{
10+
maxnum = nums[0];
11+
submaxnum = nums[1];
12+
pos = 0;
13+
}
14+
else
15+
{
16+
maxnum = nums[1];
17+
submaxnum = nums[0];
18+
pos = 1;
19+
}
20+
int i = 2;
21+
while (i < len)
22+
{
23+
if (nums[i] > maxnum)
24+
{
25+
submaxnum = maxnum;
26+
maxnum = nums[i];
27+
pos = i;
28+
}
29+
else if (nums[i] > submaxnum)
30+
{
31+
submaxnum = nums[i];
32+
if (submaxnum >= 50)
33+
return -1;
34+
}
35+
++i;
36+
}
37+
if (maxnum >= submaxnum*2)
38+
return pos;
39+
else
40+
return -1;
41+
}

0 commit comments

Comments
 (0)