forked from yezihack/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package main | ||
//Trie树,一种多叉树, 也称前缀树,字典树. |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package _1_贪心思想 | ||
|
||
import ( | ||
"strconv" | ||
) | ||
|
||
//问题描述: 给你一个整数 n,使得从 n 中删除 k 个数字之后的数字最大。 | ||
|
||
//如果 1432219 整数, 删除3个数, 得到4329最大值 | ||
|
||
//使用贪心算法求得,时间复杂度O(n*m) | ||
func GetMaxNumber(n, count int) int { | ||
//先将数字转整形数组 | ||
s := strconv.Itoa(int(n)) | ||
b := make([]int, len(s)) | ||
for k, v := range s {//转换成数字数组 | ||
b[k], _ = strconv.Atoi(string(v)) | ||
} | ||
for i := 0; i < count; i ++ { | ||
min := b[0] | ||
k := 0 | ||
for key, v := range b { | ||
if v < min {//查找最小值 | ||
min = v//最小值 | ||
k = key //记录下标 | ||
} | ||
} | ||
if k >= 0 { | ||
b = append(b[:k], b[k+1:]...)//最小值从下标中删除掉 | ||
} | ||
} | ||
//数字数组转换成整数 | ||
result := "" | ||
for i := 0; i < len(b); i++ { | ||
result += strconv.Itoa(b[i]) | ||
} | ||
v, _ := strconv.Atoi(result) | ||
return v | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package _1_贪心思想 | ||
|
||
import ( | ||
"github.com/yezihack/algo/00.src" | ||
"testing" | ||
) | ||
|
||
func TestGetMaxNumber(t *testing.T) { | ||
src.Asset(1, t, 4329, GetMaxNumber(1432219, 3)) | ||
src.Asset(2, t, 439, GetMaxNumber(1432219, 4)) | ||
src.Asset(3, t, 49, GetMaxNumber(1432219, 5)) | ||
src.Asset(4, t, 21, GetMaxNumber(111121, 4)) | ||
} |