最近做的算法题,比较适合新手入门。
1. 最长公共前缀
查找字符串数组中的最长公共前缀。如果不存在,返回空字符串。
示例:输入:strs = [“flower”,“float”,“flight”]。 输出:“fl”。
思路
这道题有多种解法,我这里借助Trie字典树。将这些字符串存储到Trie树中。那么Trie树的第一个分叉口之前的单分支树的就是所求。
字典树,又称单词查找树,Trie 树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。
字典树有3个基本性质:
- 根节点不包含字符,除根节点外每一个节点都只包含一个字符。
- 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
- 每个节点的所有子节点包含的字符都不相同。
/**
* @brief 字典树
* @author jichenghan
* @since 2021-09-15
*/
class Solution {
/**
* @param String[] $strs
* @return String
*/
function longestCommonPrefix($strs) {
$trie = new Trie();
foreach($strs as $str)
{
//特殊处理,若有空字符串
if($str == '')
{
return "";
}
$trie->addStr($str);
}
return $trie->searchCommonPrefix();
}
}
//字典树
class Trie {
private $head;
public function __construct()
{
//初始化根节点
$this->head = new Node();
}
//插入字符串
public function addStr($str)
{
$len = strlen($str);
for($i = 0; $i < $len; $i++)
{
//字符串结束标记
$is_end = $i == $len - 1 ? true : false;
if($i == 0)
{ //第一个插在根节点下
$node = $this->head->addChild($str[$i], $is_end);
}
else
{ //插在返回的父节点下
$node = $node->addChild($str[$i], $is_end);
}
}
}
//查询最长公共前缀
public function searchCommonPrefix()
{
$str = '';
$node = $this->head;
//判断子节点数目是否为1
while(count($node->children) == 1)
{
//最短字符串
if($node->is_end === true)
{
break;
}
$str .= $node->value;
$node = $node->children[0];
}
//别忘了把子节点加上
$str .= $node->value;
return $str;
}
}
//节点类
class Node {
//值
public $value;
//子节点对象数组
public $children = [];
//字符串结束标记,可扩展获取Trie数所有字符串或查找字串是否存在
public $is_end = false;
public function __construct($value = '')
{
$this->value = $value;
}
//添加子节点
public function addChild($value, $is_end)
{
$child = $this->searchChildren($value);
if($child === false)
{
//不存在就新建
$child = new Node($value);
$this->setChild($child);
}
//添加结束标记,注意只能设true
if($is_end === true)
{
$child->is_end = $is_end;
}
return $child;
}
//查找子节点中是否有该值
private function searchChildren($value)
{
foreach($this->children as $child)
{
if($child->value == $value)
{
return $child;
}
}
return false;
}
//设置子节点
private function setChild($child)
{
$this->children[] = $child;
}
}
相对这道题目效率不高,这里主要是模拟实现字典树,你们有兴趣可扩展联想搜索、排序的方法。
2. 下一个更大的元素
找出一个数组元素在另一个数组中下一个更大的元素。
给你两个 没有重复元素 的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集。
找出 nums1 中每个元素在 nums2 中的下一个比其大的值。
nums1 中数字 x 的下一个更大元素是指 x 在 nums2 中对应位置的 右边的第一个 比 x 大的元素。如果不存在,对应位置输出 -1 。
示例: 给定数组 nums1 = [2, 6, 7],nums1 = [4, 2, 8, 6, 5, 7, 1, 3 ],
返回数组为:[ 8, 7, -1]
思路
先定义一个没有找到下一个更大数的队列 list,和一个匹配完成的队列 arr,遍历 nums2 把从第一个数开始放入list,并从第二个数和 list 中所有元素比较,如果比之大就将作为键值对放入 arr,最后遍历 nums1,从 arr 中找出对应值,没有就返回 -1。
class Solution {
/**
* @param Integer[] $nums1
* @param Integer[] $nums2
* @return Integer[]
*/
function nextGreaterElement($nums1, $nums2) {
$list = [];//未配对队列
$arr = [];//匹配结果
foreach($nums2 as $key => $item)
{
if($key == 0)
{
$list[] = $item;
}
else
{
foreach($list as $i => $val)
{
//后面的数比前面大就取出
if($item > $val)
{
$arr[$val] = $item;
unset($list[$i]);
}
}
array_push($list, $item);
}
}
$result = [];
foreach($nums1 as $key => $item)
{
if(isset($arr[$item]))
{
$result[] = $arr[$item];
}
else
{
$result[] = -1;
}
}
return $result;
}
}
这样 list 里只会存在比较大的数,相对会快一些
3. 回文数
给你一个整数 x ,如果 x 是一个回文整数,返回 true ;否则,返回 false 。
回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
示例: 输入12321,输出 true。输入12345,输出 false。
思路
转化成数组,对应位比较。
class Solution {
/**
* @param Integer $x
* @return Boolean
*/
function isPalindrome($x) {
$arr = str_split($x);
$len = count($arr);
for($i = 0;$i< ceil($len/2); $i++)
{
if($arr[$i] != $arr[$len - $i - 1])
{
return false;
}
}
return true;
}
}
题目来源:力扣(LeetCode)