This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from bisect import bisect_left | |
def countTriangleTripleIndex(a): | |
a.sort() | |
n, count = len(a), 0 | |
for i in range(n-2): | |
j = i+1 | |
k = bisect_left(a, a[i]+a[j])-1 | |
while k < n: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Given integer array and integer k, return all possible equation that array can be calculated to k. | |
Operations can be +, -, *, /, () | |
1. searchRecursive is the solution that the relative position of array can not be changed | |
2. search2Recursive is the solution that the relative position can be changed | |
''' | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bisect | |
''' | |
1. sort the array | |
2. iterate i from 0 to n-3 | |
3. j and k as another two rectangle point, j set to i+1, k set to i+2 | |
4. if a[i] + a[j] >= a[k], move k forward | |
5. if a[i] + a[j] < a[k], [j+1,...k-1] is legal rectangle, | |
nums += k-j-1, then move j forward | |
6. if k equal to n, [i, [j, ..., n-2], n-1] could be rectangle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InvalidException(Exception): | |
pass | |
def encode1(words): | |
res = "" | |
for word in words: | |
res += str(len(word)) + ':' + word | |
return res |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
/* | |
0xxxxxxx A single-byte US-ASCII code (from the first 127 characters) | |
110xxxxx One more byte follows | |
1110xxxx Two more bytes follow | |
11110xxx Three more bytes follow | |
10xxxxxx A continuation of one of the multi-byte characters |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 1 2 6 4 5 | |
3 | |
1 6 | |
2 4 | |
5 | |
Basic Method: | |
Use the first element of preorder to divide the problem into 2 sub problems, construct the tree recursively | |
1. make the first element as root node |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# m + n | |
# element expressed as (pos, value) | |
def multiply1(vec1, vec2): | |
i = 0 | |
j = 0 | |
vec = [] | |
while i < len(vec1) and j < len(vec2): | |
if vec1[i][0] > vec2[j][0]: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
# push O(n) | |
# top, pop, O(1) | |
class Stack1: | |
# initialize your data structure here. | |
def __init__(self): | |
self.q = deque() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; keys define by this won't get override by other module | |
(defvar my-keys-minor-mode-map (make-keymap) "my-keys-minor-mode keymap.") | |
;; ident | |
(global-set-key (kbd ">") 'my-indent-region) | |
(global-set-key (kbd "<") 'my-unindent-region) | |
;; remap c-a | |
(global-set-key [remap move-beginning-of-line] | |
'smarter-move-beginning-of-line) | |
;; remap m-d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package marshal | |
import ( | |
"bytes" | |
"encoding/json" | |
"testing" | |
) | |
type MarshalInterface interface { | |
Example() string |