Skip to content

Commit 744dee8

Browse files
authored
Merge branch 'main' into 1630
2 parents 0578551 + 30a663d commit 744dee8

17 files changed

+7033
-6414
lines changed

.problemSiteData.json

Lines changed: 6778 additions & 6250 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 31 additions & 15 deletions
Large diffs are not rendered by default.

cpp/0026-remove-duplicates-from-sorted-array.c

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
class Solution {
2-
public:
3-
int removeDuplicates(vector<int>& nums) {
4-
int left = 1;
5-
6-
for(int right = 1; right < nums.size(); right++){
7-
if(nums[right] != nums[right - 1]){
8-
nums[left] = nums[right];
9-
left++;
10-
}
1+
int removeDuplicates(int* nums, int numsSize){
2+
int indx = 1;
3+
4+
for(int i = 1; i < numsSize; i++){
5+
if(nums[i] != nums[i-1]){
6+
nums[indx] = nums[i];
7+
indx++;
118
}
12-
13-
return left;
149
}
15-
};
10+
return indx;
11+
}
12+

cpp/0069-sqrt(x).cpp

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

cpp/0069-sqrtx.cpp

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
1+
/*
2+
Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.
3+
You must not use any built-in exponent function or operator.
4+
5+
For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.
6+
7+
Ex. Input: x = 4
8+
Output: 2
9+
Explanation: The square root of 4 is 2, so we return 2.
10+
11+
Time : O(log N)
12+
Space : O(1)
13+
*/
14+
115
class Solution {
216
public:
317
int mySqrt(int x) {
4-
5-
int l = 0;
6-
int r = x;
7-
8-
while(l <= r){
9-
long int m = (l + r) / 2;
10-
if(m * m == x){
11-
return m;
12-
}
13-
else if(m * m > x){
14-
r = m - 1;
15-
}
16-
else{
17-
l = m + 1;
18-
}
18+
if(x == 0 || x == 1)
19+
return x;
20+
21+
long long beg = 0, mid = 0, end = x/2;
22+
while(beg <= end) {
23+
mid = (beg + end)/2;
24+
if(mid * mid < x) {
25+
if((mid + 1) * (mid + 1) > x)
26+
return mid;
27+
beg = mid+1;
28+
} else if(mid * mid > x) {
29+
if( (mid - 1) * (mid - 1) < x)
30+
return mid-1;
31+
end = mid - 1;
32+
} else
33+
return mid;
1934
}
20-
return r;
35+
return mid;
2136
}
22-
};
37+
};

cpp/0448-find-all-numbers-disappeared-in-an-array.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Solution {
88
At the end, which ever places are positive, add them to our answer.
99
1010
Time complexity: O(n)
11-
Space complexity: O(1)
11+
Space complexity: O(n)
1212
*/
1313
vector<int> findDisappearedNumbers(vector<int>& nums) {
1414
vector<int> ans;
@@ -23,4 +23,4 @@ class Solution {
2323

2424
return ans;
2525
}
26-
};
26+
};

cpp/2306-naming-a-company.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//time O(n)
2+
//space O(n)
3+
4+
class Solution {
5+
public:
6+
long long distinctNames(vector<string>& ideas) {
7+
8+
unordered_map<char,unordered_set<string>> dict;
9+
10+
for(const string& idea : ideas){
11+
dict[idea[0]].insert(idea.substr(1));
12+
}
13+
14+
if(dict.size() < 2){
15+
return 0;
16+
}
17+
18+
long long count = 0;
19+
20+
for(char a = 'a'; a <= 'z' ; a++){
21+
22+
if(dict.find(a) == dict.end())
23+
continue;
24+
25+
for(char b = a+1; b <= 'z'; b++){
26+
27+
if(dict.find(b) == dict.end())
28+
continue;
29+
30+
int aKeys = dict[a].size();
31+
int bKeys = dict[b].size();
32+
33+
for(const string& suffix : dict[a]){
34+
if(dict[b].find(suffix) != dict[b].end()){
35+
aKeys--;
36+
bKeys--;
37+
}
38+
}
39+
40+
count += 2 * (aKeys*bKeys);
41+
}
42+
}
43+
44+
return count;
45+
}
46+
};

csharp/0088-merge-sorted-array.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution
2+
{
3+
public void Merge(int[] nums1, int m, int[] nums2, int n)
4+
{
5+
var index1 = m - 1;
6+
var index2 = n - 1;
7+
var targetIndex = m + n - 1;
8+
9+
for (; targetIndex >= 0; targetIndex--)
10+
{
11+
var useNums1 = (index2 < 0) || (index1 >= 0 && nums1[index1] >= nums2[index2]);
12+
13+
if (useNums1)
14+
{
15+
nums1[targetIndex] = nums1[index1];
16+
index1 -= 1;
17+
}
18+
else
19+
{
20+
nums1[targetIndex] = nums2[index2];
21+
index2 -= 1;
22+
}
23+
}
24+
}
25+
}

java/0705-desgin-hashset.java

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

0 commit comments

Comments
 (0)