Skip to content

第二周作业#6 #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
49 changes: 49 additions & 0 deletions Week_01/id_6/LeetCode_189_6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php


class Solution {

/**
* @param Integer[] $nums
* @param Integer $k
* @return NULL
*/
function rotate(&$nums, $k)
{
if ($k == 0) return;
$len = count($nums);
$k %= $len;

$this->reverse($nums, 0, $len - 1);
$this->reverse($nums, 0, $k - 1);
$this->reverse($nums, $k, $len - 1);

return;
}

/**
* revert an array
*
* @param $nums
* @param $start
* @param $end
*/
private function reverse(&$nums, $start, $end)
{
while ($start < $end) {
$tmp = $nums[$start];
$nums[$start] = $nums[$end];
$nums[$end] = $tmp;

$start ++;
$end --;
}

return;
}
}

$nums = [1,2,3,4,5,6,7];
$sol = new Solution();
$sol->rotate2($nums, 3);
var_export($nums);
54 changes: 54 additions & 0 deletions Week_01/id_6/LeetCode_21_6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php


/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val) { $this->val = $val; }
* }
*/
class Solution {

/**
* @param ListNode $l1
* @param ListNode $l2
* @return ListNode
*/
function mergeTwoLists($l1, $l2)
{
if (null == $l1) return $l2;
if (null == $l2) return $l1;

$list = new ListNode(0);
$cur = $list;
while (null != $l1 && null != $l2) {
if ($l1->val < $l2->val) {
$cur->next = $l1;
$cur = $cur->next;
$l1 = $l1->next;
} else {
$cur->next = $l2;
$cur = $cur->next;
$l2 = $l2->next;
}
}

if ($l1 == null) {
$cur->next = $l2;
}

if ($l2 == null) {
$cur->next = $l1;
}

return $list->next;
}
}

class ListNode {
public $val = 0;
public $next = null;
function __construct($val) { $this->val = $val; }
}
29 changes: 29 additions & 0 deletions Week_01/id_6/LeetCode_26_6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php


class Solution {

/**
* @param Integer[] $nums
* @return Integer
*/
function removeDuplicates(&$nums)
{
$flag = 1;
$num = $nums[0];
for ($i = 1; $i < count($nums); $i ++) {
if ($nums[$i] == $num) {
unset($nums[$i]);
} else {
$num = $nums[$i];
$flag++;
}
}

return $flag;
}
}

$nums = [0,0,1,1,1,2,2,3,3,4];
$sol = new Solution();
echo $sol->removeDuplicates($nums);
51 changes: 51 additions & 0 deletions Week_01/id_6/LeetCode_88_6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

class Solution
{

/**
* @param Integer[] $nums1
* @param Integer $m
* @param Integer[] $nums2
* @param Integer $n
* @return NULL
*/
function merge(&$nums1, $m, $nums2, $n)
{
$len = $m + $n;
while ($m > 0 && $n > 0) {
if ($nums1[$m - 1] > $nums2[$n - 1]) {
$nums1[$len - 1] = $nums1[$m - 1];
$m --;
} else {
$nums1[$len - 1] = $nums2[$n - 1];
$n --;
}
$len --;
}

while ($n > 0) {
$nums1[$len - 1] = $nums2[$n - 1];
$n --;
$len --;
}

return;
}
}

$nums1 = [2,0];
$m = 1;

$nums2 = [1];
$n = 1;
/*
$nums1 = [1,2,3,0,0,0];
$m = 3;

$nums2 = [2,5,6];
$n = 3;*/

$sol = new Solution();
$sol->merge($nums1, $m, $nums2, $n);
var_export($nums1);
6 changes: 6 additions & 0 deletions Week_01/id_6/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# 学习笔记

主要练习了数组和链表的习题,每道题都会根据所学思考不同的算法解法。数组和链表树所有数据结构和算法的基础,用数组和链表去实现其他的数据结构,如堆、栈、树等等,手写一遍代码 ,会更加加深自己对于其中数据结构和算法的掌握。

在平时的工作之中,自己会刻意地去想有没有更好、更优化的解决方案,这个业务的时间复杂度是多少、空间复杂度是多少,有没有更加优化的解决方案,可以减少内存占用或者服务器资源占用,自己会更多地去思考这方面的问题,这对于自己的平时工作也是一个很的提升。

本周联系的题目不算多,但是自己对于每一个题目的思考有很多,不只是每道题目的解法算法,还有不同的思路,自己选择的算法与数据结构,开拓了自己从前没有思考过的思维,并且能够把这种思维应用到实际的业务和场景中,这是自己最大的收获。
Binary file not shown.
37 changes: 37 additions & 0 deletions Week_02/id_6/LeetCode_101_6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution {

/**
* @param TreeNode $root
* @return Boolean
*/
function isSymmetric($root)
{
$this->isSymmetricRec($root, $root);
}

/**
* @param TreeNode $left_root
* @param TreeNode $right_root
* @return Boolean
*/
function isSymmetricRec($left_root, $right_root)
{
if ($left_root == null && $right_root == null) return true;
if ($left_root == null || $right_root == null) return false;
if ($left_root->val == $right_root->val) {
return $this->isSymmetricRec($left_root->left, $right_root->right) && $this->isSymmetricRec($left_root->right, $right_root->left);
}
return false;
}
}
41 changes: 41 additions & 0 deletions Week_02/id_6/LeetCode_102_6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution {

public $levels = [];

/**
* @param TreeNode $root
* @return Integer[][]
*/
function levelOrder($root)
{
if ($root == null) return [];
// BFS
$this->bfs($root, 0);
return $this->levels;

}

function bfs($root, $level)
{
$this->levels[$level][] = $root->val;

if ($root->left != null) {
$this->bfs($root->left, $level + 1);
}

if ($root->right != null) {
$this->bfs($root->right, $level + 1);
}
}
}
44 changes: 44 additions & 0 deletions Week_02/id_6/LeetCode_103_6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution {

public $levels = [];

/**
* @param TreeNode $root
* @return Integer[][]
*/
function zigzagLevelOrder($root) {
if ($root == null) return [];
// BFS
$this->bfs($root, 0);
return $this->levels;
}

function bfs($root, $level)
{
if ($level % 2 != 0) {
if (!isset($this->levels[$level])) $this->levels[$level] = [];
array_unshift($this->levels[$level], $root->val);
} else {
$this->levels[$level][] = $root->val;
}

if ($root->left != null) {
$this->bfs($root->left, $level + 1);
}

if ($root->right != null) {
$this->bfs($root->right, $level + 1);
}
}
}
30 changes: 30 additions & 0 deletions Week_02/id_6/LeetCode_3_6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

class Solution {

/**
* @param String $s
* @return Integer
*/
function lengthOfLongestSubstring($s) {
$hash = [];
$len = strlen($s);
$count = $i = $j = 0;
while ($i < $len && $j < $len) {
if (!isset($hash[$s[$j]])) {
$hash[$s[$j]] = 1;
$j ++;
$count = max($count, $j - $i);
} else {
unset($hash[$s[$i]]);
$i ++;
}
}

return $count;
}
}

$sol = new Solution();
$s = "abcabcbb";
var_dump($sol->lengthOfLongestSubstring($s));
32 changes: 32 additions & 0 deletions Week_02/id_6/LeetCode_938_6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution {

/**
* @param TreeNode $root
* @param Integer $L
* @param Integer $R
* @return Integer
*/
function rangeSumBST($root, $L, $R)
{
if ($root == null) return $this->sum;
if ($root->val > $R) {
return $this->rangeSumBST($root->left, $L, $R);
}
if ($root->val < $L) {
return $this->rangeSumBST($root->right, $L, $R);
}
return $root->val + $this->rangeSumBST($root->left, $L, $R) + $this->rangeSumBST($root->right, $L, $R);
}

}
Loading