class Solution4 { public: bool hasPathSum(TreeNode *root, int sum) { if (!root) return false;
bool ret = false; TreeNode* nd = root; while (nd) { if (!nd->left) { //NOTE: can not write `return true` here. since morris traversion modified //struture of tree, we need to run over to make sure all changes restored. //because of this, this may not as fast as primitive solution. if (!nd->right && nd->val == sum) ret = true; if (nd->right) nd->right->val += nd->val; nd = nd->right; } else { auto* l = nd->left; while (l->right && l->right != nd) l = l->right; if (l->right == nd) { if (!l->left && l->val == sum) ret = true; nd = nd->right; l->right = nullptr; } else { nd->left->val += nd->val; if (nd->right) nd->right->val += nd->val; l->right = nd; nd = nd->left; } } }
/* This is cached in the caller at the start and compared at each * iteration. If it changes during the iteration then * objc_enumerationMutation() will be called, throwing an exception. */ state->mutationsPtr = (unsignedlong *)size; count = MIN(len, size - state->state); /* If a mutation has occurred then it's possible that we are being asked to * get objects from after the end of the array. Don't pass negative values * to memcpy. */ if (count > 0) { IMP imp = [self methodForSelector: @selector(objectAtIndex:)]; int p = state->state; int i;
/* * Since the out pointers in the list are always * uninitialized, we use the pointers themselves * as storage for the Ptrlists. */ union Ptrlist { Ptrlist *next; State *s; };
/* Create singleton list containing just outp. */ Ptrlist* list1(State **outp) { Ptrlist *l; l = (Ptrlist*)outp; l->next = NULL; return l; }
/* Free the tree of states rooted at d. */ void freestates(DState *d) { if(d == NULL) return; freestates(d->left); freestates(d->right); d->left = freelist; freelist = d; }
/* Throw away the cache and start over. */ void freecache(void) { freestates(alldstates); alldstates = NULL; nstates = 0; }
There are two possible ways to avoid the seemingly unbounded tracking of space implied by POSIX submatching semantics. First, it turns out that matching the regular expression backward bounds the bookkeeping to being linear in the size of the regular expression. This program demonstrates the technique.
Because of the nature of class clusters, string objects aren’t actual instances of the NSString or NSMutableString classes but of one of their private subclasses. Although a string object’s class is private, its interface is public, as declared by these abstract superclasses, NSString and NSMutableString. The string classes adopt the NSCopying and NSMutableCopying protocols, making it convenient to convert a string of one type to the other.
iterm2 is a great terminal in Mac OS X. It has a lot of features go beyond what you thought a terminal might be. here is a tip I learned recently.
Turns out you can select text without mouse in iterm2. just by press CMD-f to activate find. Enter some initial text you wish to copy, you’ll find that the last matching highlighted with selected state. use CMD-G or CMD-Shift-G to switch selected text as you want and then press tab to advanced the selected text by words. To advance the beginning of the selection to the left, press Shift-tab. At most one line of text can be selected this way.