Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 05bd70e

Browse files
committed
Add solution 118.
1 parent c97719e commit 05bd70e

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public $val;
5+
public $left;
6+
public $right;
7+
public $next;
8+
9+
@param Integer $val
10+
@param Node $left
11+
@param Node $right
12+
@param Node $next
13+
function __construct($val, $left, $right, $next) {
14+
$this->val = $val;
15+
$this->left = $left;
16+
$this->right = $right;
17+
$this->next = $next;
18+
}
19+
}
20+
*/
21+
class Solution {
22+
23+
/**
24+
* @param Node $root
25+
* @return Node
26+
*/
27+
function connect($root) {
28+
if ($root == null)
29+
{
30+
return null;
31+
}
32+
33+
$start = $root;
34+
35+
while ($start != null)
36+
{
37+
$temp = $start;
38+
$prev = new Node(-1, null, null, null);
39+
$start = null;
40+
41+
while ($temp != null)
42+
{
43+
if ($start == null)
44+
{
45+
$start = $temp->left == null ? $temp->right : $temp->left;
46+
}
47+
48+
if ($temp->left != null)
49+
{
50+
$prev->next = $temp->left;
51+
$prev = $temp->left;
52+
}
53+
54+
if ($temp->right != null)
55+
{
56+
$prev->next = $temp->right;
57+
$prev = $temp->right;
58+
}
59+
60+
$temp = $temp->next;
61+
}
62+
}
63+
64+
return $root;
65+
}
66+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
3+
/**
4+
* @param Integer $numRows
5+
* @return Integer[][]
6+
*/
7+
function generate($numRows) {
8+
$ans=[];
9+
if($numRows==0)
10+
return $ans;
11+
$t1=[];
12+
13+
array_push($t1, 1);
14+
$count=0;
15+
while($count!=$numRows) {
16+
array_push($ans, $t1);
17+
$t2=[];
18+
array_push($t2, 1);
19+
for($i=1;$i<count($t1);$i++)
20+
array_push($t2, $t1[$i-1]+$t1[$i]);
21+
array_push($t2, 1);
22+
23+
$t1=$t2;
24+
$count++;
25+
}
26+
return $ans;
27+
}
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /