From: AceVest Date: Sun, 17 Jan 2021 14:30:18 +0000 (+0800) Subject: ... X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zpipe.c?a=commitdiff_plain;h=a6f6c95c989b3e24d3b0e0e54f304c5c69c315e9;p=acecode.git ... --- diff --git a/learn/doc/zshrc b/learn/doc/zshrc index 077df55..ee3c8fc 100644 --- a/learn/doc/zshrc +++ b/learn/doc/zshrc @@ -12,7 +12,8 @@ export ZSH="/Users/ace/.oh-my-zsh" # 需要安装字体: https://github.com/powerline/fonts.git # 字体安装之后,需要将终端的字体设置为‘Roboto Mono for Powerline -ZSH_THEME="agnoster" +#ZSH_THEME="agnoster" +ZSH_THEME="zhaoyanbai" # Set list of themes to pick from when loading at random # Setting this variable when ZSH_THEME=random will cause zsh to load @@ -76,6 +77,7 @@ ZSH_THEME="agnoster" # 将incr-0.2.zsh保存到~/.oh-my-zsh/custom/plugins/incr/并重命名为incr.plugin.zsh plugins=( git + zsh-autosuggestions # incr ) @@ -118,6 +120,7 @@ export PATH=$PATH:$HOME/Library/Arduino15/packages/arduino/tools/avrdude/6.0.1-a export PATH=$PATH:/usr/local/opt/go/libexec/bin export PATH=$PATH:/Users/ace/sys/gnu-mcu-eclipse/riscv-none-gcc/8.2.0-2.2-20190521-0004/bin export RTOS_PATH=$HOME/workspace/acecode/rtos/ +export PATH=$PATH:$HOME/go/bin export PATH=/usr/local/sbin:$PATH export PATH=$HOME/.local/bin:$PATH diff --git a/learn/go/qmc0_to_mp3.go b/learn/go/qmc0_to_mp3.go new file mode 100644 index 0000000..f7c0a89 --- /dev/null +++ b/learn/go/qmc0_to_mp3.go @@ -0,0 +1,92 @@ +package main + +import ( + "fmt" + "log" + "os" + "strings" +) + +func main() { + if len(os.Args)!=2 { + log.Fatal("Which file you want to convert ?") + } + fmt.Println("Converting "+os.Args[1]) + convert(os.Args[1]) + fmt.Println("Done.") +} +func encrypt(offset int, buf []byte, len int) int { + if offset < 0 { + return -1 + } + for i := 0; i < len; i++ { + buf[i] ^= mapL(offset + i) + } + return 0 +} + +func mapL(v int) byte { + key := []byte{ + 0x77, 0x48, 0x32, 0x73, 0xDE, 0xF2, 0xC0, 0xC8, 0x95, 0xEC, 0x30, 0xB2, + 0x51, 0xC3, 0xE1, 0xA0, 0x9E, 0xE6, 0x9D, 0xCF, 0xFA, 0x7F, 0x14, 0xD1, + 0xCE, 0xB8, 0xDC, 0xC3, 0x4A, 0x67, 0x93, 0xD6, 0x28, 0xC2, 0x91, 0x70, + 0xCA, 0x8D, 0xA2, 0xA4, 0xF0, 0x08, 0x61, 0x90, 0x7E, 0x6F, 0xA2, 0xE0, + 0xEB, 0xAE, 0x3E, 0xB6, 0x67, 0xC7, 0x92, 0xF4, 0x91, 0xB5, 0xF6, 0x6C, + 0x5E, 0x84, 0x40, 0xF7, 0xF3, 0x1B, 0x02, 0x7F, 0xD5, 0xAB, 0x41, 0x89, + 0x28, 0xF4, 0x25, 0xCC, 0x52, 0x11, 0xAD, 0x43, 0x68, 0xA6, 0x41, 0x8B, + 0x84, 0xB5, 0xFF, 0x2C, 0x92, 0x4A, 0x26, 0xD8, 0x47, 0x6A, 0x7C, 0x95, + 0x61, 0xCC, 0xE6, 0xCB, 0xBB, 0x3F, 0x47, 0x58, 0x89, 0x75, 0xC3, 0x75, + 0xA1, 0xD9, 0xAF, 0xCC, 0x08, 0x73, 0x17, 0xDC, 0xAA, 0x9A, 0xA2, 0x16, + 0x41, 0xD8, 0xA2, 0x06, 0xC6, 0x8B, 0xFC, 0x66, 0x34, 0x9F, 0xCF, 0x18, + 0x23, 0xA0, 0x0A, 0x74, 0xE7, 0x2B, 0x27, 0x70, 0x92, 0xE9, 0xAF, 0x37, + 0xE6, 0x8C, 0xA7, 0xBC, 0x62, 0x65, 0x9C, 0xC2, 0x08, 0xC9, 0x88, 0xB3, + 0xF3, 0x43, 0xAC, 0x74, 0x2C, 0x0F, 0xD4, 0xAF, 0xA1, 0xC3, 0x01, 0x64, + 0x95, 0x4E, 0x48, 0x9F, 0xF4, 0x35, 0x78, 0x95, 0x7A, 0x39, 0xD6, 0x6A, + 0xA0, 0x6D, 0x40, 0xE8, 0x4F, 0xA8, 0xEF, 0x11, 0x1D, 0xF3, 0x1B, 0x3F, + 0x3F, 0x07, 0xDD, 0x6F, 0x5B, 0x19, 0x30, 0x19, 0xFB, 0xEF, 0x0E, 0x37, + 0xF0, 0x0E, 0xCD, 0x16, 0x49, 0xFE, 0x53, 0x47, 0x13, 0x1A, 0xBD, 0xA4, + 0xF1, 0x40, 0x19, 0x60, 0x0E, 0xED, 0x68, 0x09, 0x06, 0x5F, 0x4D, 0xCF, + 0x3D, 0x1A, 0xFE, 0x20, 0x77, 0xE4, 0xD9, 0xDA, 0xF9, 0xA4, 0x2B, 0x76, + 0x1C, 0x71, 0xDB, 0x00, 0xBC, 0xFD, 0xC, 0x6C, 0xA5, 0x47, 0xF7, 0xF6, + 0x00, 0x79, 0x4A, 0x11} + if v >= 0 { + if v > 0x7FFF { + v %= 0x7FFF + } + } else { + v = 0 + } + return key[(v*v+80923)%256] +} + +func convert(in string) bool { + rindex:=strings.LastIndex(in,".") + name:=in[:rindex] + e:=in[rindex+1:] + ext:="" + if e=="qmcflac" || e=="mflac"{ + ext="flac" + } else if e=="qmc0" || e=="qmc3"{ + ext="mp3" + } + fin,err:=os.Open(in) + if err!=nil{ + log.Fatal("cannot open "+in) + return false + } + fout,err:=os.Create(name+"."+ext) + if err!=nil{ + log.Fatal("cannot create new file.") + return false + } + defer fin.Close() + defer fout.Close() + + stat,_:=fin.Stat() + buf:=make([]byte,stat.Size()) + fin.Read(buf) + encrypt(0,buf,len(buf)) + fout.Write(buf) + + return true +} diff --git a/learn/leetcode/01.4.combinationSum.go b/learn/leetcode/01.4.combinationSum.go new file mode 100644 index 0000000..7daffdd --- /dev/null +++ b/learn/leetcode/01.4.combinationSum.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "sort" +) + +func combinationSum(candidates []int, target int) [][]int { + sort.Ints(candidates) + + var ret [][]int + + for i := range candidates { + sum := 0 + stack := make([]int, 0) + + for j := i; j < len(candidates); { + n := candidates[j] + for sum+n < target { + sum += n + stack = append(stack, n) + } + if sum == target { + ret = append(ret, stack) + sum := 0 + stack := make([]int, 0) + j++ + } else if sum < target { + j++ + } else { + stack = stack[0 : len(stack)-1] + sum -= n + } + } + } + + return ret +} + +func main() { + fmt.Println(combinationSum([]int{2, 3, 6, 7}, 7)) + fmt.Println(combinationSum([]int{2, 3, 5}, 8)) +} diff --git a/learn/leetcode/09.removeNthFromEnd.go b/learn/leetcode/09.removeNthFromEnd.go new file mode 100644 index 0000000..75b9db0 --- /dev/null +++ b/learn/leetcode/09.removeNthFromEnd.go @@ -0,0 +1,234 @@ +package main + +import ( + "fmt" + "math/rand" + "time" +) + +// ListNode list node +type ListNode struct { + Val int + Next *ListNode +} + +func removeNthFromEnd(head *ListNode, n int) *ListNode { + var poineer *ListNode + var deleter *ListNode + + poineer = head + deleter = &ListNode{0, head} + + // 向前移动n个,就和deleter相隔n+1个节点 + // 这样deleter就指向了要删除节点的父节点 + for i := 0; i < n && poineer != nil; i++ { + poineer = poineer.Next + } + + // 一次遍历到尾部节点 + for poineer != nil { + poineer = poineer.Next + deleter = deleter.Next + } + + // 删除倒数第n个节点 + if deleter.Next != head { + deleter.Next = deleter.Next.Next + } else { + head = head.Next + } + + return head +} + +func init() { + rand.Seed(time.Now().UnixNano()) +} +func printList(p *ListNode) { + for p != nil { + fmt.Printf("%v -> ", p.Val) + p = p.Next + } + + fmt.Println("") +} + +func lengthOfLastWord(s string) int { + c := 0 + for i := 0; i < len(s); i++ { + if s[i] == ' ' { + c = 0 + } else { + c++ + } + } + + return c +} + +func reverseBetween(head *ListNode, m int, n int) *ListNode { + if m == n { + return head + } + + T := &ListNode{0, head} + + ph := T + pe := T.Next + for i := 1; i < m; i++ { + ph = ph.Next + pe = pe.Next + } + pt := pe.Next + + for i := 0; i < (n - m); i++ { + pe.Next = pt.Next + pt.Next = ph.Next + ph.Next = pt + + pt = pe.Next + } + + return T.Next +} + +func swapPairs(head *ListNode) *ListNode { + if head == nil { + return head + } + + ph := head + pt := head.Next + + if pt != nil { + ph.Next = pt.Next + pt.Next = ph + + head = pt + } + + for ph.Next != nil { + pt := ph.Next.Next + if pt != nil { + ph.Next.Next = pt.Next + pt.Next = ph.Next + ph.Next = pt + + ph = ph.Next.Next + } else { + break + } + } + + return head +} + +func factorial(n int) int { + + for i := n - 1; i > 1; i-- { + n *= i + } + if n == 0 { + return 1 + } + return n +} + +func getInxNum() func(int) int { + nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} + return func(inx int) int { + c := 0 + + for i := 0; i < len(nums); i++ { + if nums[i] != 0 { + if c == inx { + n := nums[i] + nums[i] = 0 + return n + } + c++ + } + } + return 0 + } +} +func getPermutation(n int, k int) string { + getNext := getInxNum() + ret := 0 + + k-- + + for i := n; i > 0; i-- { + FIB := factorial(i - 1) + div := k / FIB + mod := k % FIB + + ret = ret*10 + getNext(div) + + k = mod + + } + return fmt.Sprintf("%v", ret) +} + +func findMedianSortedArrays(nums1 []int, nums2 []int) float64 { + maxN := (len(nums1) + len(nums2) - 1) / 2 + + i := 0 + j := 0 + + a := 0 + b := 0 + for c := 0; ; c++ { + if i >= len(nums1) && j < len(nums2) { + b = nums2[j] + j++ + } else if j >= len(nums2) && i < len(nums1) { + b = nums1[i] + i++ + } else if i < len(nums1) && j < len(nums2) { + if nums1[i] < nums2[j] { + b = nums1[i] + i++ + } else { + b = nums2[j] + j++ + } + } + if c == maxN+1 { + break + } + + a = b + } + //fmt.Println(a, b) + + if (len(nums1)+len(nums2))%2 == 0 { + //fmt.Println("as") + return float64(a+b) / 2.0 + } + + return float64(a) +} + +func main() { + head := &ListNode{1234, nil} + p := head + + cnt := rand.Intn(20) + 1 // 保证是大于或等于1的数 + cnt = 6 + for i := 0; i < cnt; i++ { + p.Next = &ListNode{i, nil} + p = p.Next + } + printList(head) + p = swapPairs(head) + printList(p) + + fmt.Println(findMedianSortedArrays([]int{1, 3}, []int{2})) + fmt.Println(findMedianSortedArrays([]int{1, 2}, []int{3, 4})) + fmt.Println(findMedianSortedArrays([]int{1}, []int{})) + fmt.Println(findMedianSortedArrays([]int{1, 2}, []int{})) + fmt.Println(findMedianSortedArrays([]int{1, 2, 3}, []int{})) + fmt.Println(findMedianSortedArrays([]int{1, 2, 3}, []int{1, 2, 3})) +} diff --git a/learn/leetcode/go.mod b/learn/leetcode/go.mod new file mode 100644 index 0000000..034e93c --- /dev/null +++ b/learn/leetcode/go.mod @@ -0,0 +1,3 @@ +module leetcode + +go 1.14