This file contains hidden or 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
//Runtime: 64 ms, faster than 44.53% | |
//Memory Usage: 13.5 MB, less than 49.72% | |
class Solution { | |
public: | |
int firstUniqChar(string s) { | |
unordered_map<char,int> set; | |
for(int i = 0;i < s.length();++i) |
This file contains hidden or 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
//Runtime: 8 ms, faster than 100.00% | |
//Memory Usage: 9.5 MB, less than 65.93% | |
class Solution { | |
public: | |
int maxProfit(vector<int>& prices) { | |
if(prices.empty()) return 0; | |
int ret = 0; | |
for (size_t p = 1; p < prices.size(); ++p) | |
ret += max(prices[p] - prices[p - 1], 0); |
This file contains hidden or 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
//Runtime: 16 ms, faster than 98.23% | |
//Memory Usage: 12.3 MB, less than 77.06% | |
class Solution { | |
public: | |
bool isValidSudoku(vector<vector<char>>& board) { | |
int used1[9][9] = {0},used2[9][9] = {0},used3[9][9] = {0}; | |
for(int i = 0;i < board.size();++i) | |
for(int j = 0;j < board[i].size();++j) |
This file contains hidden or 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
//Runtime: 12 ms, faster than 82.84% | |
//Memory Usage: 9.6 MB, less than 26.11% | |
class Solution { | |
public: | |
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { | |
unordered_map<int,int> hash; | |
vector<int> res; | |
for(auto i : nums1) ++hash[i]; | |
for(auto i : nums2) { |
This file contains hidden or 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
//Runtime: 12 ms, faster than 89.15% | |
//Memory Usage: 9.2 MB, less than 73.59% | |
class Solution { | |
public: | |
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { | |
set<int> res; | |
sort(nums2.begin(),nums2.end()); | |
for(auto a : nums1) { | |
if(binarySearch(nums2,a)) { |
This file contains hidden or 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
/* | |
* anticlockwise rotate | |
* first reverse left to right, then swap the symmetry | |
* 1 2 3 3 2 1 3 6 9 | |
* 4 5 6 => 6 5 4 => 2 5 8 | |
* 7 8 9 9 8 7 1 4 7 | |
*/ | |
//Runtime: 8 ms, faster than 100.00% | |
void anti_rotate(vector<vector<int> > &matrix) { |
This file contains hidden or 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
//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Plus One. | |
//Memory Usage: 770 KB, less than 50.21% of C++ online submissions for Plus One. | |
class Solution { | |
public: | |
vector<int> plusOne(vector<int>& digits) { | |
for(int i = digits.size() - 1; i >= 0; --i){ | |
if(digits[i] == 9) | |
digits[i] = 0; | |
else{ |
This file contains hidden or 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
//Runtime: 24 ms, faster than 98.78% of C++ online submissions | |
/** | |
* /------------\ | |
* nums: [2, 6, 4, 8, 10, 9, 15] | |
* minr: 2 4 4 8 9 9 15 | |
* <-------------------- | |
* maxl: 2 6 6 8 10 10 15 | |
* --------------------> | |
*/ |
This file contains hidden or 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
_ |
This file contains hidden or 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
//Runtime: 76 ms, faster than 100.00% of C++ | |
class Solution { | |
public: | |
vector<int> sortedSquares(vector<int>& A) { | |
vector<int> array; | |
for(int i = 0;i < A.size();++i){ | |
array.push_back(A[i] * A[i]); | |
} | |
std::sort(array.begin(),array.end()); |