func(this *Trie) Insert(word string) { curr := this.Root for i := range word { // check if has word[i] as a child k := word[i] - 'a' if curr.Children[k] == nil { curr.Children[k] = &TrieNode{} } curr = curr.Children[k] } curr.IsEnd = true }
func(this *Trie) Search(word string) bool { curr := this.Root for i := range word { // check if has word[i] as a child k := word[i] - 'a' if curr.Children[k] == nil { returnfalse } curr = curr.Children[k] } return curr.IsEnd }
func(this *Trie) StartsWith(word string) bool { curr := this.Root for i := range word { // check if has word[i] as a child k := word[i] - 'a' if curr.Children[k] == nil { returnfalse } curr = curr.Children[k] } returntrue }
Quick sorting is a pre-order traversal operation. Every time we dive into a node, we divide it into two portions: small and large. Then recursively apply this logic to these two portions.
Implementation
Let’s compare the two implementations below. The first one is straightforward, and the other one uses struct. #1
Thus use slice, if we want to implement a function that create a N size matrix
Implement
straight forward
bad memory using, not contiguous matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcnewMatrix(s int) [][]int { m := make([][]int, s) // Wrong! // // for _, row := range m { // row = make([]int, s) // } // for i, _ := range m { m[i] = make([]int, s) } return m }
method 2
ensure to get a contiguous matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcnewMatrix(s int) [][]int { // allocate the whole space of the matrix spaces := make([]int, s*s)
// create an empty matrix: [[] [] []...] m := make([][]int, s)
// assign spaces to the matrix i, j := 0, 0 for i < s { m[i] = spaces[j : j+s] i++ j += s }
return m }
Generic
more flexiable!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcnewMatrix[Tany](s int) [][]T { // allocate the whole space of the matrix spaces := make([]T, s*s)
// create an empty matrix: [[] [] []...] m := make([][]T, s)
// assign spaces to the matrix i, j := 0, 0 for i < s { m[i] = spaces[j : j+s] i++ j += s }
Given a slice a. There are 2 sub slices. One’s length is k, the other’s is p. These 2 sub slices are not overlaid. Find the max sum of the 2 sub slices.
Return -1 if can’t find one.
Example 1:
1 2
Input: [1,3,4,7,5,8,2,9], 3, 2 Output: 35
Example 2:
1 2
Input: [1,3,4,7,5,8], 100, 2 Output: -1
Thought
Move slice K through A, at the left and right side of K place the slice P. To improve the efficiency, store sums of K and P in hash tables.
Given a string s, return a cropped which max length is n. Also, spaces at prefix and postfix need to be removed.
Example 1:
1 2
Input: "Hello World", 8 Output: "Hello"
Example 2:
1 2
Input: " Hello World ", 100 Output: "Hello World"
Example 3:
1 2
Input: "Hello World", 3 Output: ""
Thought
Find the start point l and end point r then return result s[l:r] Left point is easy to find. If s[l] is space, pointer l move right. On the other hand, r starts from l + N, if s[r] is NOT space, pointer r move left.
The tricky point is that if the given string’s length equals to given n, and the string ends with character, the last word would be cropped.
funccrop(s string, n int)string { s = s + " "// append an extra space
// keep moving right until reaching the first character l := 0 for l < len(s)-1 { if s[l] == ' ' { l++ } else { break } }
// start from `l+n` or the last char (extra space) // keep moving left until reaching the first space r := min(l+n, len(s)-1) for r > l { if s[r] == ' ' { break } else { r-- } } return s[l:r] }
funcmin(i, j int)int { if i < j { return i } return j }
There is no output window in Mac’s Maya so I can’t print out text by std::out. The only way I know is to use Global::displayInfo. However, Global::displayInfo only accepts MString. I haven’t digged into what MString is yet but just provide some examples here. Hope you can get the basic idea:)
1 2 3 4 5 6 7 8
Global::displayInfo("hello"); // Works
string str = "hello"; Global::displayInfo(str); // This will fail
char str[10]; sprintf(str, "hello"); // Writes content to `str` Global::displayInfo(str); // Works
Write a custom Maya command that print “Hello world!” in Maya console. There are Python API 1.0 and 2.0. The syntax would be different. We will integrate with API 2.0 in this example.