Skip to content

Commit e76154f

Browse files
committed
206,217,237,242
1 parent c3ea9e9 commit e76154f

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

scripts/L206_ReverseLinkedList.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
反转一个单链表。
4+
5+
示例:
6+
7+
输入: 1->2->3->4->5->NULL
8+
输出: 5->4->3->2->1->NULL
9+
进阶:
10+
你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
11+
12+
来源:力扣(LeetCode)
13+
链接:https://leetcode-cn.com/problems/reverse-linked-list
14+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
15+
*/
16+
17+
namespace app\scripts;
18+
require_once '../autoload.php';
19+
use app\lib\ToolUtil;
20+
class L206_ReverseLinkedList
21+
{
22+
/**
23+
* @param ListNode $head
24+
* @return ListNode
25+
*/
26+
function reverseList($head) {
27+
$current = $head;
28+
$prev = null;
29+
while($current){
30+
$temp = $current->next;
31+
$current->next = $prev;
32+
$prev = $current;
33+
$current = $temp;
34+
}
35+
return $prev;
36+
}
37+
}
38+
39+
$nums = [1,2,3,4];
40+
$head = ToolUtil::initListNode($nums);
41+
$m = new L206_ReverseLinkedList();
42+
print_r($m->reverseList($head));

scripts/L217_ContainsDuplicate.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
给定一个整数数组,判断是否存在重复元素。
4+
5+
如果任意一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false 。
6+
7+
示例 1:
8+
9+
输入: [1,2,3,1]
10+
输出: true
11+
示例 2:
12+
13+
输入: [1,2,3,4]
14+
输出: false
15+
示例 3:
16+
17+
输入: [1,1,1,3,3,4,3,2,4,2]
18+
输出: true
19+
20+
来源:力扣(LeetCode)
21+
链接:https://leetcode-cn.com/problems/contains-duplicate
22+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
23+
*/
24+
25+
class L217_ContainsDuplicate
26+
{
27+
/**
28+
* @param Integer[] $nums
29+
* @return Boolean
30+
*/
31+
function containsDuplicate($nums) {
32+
if(array_unique($nums) == $nums){
33+
return false;
34+
}
35+
return true;
36+
}
37+
}
38+
39+
$nums = [1,2,3,1];
40+
$m = new L217_ContainsDuplicate();
41+
var_dump($m->containsDuplicate($nums));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: linyz
5+
* Date: 2020/6/6
6+
* Time: 17:02
7+
*/
8+
namespace app\scripts;
9+
require_once '../autoload.php';
10+
use app\lib\ToolUtil;
11+
class L237_DeleteNodeInALinkedList
12+
{
13+
/**
14+
* @param ListNode $node
15+
* @return
16+
*/
17+
function deleteNode($node) {
18+
$node->val = $node->next->val;
19+
$node->next = $node->next->next;
20+
}
21+
}
22+
23+
$nums = [1,2,3,4];
24+
$head = ToolUtil::initListNode($nums);
25+
$m = new L237_DeleteNodeInALinkedList();
26+
$m->deleteNode($head);
27+

scripts/L242_ValidAnagram.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
4+
5+
示例 1:
6+
7+
输入: s = "anagram", t = "nagaram"
8+
输出: true
9+
示例 2:
10+
11+
输入: s = "rat", t = "car"
12+
输出: false
13+
说明:
14+
你可以假设字符串只包含小写字母。
15+
16+
进阶:
17+
如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?
18+
19+
来源:力扣(LeetCode)
20+
链接:https://leetcode-cn.com/problems/valid-anagram
21+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
22+
*/
23+
24+
class L242_ValidAnagram
25+
{
26+
/**
27+
* @param String $s
28+
* @param String $t
29+
* @return Boolean
30+
*/
31+
function isAnagram($s, $t) {
32+
$s = str_split($s);
33+
$t = str_split($t);
34+
35+
sort($s);
36+
sort($t);
37+
38+
if($s == $t){
39+
return true;
40+
}
41+
return false;
42+
}
43+
}
44+
45+
$s = 'abc';
46+
$t = 'cba';
47+
48+
$m = new L242_ValidAnagram();
49+
$m->isAnagram($s,$t);

0 commit comments

Comments
 (0)