Skip to content

Commit bb4113a

Browse files
authored
Merge pull request strengthen#7 from wuqinqiang/master
Leetcode-PHP
2 parents 9d5daeb + 94b1877 commit bb4113a

File tree

10,211 files changed

+17456
-50000
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

10,211 files changed

+17456
-50000
lines changed

.idea/LeetCode.iml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/codeStyleConfig.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/dictionaries/wuqinqiang.xml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+39
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+10,148
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PHP/0-50/17.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## :pencil2:Leetcode之PHP版题目解析(17. Letter Combinations of a Phone Number)
2+
**2019-06-03 吴亲库里 库里的深夜食堂**
3+
****
4+
### :pencil2:描述
5+
**让我们根据电话号码的数字,组合所有情况的字符串,题目给的是23,对应着九种组合。**
6+
****
7+
### :pencil2:题目实例
8+
<a href="https://github.com/wuqinqiang/">
9+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/images/17.png">
10+
</a>
11+
****
12+
13+
### :pencil2:题目分析
14+
**思想就是把给定的数字一位位拿出来,获取它指定的字符串集,然后和其他位上的数字字符串集和一位位进行组合。**
15+
****
16+
### :pencil2:解法一
17+
```php
18+
/**
19+
* @param String $digits
20+
* @return String[]
21+
*/
22+
function letterCombinations($digits) {
23+
return $this->helper($digits,"");
24+
}
25+
26+
function helper($digits,$char)
27+
{
28+
$array = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"];
29+
$res=[];
30+
$strings=$array[$digits[0]];
31+
$len=strlen($strings);
32+
for($i=0;$i<$len;$i++){
33+
$val=$strings[$i];
34+
if(strlen($digits)==1){ //如果给定只有一个数字的话
35+
array_push($res,$char.$val);
36+
}else{
37+
$moreStrings=$this->helper(substr($digits,1),$char.$val);
38+
foreach($moreStrings as $more){
39+
array_push($res,$more);
40+
}
41+
}
42+
}
43+
return $res;
44+
}
45+
```
46+
****
47+
48+
### 联系
49+
50+
<a href="https://github.com/wuqinqiang/">
51+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg" width="150px" height="150px">
52+
</a>
53+
54+
55+
56+
57+

PHP/0-50/2.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
**2019-05-13 吴亲库里 库里的深夜食堂**
3+
4+
### :pencil2:题目描述
5+
6+
**给定两个非空的链表,它们的位数是按照逆序存储的,并且每个结点存储一个非负整数,将各结点上的两个数字相加返回一个新的链表。**
7+
8+
### :pencil2:题目实例
9+
<a href="https://github.com/wuqinqiang/">
10+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/images/2.png">
11+
</a>
12+
13+
****
14+
15+
### :pencil2:题目分析
16+
**这题还需要注意几个情况就是:1.两个链表的长度不一样的.2.如果某两个结点相加大于10时取个位数,并且进一位。这也就能看出题中最后一个结点值是8,4+6进了一位。这道题可以用递归来解。**
17+
18+
****
19+
20+
### :pencil2:最终实现代码
21+
22+
```php
23+
/**
24+
* Definition for a singly-linked list.
25+
* class ListNode {
26+
* public $val = 0;
27+
* public $next = null;
28+
* function __construct($val) { $this->val = $val; }
29+
* }
30+
*/
31+
class Solution {
32+
33+
private $res=0;
34+
35+
/**
36+
* @param ListNode $l1
37+
* @param ListNode $l2
38+
* @return ListNode
39+
*/
40+
function addTwoNumbers($l1, $l2) {
41+
$node=new ListNode($this->res + $l1->val + $l2->val);
42+
if($this->res=intval($node->val >9)){
43+
$node->val -=10;
44+
}
45+
$node->next=(!$this->res && is_null($l1->next) && is_null($l2->next) )?null:$this->addTwoNumbers($l1->next,$l2->next);
46+
return $node;
47+
}
48+
}
49+
```
50+
****
51+
52+
53+
54+
### 联系
55+
56+
<a href="https://github.com/wuqinqiang/">
57+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg" width="150px" height="150px">
58+
</a>

PHP/0-50/22.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## :pencil2:Leetcode之PHP版题目解析(22. Generate Parentheses)
2+
**2019-06-04 吴亲库里 库里的深夜食堂**
3+
****
4+
### :pencil2:描述
5+
**让我们根据电话号码的数字,组合所有情况的字符串,题目给的是23,对应着九种组合。**
6+
****
7+
### :pencil2:题目实例
8+
<a href="https://github.com/wuqinqiang/">
9+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/images/22.png">
10+
</a>
11+
****
12+
13+
### :pencil2:题目分析
14+
**每一次可以有两种选择加左括号还是加右括号,但是我们需要注意一个重要规则,如果当前右括号的数等于左括号出现的数,那么不能继续再加右括号了,否则肯定没有与他对应的左括号,递归出口呢?也就是左括号和右括号都用完的时候。**
15+
****
16+
### :pencil2:解法一
17+
```php
18+
/**
19+
* @param Integer $n
20+
* @return String[]
21+
*/
22+
function generateParenthesis($n) {
23+
$res=[];
24+
$this->helper($res,"",$n,$n);
25+
return $res;
26+
}
27+
28+
function helper(&$res,$cur,$open,$close){
29+
if($open == 0 && $close ==0) array_push($res,$cur);
30+
if($open>$close) return ;
31+
if($open>0) $this->helper($res,$cur.'(',$open-1,$close);
32+
if($close>0) $this->helper($res,$cur.')',$open,$close-1);
33+
}
34+
```
35+
****
36+
37+
### 联系
38+
39+
<a href="https://github.com/wuqinqiang/">
40+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg" width="150px" height="150px">
41+
</a>
42+
43+
44+
45+
46+

PHP/0-50/3.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
**2019-05-15 吴亲库里 库里的深夜食堂**
3+
4+
### :pencil2:题目描述
5+
6+
**给定一个字符串,让我们求最长无重复的字符子串,从例3中可以看出来,求的是连续最长的。如果不是连续结果就是pwke了。**
7+
8+
### :pencil2:题目实例
9+
<a href="https://github.com/wuqinqiang/">
10+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/images/3.png">
11+
</a>
12+
13+
****
14+
15+
### :pencil2:题目分析
16+
**这里遍历整个字符串,定义一个变量用来存储字符串,定义一个变量存储最大的长度,只要出现重复了,说明此时就是从头到当前字母无重复的最大长度,我们需要把开始计算位置移至它的下一个位置即可。每次保存最大连续长度,直至遍历结束。**
17+
****
18+
19+
### :pencil2:最终实现代码
20+
21+
```php
22+
/**
23+
* @param String $s
24+
* @return Integer
25+
*/
26+
function lengthOfLongestSubstring($s) {
27+
$max = 0;
28+
$current = '';
29+
$len=$i=0;
30+
for (;$i < strlen($s); $i++) {
31+
if (strpos($current, $s[$i]) !== false) {
32+
$current = substr($current, strpos($current, $s[$i])+1);
33+
$len = strlen($current);
34+
}
35+
$current .= $s[$i];
36+
$len++;
37+
$max = max($max, $len);
38+
}
39+
40+
return $max;
41+
}
42+
43+
```
44+
****
45+
46+
47+
48+
### 联系
49+
50+
<a href="https://github.com/wuqinqiang/">
51+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg" width="150px" height="150px">
52+
</a>

PHP/0-50/33.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
**2019-05-08 吴亲库里 库里的深夜食堂**
3+
4+
### :pencil2:题目描述
5+
6+
**一个有序数组在不确定的位置进行了旋转,让我们 来求旋转后给定值在当前数组的下标位置,如果没有返回-1.**
7+
8+
9+
### :pencil2:题目实例
10+
<a href="https://github.com/wuqinqiang/">
11+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/images/33.png">
12+
</a>
13+
14+
****
15+
16+
### :pencil2:题目分析
17+
**这道题因为本身的数组是有序的,只是在哪个点上发生了旋转的操作,这就造成了数组一半是有序的,我们取数组的中间数做判断,如果中间数小于数组的右半边,说明中间到右半边这一块是有序的,否则左半边就是有序的,然后根据给定值以及确定的半边有序区域的首尾的位置,继续缩小搜索的位置。**
18+
****
19+
20+
### :pencil2:最终实现代码
21+
22+
```php
23+
/**
24+
* @param Integer[] $nums
25+
* @param Integer $target
26+
* @return Integer
27+
*/
28+
function search($nums, $target) {
29+
$l=0;
30+
$r=count($nums)-1;
31+
while($l<=$r){
32+
$middle=$l+(($r-$l)>>1);
33+
if($nums[$middle]===$target) return $middle;
34+
else if($nums[$middle]<$nums[$r]){
35+
if($nums[$middle]<$target && $nums[$r]>=$target) $l=$middle+1;
36+
else $r=$middle-1;
37+
}else{
38+
if($nums[$l]<=$target && $nums[$middle]>$target) $r=$middle-1;
39+
else $l=$middle+1;
40+
}
41+
}
42+
return -1;
43+
}
44+
45+
```
46+
****
47+
48+
### 联系
49+
50+
<a href="https://github.com/wuqinqiang/">
51+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg" width="150px" height="150px">
52+
</a>

0 commit comments

Comments
 (0)