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
class Solution { | |
public: | |
int largestPalindrome(int n) { | |
int l = pow(10, n-1), r = pow(10, n) - 1; | |
for(int i = r; i>= 1; i--) { | |
string s = to_string(i); | |
string t = s; | |
reverse(t.begin(), t.end()); | |
long ans = stol(s + t); | |
for(long j = r; j * j >= ans; 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
class Solution { | |
public: | |
int findRadius(vector<int>& houses, vector<int>& heaters) { | |
if (heaters.size() == 0) return 0; | |
sort(houses.begin(), houses.end()); | |
sort(heaters.begin(), heaters.end()); | |
int radius = 0; | |
int index = 0; | |
for(int i = 0; i < houses.size(); i++) { | |
while(index + 1 < heaters.size() && abs(heaters[index+1] - houses[i]) <= abs(heaters[index] - houses[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
class Solution { | |
public: | |
int findNthDigit(int n) { | |
long digit = 1, sum = 9; | |
while(n > digit * sum) { | |
n = n - digit * sum; | |
sum *= 10; | |
digit++; | |
} | |
int index = n % digit; |
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
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: |
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
class MinStack { | |
public: | |
stack<int> all; | |
stack<int> minsta; | |
/** initialize your data structure here. */ | |
MinStack() { | |
} | |
void push(int x) { |
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
class Solution { | |
public: | |
bool judgeSquareSum(int c) { | |
int half = (c >> 1), sb, b; | |
for(int a = 0; a * a <= half; a++) { | |
sb = c - a * a; | |
b = sqrt(sb); | |
if (b * b == sb) return true; | |
} | |
return false; |
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
class Solution { | |
public: | |
string countAndSay(int n) { | |
if (n == 0) return ""; | |
string res = "1"; | |
while (--n) { | |
string cur = ""; | |
//cout << "new: n: " << n << ", res: " << res << endl; | |
for(int i = 0; i < res.size(); i++) { | |
int count = 1; |
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
class Solution { | |
public: | |
int lengthOfLastWord(string s) { | |
int len = 0, tail = s.length() - 1; | |
while (tail >= 0 && s[tail] == ' ') tail--; | |
while (tail >= 0 && s[tail] != ' ') { | |
len++; | |
tail--; | |
} | |
return len; |
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
class Solution { | |
private: | |
public: | |
void DFS(string s, int start, int step, string ip, vector<string>& ans) { | |
if (start == s.size() && step == 4) { | |
ip.erase(ip.end() - 1); | |
ans.push_back(ip); | |
return; | |
} | |
if (s.size()-start >(4-step)*3) return; |
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
class Solution { | |
public: | |
vector<vector<int>> combinationSum(vector<int>& candidates, int target) { | |
sort(candidates.begin(), candidates.end()); | |
vector<vector<int>> res; | |
vector<int> curr; | |
bk(res, candidates, curr, 0, target); | |
return res; | |
} | |
void bk(vector<vector<int>>& ans, vector<int>& candidates, vector<int> curr, int last_use, int rest_target) { |