Skip to content

Instantly share code, notes, and snippets.

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++;
}
class Solution {
public:
int hammingDistance(int x, int y) {
int z = x^y;
int c=0;
while(z!=0){
z= (z-1)&z;
c++;
}
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()){
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;
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];
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);
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]++;
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++){
/**
* 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 {
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);