
目录
208. 实现 Trie (前缀树) Implement-trie-prefix-tree 🌟🌟
209. 长度最小的子数组 Minimum-size-subarray-sum 🌟🌟
🌟 每日一练刷题专栏 🌟
Rust每日一练 专栏
Golang每日一练 专栏
Python每日一练 专栏
C/C++每日一练 专栏
Java每日一练 专栏
208. 实现 Trie (前缀树) Implement-trie-prefix-tree
Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。
请你实现 Trie 类:
- Trie()初始化前缀树对象。
- void insert(String word)向前缀树中插入字符串- word。
- boolean search(String word)如果字符串- word在前缀树中,返回- true(即,在检索之前已经插入);否则,返回- false。
- boolean startsWith(String prefix)如果之前已经插入的字符串- word的前缀之一为- prefix,返回- true;否则,返回- false。
示例:
输入
["Trie", "insert", "search", "search", "startsWith", "insert", "search"]
[[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]]
输出
[null, null, true, false, true, null, true]
解释
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple");   // 返回 True
trie.search("app");     // 返回 False
trie.startsWith("app"); // 返回 True
trie.insert("app");
trie.search("app");     // 返回 True
 
提示:
- 1 <= word.length, prefix.length <= 2000
- word和- prefix仅由小写英文字母组成
- insert、- search和- startsWith调用次数 总计 不超过- 3 * 10^4次
代码:
package main
import "fmt"
type Trie struct {
	isEnd bool
	next  [26]*Trie
}
/** Initialize your data structure here. */
func Constructor() Trie {
	return Trie{}
}
/** Inserts a word into the trie. */
func (this *Trie) Insert(word string) {
	node := this
	for _, ch := range word {
		if node.next[ch-'a'] == nil {
			node.next[ch-'a'] = &Trie{}
		}
		node = node.next[ch-'a']
	}
	node.isEnd = true
}
/** Returns if the word is in the trie. */
func (this *Trie) Search(word string) bool {
	node := this
	for _, ch := range word {
		if node = node.next[ch-'a']; node == nil {
			return false
		}
	}
	return node.isEnd
}
/** Returns if there is any word in the trie that starts with the given prefix. */
func (this *Trie) StartsWith(prefix string) bool {
	node := this
	for _, ch := range prefix {
		if node = node.next[ch-'a']; node == nil {
			return false
		}
	}
	return true
}
func main() {
	trie := Constructor()
	trie.Insert("apple")
	fmt.Println(trie.Search("apple"))   // 返回 true
	fmt.Println(trie.Search("app"))     // 返回 false
	fmt.Println(trie.StartsWith("app")) // 返回 true
	trie.Insert("app")
	fmt.Println(trie.Search("app")) // 返回 true
}
输出:
true
 false
 true
 true
209. 长度最小的子数组 Minimum-size-subarray-sum
给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, ..., numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。
示例 1:
输入:target = 7, nums = [2,3,1,2,4,3] 输出:2 解释:子数组 [4,3] 是该条件下的长度最小的子数组。
示例 2:
输入:target = 4, nums = [1,4,4] 输出:1
示例 3:
输入:target = 11, nums = [1,1,1,1,1,1,1,1] 输出:0
提示:
- 1 <= target <= 10^9
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^5
进阶:
- 如果你已经实现 O(n)时间复杂度的解法, 请尝试设计一个O(n log(n))时间复杂度的解法。
代码:
package main
import "fmt"
func minSubArrayLen(target int, nums []int) int {
	minLen := 1 << 31
	left, right := 0, 0
	sum := 0
	for right < len(nums) {
		sum += nums[right]
		for sum >= target {
			minLen = min(minLen, right-left+1)
			sum -= nums[left]
			left++
		}
		right++
	}
	if minLen == 1<<31 {
		return 0
	}
	return minLen
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func main() {
	nums := []int{2, 3, 1, 2, 4, 3}
	fmt.Println(minSubArrayLen(7, nums))
	nums = []int{1, 4, 4}
	fmt.Println(minSubArrayLen(4, nums))
	nums = []int{1, 1, 1, 1, 1, 1, 1, 1}
	fmt.Println(minSubArrayLen(11, nums))
}
输出:
2
 1
 0
🌟 每日一练刷题专栏 🌟
✨ 持续,努力奋斗做强刷题搬运工!
👍 点赞,你的认可是我坚持的动力!
🌟 收藏,你的青睐是我努力的方向!
✎ 评论,你的意见是我进步的财富!
☸ 主页:https://hannyang.blog.csdn.net/
| 
 | Rust每日一练 专栏(2023.5.16~)更新中... | 
| 
 | Golang每日一练 专栏(2023.3.11~)更新中... | 
| 
 | Python每日一练 专栏(2023.2.18~2023.5.18)暂停更 | 
| 
 | C/C++每日一练 专栏(2023.2.18~2023.5.18)暂停更 | 
| 
 | Java每日一练 专栏(2023.3.11~2023.5.18)暂停更 | 










![[学习笔记] [机器学习] 6. [上]决策树算法(熵Entropy、信息增益(率)、基尼值(指数)、CART剪枝、特征工程特征提取、回归决策树)](https://img-blog.csdnimg.cn/30100d71aad648f691b0236dc779483b.png#pic_center)













