GithubHelp home page GithubHelp logo

leetcode's Introduction

LeetCode

Solve problems from Leetcode. All the codes are tested using online-judge.

Here is a difficulty and frequency distribution chart for each problem, which I got from the Internet and is very useful. Feel free to make pull request for adding the difficulty and frequency for new problems here.

Please feel free to let me know if you have any problem or better solutions:)

Note: The problem link in the file will take you to the Old OJ, which will expire on October 12, 12pm PST.
You may use this link to the new OJ instead!

leetcode's People

Contributors

anniekim avatar applewjg avatar codingfansteve avatar defipanda avatar marthew777 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leetcode's Issues

scaramble string : my dp solution

首先想了个不到n^3的dp,后来发现不适合有字符重复的情况,下面这个dp解法能过,不过我觉得时间复杂度有点高了
sub[len][i][j] 表示s1从i开始与s2从j开始的长度为len是否为Scramble String

class Solution {
public:
bool sub[100][100][100];
bool isScramble(string s1, string s2) {
if(s1.length()!=s2.length())
return false;
int len = s1.length();
for(int i = 0;i<len;i++)
for(int j = 0;j<len;j++)
sub[1][i][j] = (s1[i]==s2[j])?1:0;
for(int k = 2;k<=len;k++)
for(int i = 0;i<=len- k ;i++)
for(int j = 0;j<=len-k;j++)
{
sub[k][i][j] = 0;
for(int p =i+1;p<i+k&&!sub[k][i][j];p++)
if( sub[p-i][i][j] && sub[k-p+i][p][p-i+j] )
sub[k][i][j] =1;
else if( sub[p-i][i][k-p+i+j] &&sub[k-p+i][p][j])
sub[k][i][j]=1;
else
sub[k][i][j] = 0;
}
return sub[len][0][0];

}

};

subsets:非递归版本

class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
    int size = S.size();
    vector<vector<int>> result;
    if(size==0) return result;
    sort(S.begin(),S.end());
    vector<int>v;
    result.push_back(v);
    for(int i=1;i<=size;i++)
    {
        int*p = new int[i];
        p[0]=0;
        int index = 0;
        while(1)
        {
            if(p[index] == size)
            {
                if(index==0)break;
                index--;
                p[index]++;
            }
            else if(index == i-1)
            {
                v.clear();
                for(int j = 0;j<i;j++)
                    v.push_back(S[p[j]]);
                result.push_back(v);
                p[index]++;
            }
            else
            {
                index++;
                p[index]=p[index-1]+1;
            }

        }
        delete[]p;
        p =NULL;
    }

    return result;

}
};

bug in "Interleaving String"

In such case: _s1 = "b", s2 = "acca", s3 = "abcba"_, it would return true.

This is related to:

    for (int i = 1; i <= N; ++i)
        dp[i][0] = s2[i-1] == s3[i-1];
    for (int j = 1; j <= M; ++j)
        dp[0][j] = s1[j-1] == s3[j-1];

Binary Tree Zigzag Level Order Traversal

class Solution {
public:
vector<vector > zigzagLevelOrder(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector> result;
if(root==NULL) return result;
vector<TreeNode * >v;
v.push_back(root);
bool leftsonfirst = true;
int index,begin = 0;
while(1)
{
vector newv;
newv.clear();
index = v.size();
if(index == begin) break;
int p = index-1;
while(p>=begin)
{
if(v[p]!=NULL)
{
newv.push_back(v[p]->val);
if(leftsonfirst)
{
v.push_back(v[p]->left);
v.push_back(v[p]->right);
}
else
{
v.push_back(v[p]->right);
v.push_back(v[p]->left);
}
}
p--;
}
if(newv.size())
result.push_back(newv);
begin = index;
leftsonfirst = !leftsonfirst;
}
return result;
}

};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.