Skip to content

Instantly share code, notes, and snippets.

// cannot pass larget test. Time limit
//currently it cannot pass large test. Time limit. My solution is a DFS now. and use a map to record passed nodes.
//try to use TRIE to solve it? But I am not sure whether it can pass the memory test on leetcode
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
target = end;
for( unordered_set<string>::iterator it = dict.begin();
// cannot pass the large text. recursion is time consuming.
// try to use a stack
class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
target = word;
checkers.assign(board.size(), vector<bool>(board[0].size(), false));
for( int i = 0; i < board.size(); ++i)
for( int j = 0; j < board[0].size(); ++j)
// at first I think I can divide the whole string into several parts, and then go over each substring using special iterator.
// after more consideration, I think I can just go over the strong, and push the current character to a specific box. cause
// the id of curBox preceed like 012012012012 if nRows = 3. so just create nRows boxes, push char into boxes, and then link all.
// be careful when nRows == 1
class Solution {
public:
string convert(string s, int nRows) {
if( s.size() <= nRows || nRows == 1)
return s;
// use the partition method in the partiton step in quicksort
// in the 1st round, set pivot as 0, so that the result will be like: 0000021121212221
// in the 2nd round, first find the end of 0 sequence, and do partition again by setting pivot 1
class Solution {
public:
void sortColors(int A[], int n) {
partition(A, 0, n-1, 0);
int first1;
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
//idea: divided the link list into 3 parts, start part, reverse part, last part
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode* mptr, *nptr;
ListNode* start = NULL;
mptr = nptr = NULL;
// 1. find the start, which is the end of the start part
int i = 2;
class Solution {
public:
int reverse(int x) {
return reverseInt(x,0);
}
int reverseInt(int rest, int result)
{
if(rest == 0)
return result;
//Given a string containing only digits, restore it by returning all possible valid IP address combinations.
//For example:
//Given "25525511135",
//return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)
// this is DFS, the recursion fucntion can also work with 2 paramaters
// at first time, forget to check the situation of 010 bacause 010 is valid to atoi
//Given a linked list, remove the nth node from the end of list and return its head.
//For example,
// Given linked list: 1->2->3->4->5, and n = 2.
// After removing the second node from the end, the linked list becomes 1->2->3->5.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;