Skip to content

Commit 14f23a5

Browse files
committed
add solutions
1 parent 9debd31 commit 14f23a5

File tree

11 files changed

+123
-1
lines changed

11 files changed

+123
-1
lines changed

problems/1356.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits", "name": "Sort Integers by The Number of 1 Bits", "difficulty": "Easy", "statement": "<div><p>Given an integer array <code>arr</code>. You have to sort the integers in the array&nbsp;in ascending order by the number of <strong>1's</strong>&nbsp;in their binary representation and in case of two or more integers have the same number of <strong>1's</strong> you have to sort them in ascending order.</p>\n\n<p>Return <em>the sorted array</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input:</strong> arr = [0,1,2,3,4,5,6,7,8]\n<strong>Output:</strong> [0,1,2,4,8,3,5,6,7]\n<strong>Explantion:</strong> [0] is the only integer with 0 bits.\n[1,2,4,8] all have 1 bit.\n[3,5,6] have 2 bits.\n[7] has 3 bits.\nThe sorted array by bits is [0,1,2,4,8,3,5,6,7]\n</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input:</strong> arr = [1024,512,256,128,64,32,16,8,4,2,1]\n<strong>Output:</strong> [1,2,4,8,16,32,64,128,256,512,1024]\n<strong>Explantion:</strong> All integers have 1 bit in the binary representation, you should just sort them in ascending order.\n</pre>\n\n<p><strong>Example 3:</strong></p>\n\n<pre><strong>Input:</strong> arr = [10000,10000]\n<strong>Output:</strong> [10000,10000]\n</pre>\n\n<p><strong>Example 4:</strong></p>\n\n<pre><strong>Input:</strong> arr = [2,3,5,7,11,13,17,19]\n<strong>Output:</strong> [2,3,5,17,7,11,13,19]\n</pre>\n\n<p><strong>Example 5:</strong></p>\n\n<pre><strong>Input:</strong> arr = [10,100,1000,10000]\n<strong>Output:</strong> [10,100,10000,1000]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= arr.length &lt;= 500</code></li>\n\t<li><code>0 &lt;= arr[i] &lt;= 10^4</code></li>\n</ul>\n</div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nint one_count(int a) {\n if (a == 0) return 0;\n return a % 2 + one_count(a / 2);\n}\n\nbool comp(int a, int b)\n{\n int c = one_count(a);\n int d = one_count(b);\n if (c == d) {\n return a < b;\n }\n return c < d;\n}\n\nclass Solution\n{\npublic:\n vector<int> sortByBits(vector<int> &arr)\n {\n sort(arr.begin(), arr.end());\n sort(arr.begin(), arr.end(), comp);\n return arr;\n }\n};"}

problems/1491.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary", "name": "Average Salary Excluding the Minimum and Maximum Salary", "difficulty": "Easy", "statement": "<div><p>Given an array of <strong>unique</strong> integers <code>salary</code>&nbsp;where <code>salary[i]</code> is the salary of the employee <code>i</code>.</p>\n\n<p>Return the average salary of employees excluding the minimum and maximum salary.</p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input:</strong> salary = [4000,3000,1000,2000]\n<strong>Output:</strong> 2500.00000\n<strong>Explanation: </strong>Minimum salary and maximum salary are 1000 and 4000 respectively.\nAverage salary excluding minimum and maximum salary is (2000+3000)/2= 2500\n</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input:</strong> salary = [1000,2000,3000]\n<strong>Output:</strong> 2000.00000\n<strong>Explanation: </strong>Minimum salary and maximum salary are 1000 and 3000 respectively.\nAverage salary excluding minimum and maximum salary is (2000)/1= 2000\n</pre>\n\n<p><strong>Example 3:</strong></p>\n\n<pre><strong>Input:</strong> salary = [6000,5000,4000,3000,2000,1000]\n<strong>Output:</strong> 3500.00000\n</pre>\n\n<p><strong>Example 4:</strong></p>\n\n<pre><strong>Input:</strong> salary = [8000,9000,2000,3000,6000,1000]\n<strong>Output:</strong> 4750.00000\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>3 &lt;= salary.length &lt;= 100</code></li>\n\t<li><code>10^3&nbsp;&lt;= salary[i] &lt;= 10^6</code></li>\n\t<li><code>salary[i]</code> is unique.</li>\n\t<li>Answers within <code>10^-5</code> of the actual value will be accepted as correct.</li>\n</ul></div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution\n{\npublic:\n double average(vector<int> &salary)\n {\n sort(salary.begin(), salary.end());\n int n = salary.size();\n int sum = 0;\n for (double i : salary)\n sum += i;\n sum -= salary[0];\n sum -= salary[n - 1];\n\n return (double)(sum / (double)(n - 2));\n }\n};"}

problems/344.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/reverse-string", "name": "Reverse String", "difficulty": "Easy", "statement": "<div><p>Write a function that reverses a string. The input string is given as an array of characters <code>char[]</code>.</p>\n\n<p>Do not allocate extra space for another array, you must do this by <strong>modifying the input array&nbsp;<a href=\"https://en.wikipedia.org/wiki/In-place_algorithm\" target=\"_blank\">in-place</a></strong> with O(1) extra memory.</p>\n\n<p>You may assume all the characters consist of <a href=\"https://en.wikipedia.org/wiki/ASCII#Printable_characters\" target=\"_blank\">printable ascii characters</a>.</p>\n\n<p>&nbsp;</p>\n\n<div>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-1-1\">[\"h\",\"e\",\"l\",\"l\",\"o\"]</span>\n<strong>Output: </strong><span id=\"example-output-1\">[\"o\",\"l\",\"l\",\"e\",\"h\"]</span>\n</pre>\n\n<div>\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-2-1\">[\"H\",\"a\",\"n\",\"n\",\"a\",\"h\"]</span>\n<strong>Output: </strong><span id=\"example-output-2\">[\"h\",\"a\",\"n\",\"n\",\"a\",\"H\"]</span>\n</pre>\n</div>\n</div>\n</div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution\n{\npublic:\n void reverseString(vector<char> &s)\n {\n int n = s.size();\n for(int i = 0; i < n / 2; i++) {\n int temp = s[i];\n s[i] = s[n - i - 1];\n s[n - i - 1] = temp;\n }\n }\n};"}

problems/559.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/maximum-depth-of-n-ary-tree", "name": "Maximum Depth of N-ary Tree", "difficulty": "Easy", "statement": "<div><p>Given a n-ary tree, find its maximum depth.</p>\n\n<p>The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.</p>\n\n<p><em>Nary-Tree input serialization&nbsp;is represented in their level order traversal, each group of children is separated by the null value (See examples).</em></p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n\n<p><img src=\"https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png\" style=\"width: 100%; max-width: 300px;\"></p>\n\n<pre><strong>Input:</strong> root = [1,null,3,2,4,null,5,6]\n<strong>Output:</strong> 3\n</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png\" style=\"width: 296px; height: 241px;\"></p>\n\n<pre><strong>Input:</strong> root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]\n<strong>Output:</strong> 5\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>The depth of the n-ary tree is less than or equal to <code>1000</code>.</li>\n\t<li>The total number of nodes is between <code>[0,&nbsp;10^4]</code>.</li>\n</ul>\n</div>", "language": "c", "solution": "\nstruct Node\n{\n int val;\n int numChildren;\n struct Node **children;\n};\n\nint *maxDepth(struct Node *root)\n{\n return calc_depth(root, 0);\n}\n\nint calc_depth(struct Node *root, int depth)\n{\n if (!root)\n return depth;\n int old_depth = depth;\n depth += 1;\n for (int i = 0; i < root->numChildren; i++)\n {\n int newd = calc_depth(root->children[i], old_depth + 1);\n printf(\"%d\\n\", newd);\n depth = depth > newd ? depth : newd;\n }\n return depth;\n}"}

problems/876.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/middle-of-the-linked-list", "name": "Middle of the Linked List", "difficulty": "Easy", "statement": "<div><p>Given a non-empty, singly&nbsp;linked list with head node <code>head</code>, return&nbsp;a&nbsp;middle node of linked list.</p>\n\n<p>If there are two middle nodes, return the second middle node.</p>\n\n<p>&nbsp;</p>\n\n<div>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-1-1\">[1,2,3,4,5]</span>\n<strong>Output: </strong>Node 3 from this list (Serialization: <span id=\"example-output-1\">[3,4,5]</span>)\nThe returned node has value 3. (The judge's serialization of this node is [3,4,5]).\nNote that we returned a ListNode object ans, such that:\nans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.\n</pre>\n\n<div>\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-2-1\">[1,2,3,4,5,6]</span>\n<strong>Output: </strong>Node 4 from this list (Serialization: <span id=\"example-output-2\">[4,5,6]</span>)\nSince the list has two middle nodes with values 3 and 4, we return the second one.\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>Note:</strong></p>\n\n<ul>\n\t<li>The number of nodes in the given list will be between <code>1</code>&nbsp;and <code>100</code>.</li>\n</ul>\n</div>\n</div>\n</div>", "language": "C", "solution": "\nstruct ListNode\n{\n int val;\n struct ListNode *next;\n};\n\nstruct ListNode *middleNode(struct ListNode *head)\n{\n int n = 0;\n struct ListNode* curr = head;\n\n while(curr) {\n curr = curr->next;\n ++n;\n }\n curr = head;\n for(int i = 0; i < n/2; ++i)\n curr = curr->next;\n return curr;\n}"}

solutions/easy/1356.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
int one_count(int a) {
7+
if (a == 0) return 0;
8+
return a % 2 + one_count(a / 2);
9+
}
10+
11+
bool comp(int a, int b)
12+
{
13+
int c = one_count(a);
14+
int d = one_count(b);
15+
if (c == d) {
16+
return a < b;
17+
}
18+
return c < d;
19+
}
20+
21+
class Solution
22+
{
23+
public:
24+
vector<int> sortByBits(vector<int> &arr)
25+
{
26+
sort(arr.begin(), arr.end());
27+
sort(arr.begin(), arr.end(), comp);
28+
return arr;
29+
}
30+
};

solutions/easy/1491.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
class Solution
7+
{
8+
public:
9+
double average(vector<int> &salary)
10+
{
11+
sort(salary.begin(), salary.end());
12+
int n = salary.size();
13+
int sum = 0;
14+
for (double i : salary)
15+
sum += i;
16+
sum -= salary[0];
17+
sum -= salary[n - 1];
18+
19+
return (double)(sum / (double)(n - 2));
20+
}
21+
};

solutions/easy/344.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
class Solution
7+
{
8+
public:
9+
void reverseString(vector<char> &s)
10+
{
11+
int n = s.size();
12+
for(int i = 0; i < n / 2; i++) {
13+
int temp = s[i];
14+
s[i] = s[n - i - 1];
15+
s[n - i - 1] = temp;
16+
}
17+
}
18+
};

solutions/easy/559.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
struct Node
3+
{
4+
int val;
5+
int numChildren;
6+
struct Node **children;
7+
};
8+
9+
int *maxDepth(struct Node *root)
10+
{
11+
return calc_depth(root, 0);
12+
}
13+
14+
int calc_depth(struct Node *root, int depth)
15+
{
16+
if (!root)
17+
return depth;
18+
int old_depth = depth;
19+
depth += 1;
20+
for (int i = 0; i < root->numChildren; i++)
21+
{
22+
int newd = calc_depth(root->children[i], old_depth + 1);
23+
printf("%d\n", newd);
24+
depth = depth > newd ? depth : newd;
25+
}
26+
return depth;
27+
}

solutions/easy/876.C

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
struct ListNode
3+
{
4+
int val;
5+
struct ListNode *next;
6+
};
7+
8+
struct ListNode *middleNode(struct ListNode *head)
9+
{
10+
int n = 0;
11+
struct ListNode* curr = head;
12+
13+
while(curr) {
14+
curr = curr->next;
15+
++n;
16+
}
17+
curr = head;
18+
for(int i = 0; i < n/2; ++i)
19+
curr = curr->next;
20+
return curr;
21+
}

src/solutions.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)