- 实现了Node和Trie类的基本结构 - 添加了insert方法用于插入单词 - 添加了search方法用于搜索完整单词 - 添加了startsWith方法用于前缀匹配 - 实现了字典树的完整功能逻辑 fix(compiler): 修复变量声明和数组访问的代码生成问题 - 为int、float、bool类型变量添加默认初始化值0 - 修复数组维度解析中的括号处理逻辑 - 解决赋值表达式上下文中的数组访问问题 example(prime): 添加素数计算性能测试示例 - 实现了NthPrime函数计算第n个素数 - 添加了时间测量功能 - 提供了30000个素数计算的性能测试用例pull/1/head
parent
75a9309801
commit
8fd83ae8b0
3 changed files with 125 additions and 4 deletions
@ -0,0 +1,40 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"strconv" |
||||||
|
"time" |
||||||
|
) |
||||||
|
|
||||||
|
func NthPrime(n int) int { |
||||||
|
|
||||||
|
var ( |
||||||
|
i int = 2 |
||||||
|
j int = 1 |
||||||
|
) |
||||||
|
|
||||||
|
for { |
||||||
|
j = j + 1; |
||||||
|
if j > i/j { |
||||||
|
n-- |
||||||
|
if n == 0 { |
||||||
|
break |
||||||
|
} |
||||||
|
j = 1 |
||||||
|
} |
||||||
|
if i%j == 0 { |
||||||
|
i++; |
||||||
|
j = 1; |
||||||
|
} |
||||||
|
} |
||||||
|
return i |
||||||
|
} |
||||||
|
|
||||||
|
func main() { |
||||||
|
s := time.Now().UnixNano() / 1e6 |
||||||
|
n := 300000 |
||||||
|
result := NthPrime(n) |
||||||
|
e := time.Now().UnixNano() / 1e6 |
||||||
|
time := e - s |
||||||
|
println("第" + strconv.Itoa(n) + "个素数的值是:" + strconv.Itoa(result) + " 耗时" + strconv.Itoa(int(time)) + "毫秒") |
||||||
|
} |
||||||
|
//第300000个素数的值是:4256233 耗时10417毫秒
|
||||||
@ -0,0 +1,75 @@ |
|||||||
|
--TEST-- |
||||||
|
mixed/2.phpt |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
class Node |
||||||
|
{ |
||||||
|
public $is_end = false; |
||||||
|
public $child = []; |
||||||
|
} |
||||||
|
|
||||||
|
class Trie |
||||||
|
{ |
||||||
|
private $root = null; |
||||||
|
|
||||||
|
function __construct() |
||||||
|
{ |
||||||
|
$this->root = new Node(); |
||||||
|
} |
||||||
|
|
||||||
|
function insert($word) |
||||||
|
{ |
||||||
|
$node = $this->root; |
||||||
|
|
||||||
|
for ($i = 0; $i < strlen($word); $i++) { |
||||||
|
if (!isset($node->child[$word[$i]])) { |
||||||
|
$node->child[$word[$i]] = new Node(); |
||||||
|
} |
||||||
|
|
||||||
|
$node = $node->child[$word[$i]]; |
||||||
|
if ($i == strlen($word) - 1) { |
||||||
|
$node->is_end = true; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function search($word) |
||||||
|
{ |
||||||
|
$node = $this->root; |
||||||
|
for ($i = 0; $i < strlen($word); $i++) { |
||||||
|
if (isset($node->child[$word[$i]])) { |
||||||
|
$node = $node->child[$word[$i]]; |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return $node->is_end; |
||||||
|
} |
||||||
|
|
||||||
|
function startsWith($prefix) |
||||||
|
{ |
||||||
|
$node = $this->root; |
||||||
|
for ($i = 0; $i < strlen($prefix); $i++) { |
||||||
|
if (isset($node->child[$prefix[$i]])) { |
||||||
|
$node = $node->child[$prefix[$i]]; |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function main() |
||||||
|
{ |
||||||
|
$Trie = new Trie(); |
||||||
|
$Trie->insert('apple'); |
||||||
|
var_dump($Trie->search("apple")); |
||||||
|
var_dump($Trie->startsWith("apps")); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
bool(true) |
||||||
|
bool(false) |
||||||
Loading…
Reference in new issue