Skip to content

Instantly share code, notes, and snippets.

@autekroy
autekroy / LeetCode OJ - Remove Duplicates from Sorted Array II.cpp
Created November 3, 2014 21:19
Remove Duplicates from Sorted Array II
class Solution {
public:
int removeDuplicates(int A[], int n) {
int index = 0;
bool twice = false;
if(n == 0) return 0;
for(int i = 1; i < n; i++)
if(A[index] != A[i]){
index ++;
@autekroy
autekroy / LeetCode OJ - Implement strStr().cpp
Created November 1, 2014 23:06
LeetCode OJ - Implement strStr()
class Solution {
public:
bool check(char *a,char *b, int len){
for(int i = 0; i < len; i++)
if(*(a+i) != *(b+i))
return false;
return true;
}
char *strStr(char *haystack, char *needle) {
int lenH = strlen(haystack);
@autekroy
autekroy / LeetCode OJ - Remove Duplicates from Sorted Array.cpp
Created November 1, 2014 22:31
LeetCode OJ - Remove Duplicates from Sorted Array
class Solution {
public:
int removeDuplicates(int A[], int n) {
int p1 = 0;
int last = A[p1];
if(n == 0) return 0;
if(n == 1) return 1;
for(int p2 = 1; p2 < n; p2++){
@autekroy
autekroy / LeetCode OJ - Palindrome Partitioning II.cpp
Created October 30, 2014 07:36
LeetCode OJ - Palindrome Partitioning II
class Solution {
public:
int minCut(string s) {
int n = s.size();
bool isPal[n][n];
int m[n + 1];
for(int i = 0; i <= n; i++)
m[i] = n - i - 1;
@autekroy
autekroy / LeetCode OJ - Palindrome Partitioning.cpp
Created October 30, 2014 07:36
LeetCode OJ - Palindrome Partitioning
class Solution {
public:
vector<vector<string>> partition(string s) {
int n = s.size();
vector<vector<string>> result;
vector<string> path;
vector<vector<bool>> isPal(n, vector<bool>(n, false));
for(int i = n - 1; i >= 0; i--)
for(int j = i; j < n; j++)
@autekroy
autekroy / LeetCode OJ - Interleaving String.cpp
Created October 30, 2014 07:32
LeetCode OJ - Interleaving String
class Solution {
public:
bool isInterleave(string s1, string s2, string s3) {
int len1 = s1.size(), len2 = s2.size(), len3 = s3.size();
if(len1 + len2 != len3)
return false;
bool dp[len1 + 1][len2 + 1];
@autekroy
autekroy / LeetCode OJ - Longest Palindromic Substring.cpp
Created October 30, 2014 07:31
LeetCode OJ - Longest Palindromic Substring
class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
int start = 0, Max = 0;
bool dp[n][n];
for(int i = n - 1; i >= 0; i--)
for(int j = i; j < n; j++){
dp[i][j] = ( s[i]==s[j] && ((j - i < 2) || dp[i+1][j-1] ) );
@autekroy
autekroy / LeetCode OJ - Minimum Path Sum.cpp
Created October 28, 2014 17:42
LeetCode OJ - Minimum Path Sum
class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
int column = grid[0].size();
int row = grid.size();
int dp[column];
memset(dp, 0, sizeof(dp));
for(int i = 0; i < row; i++){
@autekroy
autekroy / LeetCode OJ - Unique Binary Search Trees.cpp
Created October 26, 2014 09:06
LeetCode OJ - Unique Binary Search Trees
class Solution {
public:
int numTrees(int n) {
int dp[n+1];// index from 0 to n
memset(dp, 0, sizeof(dp));// you have to initialize to make sure the initial values
dp[0] = 1;
for(int i = 1; i <= n; i++)
for(int j = 0; j < i; j++)
@autekroy
autekroy / LetCode OJ - Merge Sorted Array.cpp
Created October 20, 2014 06:26
LetCode OJ - Merge Sorted Array
class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int lastPos = m + n - 1;
int flagA = m - 1, flagB = n - 1;
while(lastPos >= 0){
if(flagB < 0 || (flagA >= 0 && A[flagA] >= B[flagB])){
A[lastPos] = A[flagA];
flagA --;