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 countBattleships(vector<vector<char>>& board) { | |
int res = 0; | |
for(int i=0; i<board.size(); i++){ | |
for(int j=0; j<board[0].size(); j++){ | |
if((i-1<0||board[i-1][j]=='.')&&(j-1<0||board[i][j-1]=='.')&&board[i][j]=='X') | |
res++; | |
} |
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 hammingDistance(int x, int y) { | |
int z = x^y; | |
int c=0; | |
while(z!=0){ | |
z= (z-1)&z; | |
c++; | |
} |
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 lengthLongestPath(string s) { | |
vector<int> lvls(128, 0); | |
int res = 0; | |
int cur_len = 0; | |
int i=0; | |
int ss=0; | |
bool met_file=false; | |
while(i<s.length()){ |
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 getSum(int a, int b) { | |
int res = 0; | |
while(b!=0){ | |
int _a = a^b; | |
int _b = (a&b)<<1; | |
a = _a; | |
b = _b; | |
res = a|b; |
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 helper(int n, vector<int>& p){ | |
if(n<0) return 0; | |
if(p[n]!=0) | |
return p[n]; | |
p[n]=helper(n-1, p)+helper(n-2, p); | |
return p[n]; |
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
vector<int> grayCode(int n) | |
{ | |
vector<int> result(1, 0); | |
for (int i = 0; i < n; i++) { | |
int curCount = result.size(); | |
// push back all element in result in reverse order | |
while (curCount) { | |
curCount--; | |
int curNum = result[curCount]; | |
curNum += (1<<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
int lengthOfLongestSubstringTwoDistinct(string s) { | |
int res=0; | |
if(s.empty()) | |
return res; | |
unordered_map<char, int> myMap; | |
int p0=0, p1=0; | |
int count = 0; | |
while(p1<s.length()){ | |
char c = s[p1]; | |
myMap[c]++; |
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 maximumGap(vector<int>& nums) { | |
if(nums.size()<2) | |
return 0; | |
vector<vector<int> > curBucket(10); | |
vector<int> curNums=nums; | |
for(int i=0; i<32; i++){ | |
for(int j=0; j<curNums.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
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { |
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: | |
void helper(vector<vector<char>>& grid, int row, int col){ | |
if(row<0||col<0||row>=grid.size()||col>=grid[0].size()||grid[row][col]=='0') | |
return; | |
grid[row][col]='0'; | |
helper(grid, row, col-1); | |
helper(grid, row, col+1); | |
helper(grid, row-1, col); |
NewerOlder