Skip to content

Commit d26075a

Browse files
Merge pull request #102 from HugoWen/week2
第二周作业#6
2 parents 5953018 + 3d87adc commit d26075a

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

Week_02/id_6/LeetCode_101_6.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* public $val = null;
7+
* public $left = null;
8+
* public $right = null;
9+
* function __construct($value) { $this->val = $value; }
10+
* }
11+
*/
12+
class Solution {
13+
14+
/**
15+
* @param TreeNode $root
16+
* @return Boolean
17+
*/
18+
function isSymmetric($root)
19+
{
20+
$this->isSymmetricRec($root, $root);
21+
}
22+
23+
/**
24+
* @param TreeNode $left_root
25+
* @param TreeNode $right_root
26+
* @return Boolean
27+
*/
28+
function isSymmetricRec($left_root, $right_root)
29+
{
30+
if ($left_root == null && $right_root == null) return true;
31+
if ($left_root == null || $right_root == null) return false;
32+
if ($left_root->val == $right_root->val) {
33+
return $this->isSymmetricRec($left_root->left, $right_root->right) && $this->isSymmetricRec($left_root->right, $right_root->left);
34+
}
35+
return false;
36+
}
37+
}

Week_02/id_6/LeetCode_102_6.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* public $val = null;
7+
* public $left = null;
8+
* public $right = null;
9+
* function __construct($value) { $this->val = $value; }
10+
* }
11+
*/
12+
class Solution {
13+
14+
public $levels = [];
15+
16+
/**
17+
* @param TreeNode $root
18+
* @return Integer[][]
19+
*/
20+
function levelOrder($root)
21+
{
22+
if ($root == null) return [];
23+
// BFS
24+
$this->bfs($root, 0);
25+
return $this->levels;
26+
27+
}
28+
29+
function bfs($root, $level)
30+
{
31+
$this->levels[$level][] = $root->val;
32+
33+
if ($root->left != null) {
34+
$this->bfs($root->left, $level + 1);
35+
}
36+
37+
if ($root->right != null) {
38+
$this->bfs($root->right, $level + 1);
39+
}
40+
}
41+
}

Week_02/id_6/LeetCode_103_6.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* public $val = null;
7+
* public $left = null;
8+
* public $right = null;
9+
* function __construct($value) { $this->val = $value; }
10+
* }
11+
*/
12+
class Solution {
13+
14+
public $levels = [];
15+
16+
/**
17+
* @param TreeNode $root
18+
* @return Integer[][]
19+
*/
20+
function zigzagLevelOrder($root) {
21+
if ($root == null) return [];
22+
// BFS
23+
$this->bfs($root, 0);
24+
return $this->levels;
25+
}
26+
27+
function bfs($root, $level)
28+
{
29+
if ($level % 2 != 0) {
30+
if (!isset($this->levels[$level])) $this->levels[$level] = [];
31+
array_unshift($this->levels[$level], $root->val);
32+
} else {
33+
$this->levels[$level][] = $root->val;
34+
}
35+
36+
if ($root->left != null) {
37+
$this->bfs($root->left, $level + 1);
38+
}
39+
40+
if ($root->right != null) {
41+
$this->bfs($root->right, $level + 1);
42+
}
43+
}
44+
}

Week_02/id_6/LeetCode_3_6.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
class Solution {
4+
5+
/**
6+
* @param String $s
7+
* @return Integer
8+
*/
9+
function lengthOfLongestSubstring($s) {
10+
$hash = [];
11+
$len = strlen($s);
12+
$count = $i = $j = 0;
13+
while ($i < $len && $j < $len) {
14+
if (!isset($hash[$s[$j]])) {
15+
$hash[$s[$j]] = 1;
16+
$j ++;
17+
$count = max($count, $j - $i);
18+
} else {
19+
unset($hash[$s[$i]]);
20+
$i ++;
21+
}
22+
}
23+
24+
return $count;
25+
}
26+
}
27+
28+
$sol = new Solution();
29+
$s = "abcabcbb";
30+
var_dump($sol->lengthOfLongestSubstring($s));

Week_02/id_6/LeetCode_938_6.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* public $val = null;
7+
* public $left = null;
8+
* public $right = null;
9+
* function __construct($value) { $this->val = $value; }
10+
* }
11+
*/
12+
class Solution {
13+
14+
/**
15+
* @param TreeNode $root
16+
* @param Integer $L
17+
* @param Integer $R
18+
* @return Integer
19+
*/
20+
function rangeSumBST($root, $L, $R)
21+
{
22+
if ($root == null) return $this->sum;
23+
if ($root->val > $R) {
24+
return $this->rangeSumBST($root->left, $L, $R);
25+
}
26+
if ($root->val < $L) {
27+
return $this->rangeSumBST($root->right, $L, $R);
28+
}
29+
return $root->val + $this->rangeSumBST($root->left, $L, $R) + $this->rangeSumBST($root->right, $L, $R);
30+
}
31+
32+
}

Week_02/id_6/LeetCode_98_6.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* class TreeNode {
6+
* public $val = null;
7+
* public $left = null;
8+
* public $right = null;
9+
* function __construct($value) { $this->val = $value; }
10+
* }
11+
*/
12+
class Solution {
13+
14+
public $flag = PHP_INT_MIN;
15+
16+
/**
17+
* @param TreeNode $root
18+
* @return Boolean
19+
*/
20+
function isValidBST($root) {
21+
if ($root == null) return true;
22+
if ($this->isValidBST($root->left)) {
23+
if ($this->flag > $root->val) return false;
24+
$this->flag = $root->val;
25+
return $this->isValidBST($root->right);
26+
}
27+
}
28+
}

Week_02/id_6/NOTE.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
11
# 学习笔记
2+
3+
这一周主要是hash表、二叉树、二叉搜索树的练习。难度选择了在中等难度以上的题目进行练习。每道题目都会先自己思考,多多少少都会有一些想法和思路,但是在具体代码实现细节上面还有些欠缺,大部分题目还需要看过别人的讨论才有具体的实现思路。经过自己实现之后,对于同类型的问题做起来会更加有思路些,还是需要更多地练习。
4+
5+
Hash表的特点是使用hash函数构建键值对应关系,插入和查找的时间复杂度是O(1),可以应用到字符串搜索、匹配、数据缓存等场景下。LeetCode第3题构建使用hash表,一次循环即可找到最长子字符串,也是一种空间换时间的解决问题思路。
6+
7+
更多的对于二叉树和二叉搜索树进行了练习。主要的解题思路是递归,从做题中发现技巧是对于前序、中序、后序遍历以及BFS、DFS的合理运用,可以解决很多问题,至少可以提供一种解题思路。通过做题也对树的上面的遍历方式的具体实现代码有了更熟练的运用。对于二叉搜索树的解题思路,更多运用到二叉搜索树特性:1.左<根<右;2.每一节点都是二叉搜索树;3.中序遍历是递增数列。应用这三点特性,遇到二叉搜索树的问题,还是能提供不少的解题想法的。
8+
9+
还需要更多地做题练习来打开自己的解题思维。

0 commit comments

Comments
 (0)