Skip to content

Commit cb6c412

Browse files
author
pzqu
committed
合并个人算法项目,整理README
1 parent 14d970c commit cb6c412

File tree

22 files changed

+620
-56
lines changed

22 files changed

+620
-56
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
*.idea
2-
*.log*
2+
*.log*
3+
*.pyc
4+
*.pyo
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @Author: pzqu
3+
* @Date: 2020-03-16 23:30
4+
*/
5+
package String
6+
7+
import (
8+
"bytes"
9+
"strconv"
10+
)
11+
12+
func compressString(S string) string {
13+
if len(S) <= 2 {
14+
return S
15+
}
16+
count := 1
17+
var res bytes.Buffer
18+
res.WriteString(string(S[0]))
19+
for i := 1; i < len(S); i++ {
20+
if S[i] == S[i-1] {
21+
count ++
22+
} else {
23+
res.WriteString(strconv.Itoa(count))
24+
res.WriteString(string(S[i]))
25+
count = 1
26+
}
27+
}
28+
29+
res.WriteString(strconv.Itoa(count))
30+
resStr := res.String()
31+
if len(resStr) < len(S) {
32+
return resStr
33+
} else {
34+
return S
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @Author: pzqu
3+
* @Date: 2020-03-16 23:30
4+
*/
5+
package String
6+
7+
import (
8+
"testing"
9+
)
10+
11+
//Test
12+
func TestCompressString(t *testing.T) {
13+
14+
tables := []struct {
15+
x string
16+
y string
17+
}{
18+
{
19+
"aabcccccaaa", "a2b1c5a3",
20+
},
21+
{
22+
"aabcccccaa", "a2b1c5a2",
23+
},
24+
{"", "",},
25+
{"abbccd", "abbccd",},
26+
}
27+
28+
for _, table := range tables {
29+
y := compressString(table.x)
30+
if y != table.y {
31+
t.Fatalf("error x:%s,y:%s", table.x, table.y)
32+
}
33+
}
34+
35+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package _013_Partition_Array_Into_Three_Parts_With_Equal_Sum
2+
3+
//暴力
4+
func canThreePartsEqualSum(A []int) bool {
5+
suma, sumb, sumc := 0, 0, 0
6+
tmpsumc := 0
7+
8+
for k := 1; k < len(A); k++ {
9+
tmpsumc = tmpsumc + A[k]
10+
}
11+
12+
for i := 0; i < len(A)-2; i++ {
13+
suma = suma + A[i]
14+
sumb = 0
15+
sumc = tmpsumc
16+
for j := i + 1; j < len(A)-1; j++ {
17+
sumb = sumb + A[j]
18+
sumc = sumc - A[j]
19+
if suma == sumb && sumb == sumc {
20+
return true
21+
}
22+
}
23+
24+
tmpsumc = tmpsumc - A[i+1]
25+
}
26+
return false
27+
}
28+
29+
//数学
30+
func canThreePartsEqualSum2(A []int) bool {
31+
tmpSum, sum, singeSum, count := 0, 0, 0, 0
32+
33+
for i := 0; i < len(A); i++ {
34+
sum = sum + A[i]
35+
}
36+
if sum%3 != 0 {
37+
return false
38+
}
39+
singeSum = sum / 3
40+
41+
for i := 0; i < len(A); i++ {
42+
tmpSum = tmpSum + A[i]
43+
if tmpSum == singeSum {
44+
count = count + 1
45+
tmpSum = 0
46+
if count == 2 && i < len(A)-1 {
47+
return true
48+
}
49+
}
50+
}
51+
return false
52+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package _013_Partition_Array_Into_Three_Parts_With_Equal_Sum
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
"testing"
7+
)
8+
9+
var tables []struct {
10+
x []int
11+
y bool
12+
}
13+
14+
func init() {
15+
tables = []struct {
16+
x []int
17+
y bool
18+
}{
19+
{[]int{0, 2, 1, -6, 6, -7, 9, 1, 2, 0, 1}, true},
20+
{[]int{0, 2, 1, -6, 6, 7, 9, -1, 2, 0, 1}, false},
21+
{[]int{3, 3, 6, 5, -2, 2, 5, 1, -9, 4}, true},
22+
{[]int{2, 8, 15, -5, 0, 9, -3, 4}, true},
23+
{[]int{1414, -1401, 8970, -7112, 2902, -244, -1239, 2641, -3658, 3527, -4127, 3986, 1194, -5945, 7594, -5476, 4211, -744, -2501, -2528, 1204, 4597, -4284, -1953, 7848, -6134, 267, 5044, -363, 291, 863, -8654, 1364, 2503, -1634, 3207, -2730, 2909, -2108, -3074, 3592, -1280, -184, 4464, -1645, -5508, 6202, 941, -2043, 2144, -5900, 6425, -7410, 3964, -1472, -877, 1015, 560, 289, 2954, -2262, -1805, 2740, -834, -2639, -135, 971, -1732, 6683, -5498, 1071, 2380, 609, -2175, 1847, -3143, -514, 4438, -6423, 6886, 1641, -6718, 5776, 406, -7806, 6080, 367, -3561, 5258, -6258, 2606, 605, 43, -4503, 3455, 2391, -3070, -1483, -1126, -1272, 4856, -2234, 5252, -286, 1100, -6419, 2430, 468, 1385, -311, -2171, 709, -138, 277, -2591, 5171, -2809, 3395, -5200, 3792, -4161, 2926, 3091, -2245, -2089, -133, -3207, 5714, 1698, -1307, -227, 1775, -8361, 5012, -119, 842, 502, -3452, 5162, -1005, -5357, 6064, -2516, 632, 2741, -2636, -334, -67, -3235, 4215, 1188, -2739, 3326, -1886, 540, -2142, -1787, 5307, -4293, 3215, -1871, -2582, -2923, 3289, 4353, -1490, -3717, 1526, 3738, -2133, -3137, 678, 645, 3268, -3943, -2977, 6590, -5169, 1893, -2836, 3474, 4648, -5145, 1078, -3012, 5634, -48, 560, 227, -7929, 2029, 6789, -8258, 4800, -4300, 5709, -999, -2933, 5370, -5013, 93, 5054, -8093, 7635, -2652, 487, 1409, 90, 852, -5035, 3975, -5425, 2024, 2395, -443, 1509, 588, -6281, 2724, -3059, 1569, 2479, -4469, 864, 1894, 1328, -311, -1933, 1584, -2024, -125, 4545, -2201, 2577, -5986, 4251, -4502, 2554, -433, 3273, -2101, 4858, -7071, 4742, -742, -1593, 3196, -6103, 4719, 617, 19, -2365, 45, 667, -1234, -3165, 4799, -1040, -2402, -1225, 6315, -5822, 1778, 2626, -958, -931, 4165, -73, -7123, 2153, 4722, -1742, -1028, -1537, -313, -1701, -707, 5417, -2599, 1354, 1915, -1080, -396, 383, -5151, 6806, -1309, -614, 457, 1338, -6685, 7299, -7373, 2857, -789, 4591, -6102, -281, 7517, -3607, 1862, -2760, -2190, 412, 4244, 350, -3600, 1275, 2108, -1242, 3510, -2460, -3655, 627, 5391, -5782, 2451, -4736, 5825, -1033, -2763, 878, -2275, 2980, 2719, -1487, -136, 173, -4786, 1365, 5820, 1075, -222, -1283, -5610, 5628, -3649, -1139, 4810, -4711, -1790, 5042, -698, -3293, 1395, 1331, -3986, 1197, 6174, 867, -1341, -689, -2433, -2272, 3124, -595, -1718, 5951, -8254, 4927, -431, -4660, 4000, -3325, 713, 71, 651, 247, 289, 1386, 3796, -692, -4704, -435, 3757, 2282, -2138, -647, 129, -3736, -534, -29, 3780, -1087, -2563, 2915, 3401, -5379, 6327, -6343, 4730, 122, 617, -4721, 2618, 1507, 384, -25, 420, -3447, -1301, -3251, 6981, -4263, 2809, -5148, 7052, -3992, 3102, -2884, -91, -3227, 5112, -2735, 2489, 3610, -3925, -4815, 1904, 2516, 338, -1549, 912, -3293, 2870, 3437, -2004, 1869, 1602, -7201, 6919, -8013, 5689, -559, 1777, -5419, 6131, 462, -7879, 5077, 776, 229, -1459, 920, -3375, 3545, -1783, 475, -315, -1364, -3016, 2136, -802, 7165, -454, -6397, 1530, -578, -509, 1329, -1409, 4819, -5773, 1636, -1205, 482, 2717, 1178, 370, -3057, -874, 949, -2519, 5602, 1764, -5571, 3847, 528, 150, -3604, 3239, 220, -1566, -70, -3423, 3458, 3151, -1987, 1274, -3708, 1399, -5630, 5180, -650, 3032, -3169, -788, 759, -3457, 7102, -8073, 1571, 133, -794, 3543, -3415, -1221, 6806, -5943, -516, 3909, -2447, -672, 282, -638, 1407, -1613, 3364, 1669, 1197, -5742, 5520, -4247, 2892, -2672, -1388, 7647, -7580, 381, 4064, 2516, -4135, -2465, 724, 5093, -2003, -1200, -3367, 6826, -151, -2674, 3341, 496, -1707, -2848, -4035, 1148, 6696, -326, -3458, -714, 5346, -2884, 1683, -6034, 4050, 117, 532, -934, -2698, 5994, -8, -1375, -2597, 1268, -1867, -363, -620, 3584, 12, -2709, 3288, 1533, -3003, 2361, -3659, 3803, -6591, 5804, 922, -6595, 4367, -3789, 993, -1951, 6496, -7418, 288, 4904, 441, 1321, -2412, -3133, -796, 4460, 1823, -4100, 856, -2353, 6057, -1102, 755, 970, -5511, 2372, 2907, -4467, -1703, -813, -3, 4081, 2408, -2900, -3978, 6539, -5776, 4145, -1549, -4136, 7809, 213, -3197, -3267, 1247, 703, 4382, -6842, 537, 7182, -4601, 1656, -3828, 4353, -349, -947, -3739, 808, 1958, 3506, -3904, 2404, -4036, 5974, -7525, 4933, -4692, 3361, 143, -3043, 452, 3417, -2936, 2571, 3044, -382, 98, -5695, 2067, -767, 860, -1053, 4502, -3625, -556, -717, -1524, 3408, -4021, 429, 1651, 4553, -340, -2110, -232, -3300, 949, 5055, -6589, 6643, -518, -1006, -3419, 4572, 672, 290, -4239, 3735, 1572, -320, -4964, 3043, -5042, 319, 3049, 1957, -2648, 1235, 68, -465, -4389, -276, 3455, -3291, 3961, 1497, 1077, -630, -2168, 2575, -1778, -201, -2332, -2268, 2221, 379, 1833, 518, -5047, 3820, 2034, -754, -3529, 157, -498, 312, 345, 3868, 2005, -2759, 1348, -1736, -832, 4182, -6873, 7338, -3064, -502, 3078, -4830, -395, -2462, 156, 692, 1521, 3787, 2054, -8360, 266, 5715, -4852, 5104, -5908, 1970, -1226, 3320, -2861, -66, -675, 1078, 3938, -2035, 2805, -2017, 1550, -708, -2868, 3189, -2141, -3518, 990, 565, 6649, -7263, -205, 3186, -1513, 3505, -4136, 3735, -5182, 5593, -3319, -1792, 4552, -2797, 4443, -2394, -1902, 3239, -88, -847, -2360, 2218, 252, -2822, -677, 5527, -4319, -2240, 3831, -3892, 6333, -1065, 1973, -2227, 30, -2330, 13, 2606, -3180, 2611, 2245, -960, -3927, 5959, -3358, -1767, -2211, 3444, 2307, 514, 844, -1206, -3635, -1485, 4458, -1110, -502, -1755, 2833, -5368, -766, 6787, -3530, 1625, -1734, -1510, 3083, 2966, -136, -5312, 623, 4622, -1954, 1858, -6068, 2762, -107, -1202, 2759, -3705, 2685, -1160, -1378, 24, 4721, -1249, -1323, -1823, 5232, -3272, -2901, -601, 221, 6245, -5963, 5897, -232, -3608, -2236, -414, 4277, 3171, -3151, -2590, 1588, -1657, 1084, 4903, -7760, -280, 6896, 980, -4614, -2910, 4477, -1102, 3445, -76, -5301, 6281, -4381, -1116, 1470, -1332, -242, -681, 5884, -6046, 562, 5769, -6477, 3208, -419, 4080, -2390, -175, 91, -1117, -2085, 455, 439, -1673, 3643, -4763, 6735, -3733, -2853, 3893, 3543, -1811, 512, -1746, -5467, 4925, -1629, -1236, -459, 2542, 1906, -2823, 1744, -4255, 6855, -2226, -4403, 6479, -5016, 2972, -2153, 4407, -5139, -376, 4968, -5273, -692, 522, 5166, -726, -2114, -3688, 1513, -1003, 233, 5968, 1021, -5974, 6322, -1433, -7140, 5581, -4826, 7124, -5719, 2455, -2222, -2099, 7159, -2312, 1565, -3177, 5270, -3917, 355, -151, -4400, 5111, -3226, 1009, 4042, 1196, -7736, 1518, -1545, 3782, 21, 1893, -4839, -335, 3855, 3008, -713, -392, -7241, 242, 5381, -1304, 2439, -3001, -155, -878, -2320, 8391}, false},
24+
{[]int{1, -1, 1, -1}, false},
25+
}
26+
}
27+
28+
//Test
29+
func TestCanThreePartsEqualSum(t *testing.T) {
30+
for _, table := range tables {
31+
y := canThreePartsEqualSum(table.x)
32+
if y != table.y {
33+
var tmp []string
34+
for k := range table.x {
35+
tmp = append(tmp, strconv.Itoa(table.x[k]))
36+
}
37+
t.Fatalf("error" + strings.Join(tmp, ","))
38+
}
39+
}
40+
41+
}
42+
43+
//Test
44+
func TestCanThreePartsEqualSum2(t *testing.T) {
45+
for _, table := range tables {
46+
y := canThreePartsEqualSum2(table.x)
47+
if y != table.y {
48+
var tmp []string
49+
for k := range table.x {
50+
tmp = append(tmp, strconv.Itoa(table.x[k]))
51+
}
52+
t.Fatalf("error" + strings.Join(tmp, ","))
53+
}
54+
}
55+
56+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package _6_Permutations
2+
3+
//46. Permutations
4+
5+
var ResRow [][]int
6+
7+
func checkIndex(indexs []int, curret int) bool {
8+
for i := 0; i < curret; i++ {
9+
if indexs[i] == indexs[curret] {
10+
return true
11+
}
12+
}
13+
return false
14+
}
15+
16+
func run(indexs []int, nums []int, current int, len int) {
17+
for i := 0; i < len; i++ {
18+
indexs[current] = nums[i]
19+
if checkIndex(indexs, current) {
20+
continue
21+
}
22+
23+
if current == len-1 {
24+
res_col := make([]int, len)
25+
for i := 0; i < len; i++ {
26+
res_col[i] = indexs[i]
27+
}
28+
ResRow = append(ResRow,res_col)
29+
indexs[current] = -1
30+
return
31+
}
32+
newCurrent := current + 1
33+
run(indexs, nums, newCurrent, len)
34+
indexs[current] = -1
35+
36+
}
37+
}
38+
39+
func permute(nums []int) [][]int {
40+
indexs := make([]int, len(nums))
41+
42+
//init num_indexs
43+
for i := 0; i < len(nums); i++ {
44+
indexs[i] = -1
45+
}
46+
47+
//run
48+
run(indexs, nums, 0, len(nums))
49+
var tmpRow = ResRow
50+
ResRow = [][]int{}
51+
return tmpRow
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package _6_Permutations
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
//46. Permutations
9+
10+
//Test
11+
func TestPermute(t *testing.T) {
12+
answer1 := make([][]int,6)
13+
answer1[0] = []int{1,2,3}
14+
answer1[1] = []int{1,3,2}
15+
answer1[2] = []int{2,1,3}
16+
answer1[3] = []int{2,3,1}
17+
answer1[4] = []int{3,1,2}
18+
answer1[5] = []int{3,2,1}
19+
20+
nums := []int{1, 2, 3}
21+
res := permute(nums)
22+
if !reflect.DeepEqual(res,answer1){
23+
t.Fatalf("error ")
24+
}
25+
26+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* @Author: pzqu
3+
* @Date: 2020-03-18 01:23
4+
*/
5+
package _160_Find_Words_That_Can_Be_Formed_by_Characters
6+
7+
import (
8+
"encoding/json"
9+
)
10+
11+
//优化前
12+
func countCharacters(words []string, chars string) int {
13+
cmap := make(map[uint8]int)
14+
15+
for i := 0; i < len(chars); i++ {
16+
cmap[chars[i]] ++
17+
}
18+
19+
count := 0
20+
tmpMap := make(map[uint8]int)
21+
22+
for i := 0; i < len(words); i++ {
23+
ExtractInto(cmap, &tmpMap)
24+
j := 0
25+
for ; j < len(words[i]); j++ {
26+
if tmpMap[words[i][j]] <= 0 {
27+
break
28+
}
29+
tmpMap[words[i][j]] --
30+
}
31+
if j == len(words[i]) {
32+
count += len(words[i])
33+
}
34+
}
35+
36+
return count
37+
}
38+
39+
/*
40+
把interface类型转换成我们想要的struct类型
41+
这个通用方法可以转换成任意一个想要的类型
42+
*/
43+
func ExtractInto(source interface{}, to interface{}) error {
44+
b, err := json.Marshal(source)
45+
if err != nil {
46+
return err
47+
}
48+
err = json.Unmarshal(b, to)
49+
return err
50+
}
51+
52+
//优化后
53+
func countCharacters2(words []string, chars string) int {
54+
var byteCount [26]int
55+
for _, char := range chars {
56+
byteCount[char-'a']++
57+
}
58+
59+
ret := 0
60+
for _, word := range words {
61+
bc, match := byteCount, true
62+
for _, char := range word {
63+
if bc[char-'a'] <= 0 {
64+
match = false
65+
break
66+
}
67+
bc[char-'a']--
68+
}
69+
if match {
70+
ret += len(word)
71+
}
72+
}
73+
return ret
74+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @Author: pzqu
3+
* @Date: 2020-03-18 01:23
4+
*/
5+
package _160_Find_Words_That_Can_Be_Formed_by_Characters
6+
7+
import (
8+
"strings"
9+
"testing"
10+
)
11+
12+
var tables []struct {
13+
x []string
14+
y string
15+
z int
16+
}
17+
18+
func init() {
19+
tables = []struct {
20+
x []string
21+
y string
22+
z int
23+
}{
24+
{[]string{"cat", "bt", "hat", "tree"}, "atach", 6},
25+
{[]string{"hello", "world", "leetcode"}, "welldonehoneyr", 10},
26+
}
27+
}
28+
29+
//Test
30+
func TestCountCharacters(t *testing.T) {
31+
for _, table := range tables {
32+
z := countCharacters(table.x, table.y)
33+
if z != table.z {
34+
t.Fatalf("error : %v %v %v,but len:%v", strings.Join(table.x, ","), table.y, table.z, z)
35+
}
36+
}
37+
}
38+
39+
//Test
40+
func TestCountCharacters2(t *testing.T) {
41+
for _, table := range tables {
42+
z := countCharacters2(table.x, table.y)
43+
if z != table.z {
44+
t.Fatalf("error : %v %v %d,but len:%d", strings.Join(table.x, ","), table.y, table.z, z)
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)