-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* @lc app=leetcode id=537 lang=cpp | ||
* | ||
* [537] Complex Number Multiplication | ||
*/ | ||
|
||
// @lc code=start | ||
|
||
/** | ||
* Accepted | ||
55/55 cases passed (0 ms) | ||
Your runtime beats 100 % of cpp submissions | ||
Your memory usage beats 100 % of cpp submissions (5.9 MB) | ||
*/ | ||
class Solution { | ||
public: | ||
string complexNumberMultiply(string a, string b) { | ||
int re_a, im_a, re_b, im_b; | ||
stringToComplex(a, re_a, im_a); | ||
stringToComplex(b, re_b, im_b); | ||
|
||
int re_c = re_a * re_b - im_a * im_b; | ||
int im_c = re_a * im_b + im_a * re_b; | ||
return complexToString(re_c, im_c); | ||
} | ||
|
||
string complexToString(int re, int im) { | ||
string result = to_string(re); | ||
result += '+'; | ||
result += to_string(im); | ||
result += 'i'; | ||
|
||
return result; | ||
} | ||
|
||
void stringToComplex(string z, int& re, int& im) { | ||
int pos = z.find('+'); | ||
re = stoi(z.substr(0, pos)); | ||
im = stoi(z.substr(pos+1)); | ||
} | ||
}; | ||
// @lc code=end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* @lc app=leetcode id=593 lang=cpp | ||
* | ||
* [593] Valid Square | ||
*/ | ||
|
||
// @lc code=start | ||
|
||
/** | ||
* Accepted | ||
244/244 cases passed (0 ms) | ||
Your runtime beats 100 % of cpp submissions | ||
Your memory usage beats 100 % of cpp submissions (6.3 MB) | ||
*/ | ||
class Solution { | ||
public: | ||
bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) { | ||
vector<int> es {}; //edge square | ||
es.push_back(getEdgeSquare(p1, p2)); | ||
es.push_back(getEdgeSquare(p1, p3)); | ||
es.push_back(getEdgeSquare(p1, p4)); | ||
es.push_back(getEdgeSquare(p2, p3)); | ||
es.push_back(getEdgeSquare(p2, p4)); | ||
es.push_back(getEdgeSquare(p3, p4)); | ||
|
||
sort(es.begin(), es.end()); | ||
return es[0] != 0 | ||
&& es[0] == es[1] && es[1] == es[2] && es[2] == es[3] | ||
&& 2 * es[3] == es[4] | ||
&& es[4] == es[5]; | ||
} | ||
|
||
int getEdgeSquare(vector<int>& a, vector<int>& b) { | ||
return (a[0] - b[0]) * (a[0] - b[0]) | ||
+ (a[1] - b[1]) * (a[1] - b[1]); | ||
} | ||
}; | ||
// @lc code=end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* @lc app=leetcode id=598 lang=cpp | ||
* | ||
* [598] Range Addition II | ||
*/ | ||
|
||
// @lc code=start | ||
|
||
/** | ||
* Accepted | ||
69/69 cases passed (12 ms) | ||
Your runtime beats 70.4 % of cpp submissions | ||
Your memory usage beats 100 % of cpp submissions (9.4 MB) | ||
*/ | ||
class Solution { | ||
public: | ||
int maxCount(int m, int n, vector<vector<int>>& ops) { | ||
int min0 = m, min1 = n; | ||
for (auto& op : ops) { | ||
if (op[0] < min0) min0 = op[0]; | ||
if (op[1] < min1) min1 = op[1]; | ||
} | ||
return min0 * min1; | ||
} | ||
}; | ||
// @lc code=end | ||
|