Skip to content

Commit

Permalink
add email demo
Browse files Browse the repository at this point in the history
  • Loading branch information
wxnacy committed Jul 5, 2019
1 parent a732a86 commit b8ca70f
Show file tree
Hide file tree
Showing 59 changed files with 2,429 additions and 87 deletions.
53 changes: 53 additions & 0 deletions goland/src/leetcode/15-3sum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main
// 三数之和

import (
"testing"
"sort"
)
// 执行用时 : 1356 ms, 在3Sum的Go提交中击败了85.58% 的用户
// 内存消耗 : 155.8 MB, 在3Sum的Go提交中击败了12.01% 的用户
func threeSum(nums []int) [][]int {
res := make([][]int, 0)
sort.Ints(nums)
i := 0
length := len(nums)
for i < length - 2 {
if nums[i] > 0 {
break
}
left := i + 1
right := length - 1
for left < right {
s := nums[i] + nums[left] + nums[right]
if s == 0 {
res = append(res, []int{nums[i], nums[left], nums[right]})
}
if s <= 0 {
left++
for left < right && nums[left] == nums[left - 1] {
left++
}
} else {
right--
for left < right && nums[right] == nums[right + 1] {
right--
}
}
}
i++
for nums[i] == nums[i-1] {
i++
}
}
return res
}

func TestThreeSum(t *testing.T) {

rev := threeSum([]int{-1, 0, 1, 2, -1, -4})
if rev != [][]int{[]int{-1, -1, 2}, []int{-1, 0, 1}} {
t.Errorf("%d is error", rev)
}

}
41 changes: 41 additions & 0 deletions goland/src/sorts/heap_sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"
)

func HeapSort(nums []int) {
num_len := len(nums)
i := (num_len >> 1) - 1
for i > -1 {
heapify(nums, i, num_len)
i--
}

for i := num_len - 1; i > -1; i-- {
nums[i], nums[0] = nums[0], nums[i]
heapify(nums, 0, i)
}
fmt.Println(nums)
}

func heapify(nums []int, i, num_len int) {
k := i
left, right := i * 2 + 1, i * 2 + 2
if left < num_len && nums[left] > nums[k]{
k = left
}
if right < num_len && nums[right] > nums[k] {
k = right
}
if k != i {
nums[k], nums[i] = nums[i], nums[k]
heapify(nums, k, num_len)
}
}

func main() {
var nums = []int{6, 5, 8,4, 7, 2, 9, 3, 1}
HeapSort(nums)

}
44 changes: 44 additions & 0 deletions goland/src/sorts/quick_sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"fmt"
)

func QuickSort(nums []int) {
quickSort(nums, 0, len(nums) - 1)
}

func quickSort(nums []int, left, right int) {
if right >= left {
return
}
pivot := partition(nums, left, right)
quickSort(nums, left, pivot - 1)
quickSort(nums, pivot + 1, right)

}

func partition(nums []int, left, right int) int {
t := nums[left]
x := left
y := right
for x < y {
for x < y && nums[y] >= t {
y--
}
nums[x] = nums[y]
for x < y && nums[x] <= t {
x++
}
nums[y] = nums[x]
}
nums[x] = t
return x
}

func main() {
var nums = []int{6, 5, 8,4, 7, 2, 9, 3, 1}
QuickSort(nums)
fmt.Println(nums)

}
70 changes: 70 additions & 0 deletions goland/src/tests/loop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"testing"
"fmt"
)

var LIST = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
// var LIST []int

// func GetList() {
// if LIST == [] {
// LIST = make([]int, 0)
// for i := 0; i < 10000; i++ {
// LIST = append(LIST, i)
// }
// }
// return LIST
// }

func For() {
var total int
for i := 0; i < len(LIST); i++ {
for k := 0; k < len(LIST); k++ {
total += LIST[k]
}
}
}

func While() {
var total int
i := 0
for i < len(LIST) {
total += LIST[i]
i++
}
}

func ForEach(b *testing.B) {
var total int
for _, d := range LIST {
total += d
}
}

func BenchmarkWhile(b *testing.B) {
var total int
i := 0
for i < len(LIST) {
total += LIST[i]
i++
}
}

func BenchmarkForEach(b *testing.B) {
var total int
for _, d := range LIST {
total += d
}
}

func BenchmarkFor(b *testing.B) {
For()

}

func main() {

fmt.Print(LIST)
}
17 changes: 17 additions & 0 deletions java/generic/GenericClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class GenericClass<T>{
private T key;

public GenericClass(T key) {
this.key = key;

}

public T getKey() {
return this.key;
}
public static void main(String args[]){
System.out.println("Hello World");
GenericClass gc = new GenericClass("wxnacy");
System.out.println(gc.getKey());
}
}
36 changes: 36 additions & 0 deletions javascript/leetcode/15-3sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 执行用时 : 280 ms, 在3Sum的JavaScript提交中击败了91.01% 的用户
// 内存消耗 : 46.7 MB, 在3Sum的JavaScript提交中击败了67.20% 的用户

var threeSum = function (nums) {
let res = []
let length = nums.length;
nums.sort((a, b) => a - b) // 先排个队,最左边是最弱(小)的,最右边是最强(大)的
// 同符号,则无解退出
if( ( nums[0] < 0 && nums[length - 1] < 0 )
|| ( nums[0] > 0 && nums[length - 1] > 0 ) ) {
return res
}
for (let i = 0; i < length - 2;) {
if (nums[i] > 0) break; // 优化2: 最左值为正数则一定无解
let first = i + 1
let last = length - 1
do {
if (first >= last || nums[i] * nums[last] > 0) break // 两人选相遇,或者三人同符号,则退出
let result = nums[i] + nums[first] + nums[last]
if (result === 0) { // 如果可以组队
res.push([nums[i], nums[first], nums[last]])
}
if (result <= 0 ) { // 实力太弱,把菜鸟那边右移一位
while (first < last && nums[first] === nums[++first]){} // 如果相等就跳过
} else { // 实力太强,把大神那边右移一位
while (first < last && nums[last] === nums[--last]) {}
}
} while (first < last)
while (nums[i] === nums[++i]) {}
}
return res
}


var res = threeSum([-1, 0, 1, 2, -1, -4])
console.log(res)
69 changes: 69 additions & 0 deletions python/algorithm/fibonacci_sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])
# Description: 斐波那契数列

def fibonacci_recursive(n):
'''
递归的方式,理解简单,不会在真实开发中用到
'''
if n < 3:
return 1
else:
return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2)

def fibonacci_for(n):
'''
使用 for 循环挨个计算,减少了递归中的重复计算
时间复杂度 O(n)
空间复杂度 O(1)
'''
if n < 3:
return 1
n1, n2 = 1, 1
n3 = 0
for i in range(3, n + 1):
n3 = n1 + n2
n1, n2 = n2, n3
return n3

nums = [1, 1]
def fibonacci_array(n):
'''
for 循环的方式已经很快了,但是如果想要获取多个值时,每次都要从头计算
这时候我们可以把计算的结果保存起来,下次可以直接使用,这是一种空间换时间的算法。
时间复杂度 O(n)
空间复杂度 O(n)
'''
if len(nums) < n:
for i in range(len(nums), n):
nums.append(nums[i - 1] + nums[i - 2])
return nums[n - 1]

import unittest

class TestMain(unittest.TestCase):

def setUp(self):
'''before each test function'''
pass

def tearDown(self):
'''after each test function'''
pass

def do(self, func):
'''todo'''
n = 8
res = 21
self.assertEqual(func(n), res)
pass

def test_func(self):
self.do(fibonacci_recursive)
self.do(fibonacci_for)
self.do(fibonacci_array)

if __name__ == "__main__":
unittest.main()

57 changes: 57 additions & 0 deletions python/algorithm/string_compression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy([email protected])
# Description: 字符串压缩算法
# aabbbccccdef => a2b3c4d1e1f1

def compress(s):
if not s:
return s
tmp = [s[0]]
res = ''
for i in range(1, len(s)):
if tmp[-1] == s[i]:
tmp.append(s[i])
else:
res = f"{res}{tmp[-1]}{len(tmp)}"
tmp = [s[i]]
res = f"{res}{tmp[-1]}{len(tmp)}"
return res

def decompress(s):
if not s:
return s
res = []
for i in range(0, len(s), 2):
res.extend([s[i]] * int(s[i + 1]))
return ''.join(res)


import unittest

class TestMain(unittest.TestCase):

def setUp(self):
'''before each test function'''
pass

def tearDown(self):
'''after each test function'''
pass

def do(self, func):
'''todo'''
s = "aabbbccccdef"
res = "a2b3c4d1e1f1"
self.assertEqual(decompress(func(s)), s)
pass

def test_func(self):
self.do(compress)

if __name__ == "__main__":
unittest.main()




Loading

0 comments on commit b8ca70f

Please sign in to comment.