Skip to content

Commit 1265600

Browse files
committed
first submit
1 parent 6d38b11 commit 1265600

9 files changed

+523
-2
lines changed

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# phpstorm project files
2+
.idea
3+
4+
# netbeans project files
5+
nbproject
6+
7+
# zend studio for eclipse project files
8+
.buildpath
9+
.project
10+
.settings
11+
12+
# windows thumbnail cache
13+
Thumbs.db
14+
15+
# composer vendor dir
16+
/vendor
17+
18+
# composer itself is not needed
19+
composer.phar
20+
21+
# Mac DS_Store Files
22+
.DS_Store
23+
24+
# phpunit itself is not needed
25+
phpunit.phar
26+
# local phpunit config
27+
/phpunit.xml
28+
29+
tests/_output/*
30+
tests/_support/_generated
31+
32+
#vagrant folder
33+
/.vagrant

L13_RomanToInteger.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
4+
5+
字符 数值
6+
I 1
7+
V 5
8+
X 10
9+
L 50
10+
C 100
11+
D 500
12+
M 1000
13+
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做  XXVII, 即为 XX + V + II 。
14+
15+
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
16+
17+
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
18+
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。 
19+
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
20+
给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
21+
22+
示例 1:
23+
24+
输入: "III"
25+
输出: 3
26+
示例 2:
27+
28+
输入: "IV"
29+
输出: 4
30+
示例 3:
31+
32+
输入: "IX"
33+
输出: 9
34+
示例 4:
35+
36+
输入: "LVIII"
37+
输出: 58
38+
解释: L = 50, V= 5, III = 3.
39+
40+
来源:力扣(LeetCode)
41+
链接:https://leetcode-cn.com/problems/roman-to-integer
42+
*/
43+
44+
class L13_RomanToInteger
45+
{
46+
static public $map = [
47+
'I' => 1,
48+
'V' => 5,
49+
'X' => 10,
50+
'L' => 50,
51+
'C' => 100,
52+
'D' => 500,
53+
'M' => 1000,
54+
];
55+
56+
static public function romanToInt($s) {
57+
$sArr = str_split($s);
58+
$n = 0;
59+
foreach($sArr as $k => $v){
60+
61+
if(isset(self::$map[$v])){
62+
$next = isset($sArr[$k + 1]) ? $sArr[$k + 1] : false;
63+
if($v == 'I' && $next && in_array($next,['V','X'])){
64+
$n -= self::$map[$v];
65+
}elseif($v == 'X' && $next && in_array($next,['L','C'])){
66+
$n -= self::$map[$v];
67+
}elseif($v == 'C' && $next && in_array($next,['D','M'])){
68+
$n -= self::$map[$v];
69+
}else{
70+
$n += self::$map[$v];
71+
}
72+
}else{
73+
return false;
74+
}
75+
}
76+
return $n;
77+
}
78+
}
79+
80+
$s = 'IV';
81+
echo L13_RomanToInteger::romanToInt($s);

L14_LongestCommonPrefix.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
编写一个函数来查找字符串数组中的最长公共前缀。
4+
5+
如果不存在公共前缀,返回空字符串 ""。
6+
7+
示例 1:
8+
9+
输入: ["flower","flow","flight"]
10+
输出: "fl"
11+
示例 2:
12+
13+
输入: ["dog","racecar","car"]
14+
输出: ""
15+
解释: 输入不存在公共前缀。
16+
说明:
17+
18+
所有输入只包含小写字母 a-z 。
19+
20+
来源:力扣(LeetCode)
21+
链接:https://leetcode-cn.com/problems/longest-common-prefix
22+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
23+
*/
24+
25+
class L14_LongestCommonPrefix
26+
{
27+
function longestCommonPrefix($strs) {
28+
$commonPrefix = '';
29+
$newCommonPrefix = '';
30+
31+
//取第一个字符串作为基准
32+
$firstStr = $strs[0];
33+
if(empty($firstStr)){
34+
return '';
35+
}
36+
//第一个字符串转数组
37+
$firstStrArr = str_split($firstStr);
38+
//去掉第一个
39+
unset($strs[0]);
40+
foreach($firstStrArr as $k => $firstStrWord){
41+
//第一个字符串逐位拼接前缀
42+
$newCommonPrefix .= $firstStrWord;
43+
44+
foreach($strs as $str){
45+
if($newCommonPrefix != substr($str,0,$k + 1)){
46+
return $commonPrefix;
47+
}
48+
}
49+
//通过上述循环即说明前缀有效
50+
$commonPrefix = $newCommonPrefix;
51+
}
52+
return $commonPrefix;
53+
}
54+
}
55+
56+
$strs = ['fff','ff1'];
57+
$m = new L14_LongestCommonPrefix();
58+
echo $m->longestCommonPrefix($strs);

L1_TwoSum.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
4+
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
5+
6+
示例:
7+
给定 nums = [2, 7, 11, 15], target = 9
8+
因为 nums[0] + nums[1] = 2 + 7 = 9
9+
所以返回 [0, 1]
10+
来源:力扣(LeetCode)
11+
链接:https://leetcode-cn.com/problems/two-sum
12+
*/
13+
14+
class L1_TwoSum
15+
{
16+
static public function twoSum($nums,$target){
17+
$count = count($nums);
18+
for($i = 0;$i < $count - 1;$i ++){
19+
$initJ = $i + 1;
20+
for($j = $initJ;$j < $count;$j ++ ){
21+
if($target == ($nums[$i] + $nums[$j])){
22+
return [$i,$j];
23+
}
24+
}
25+
}
26+
return [null,null];
27+
}
28+
}
29+
30+
$nums = [2, 7, 11, 15];
31+
$target = 9;
32+
print_r(L1_TwoSum::twoSum($nums,$target));
33+
34+

L20_ValidParentheses.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。
4+
5+
有效字符串需满足:
6+
7+
左括号必须用相同类型的右括号闭合。
8+
左括号必须以正确的顺序闭合。
9+
注意空字符串可被认为是有效字符串。
10+
11+
示例 1:
12+
13+
输入: "()"
14+
输出: true
15+
示例 2:
16+
17+
输入: "()[]{}"
18+
输出: true
19+
示例 3:
20+
21+
输入: "(]"
22+
输出: false
23+
示例 4:
24+
25+
输入: "([)]"
26+
输出: false
27+
示例 5:
28+
29+
输入: "{[]}"
30+
输出: true
31+
32+
来源:力扣(LeetCode)
33+
链接:https://leetcode-cn.com/problems/valid-parentheses
34+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
35+
*/
36+
37+
class L20_ValidParentheses
38+
{
39+
public $left = ['(' => 1,'{' => 2,'[' => 3];
40+
public $right = [')' => 1,'}' => 2,']' => 3];
41+
42+
function isValid($s) {
43+
$sArr = str_split($s);
44+
$leftSet = array();
45+
$leftKey = array_keys($this->left);
46+
foreach($sArr as $k => $v){
47+
//逐个
48+
//如果是左括号
49+
if(in_array($v,$leftKey)){
50+
//压入括号值进入左集合头部
51+
array(array_unshift($leftSet,$this->left[$v]));
52+
}else{
53+
//如果是右括号
54+
//如果第一个就不对
55+
if($k == 0){
56+
if(in_array($v,array_keys($this->right))){
57+
return false;
58+
}
59+
return true;
60+
}
61+
//无法匹配
62+
if($this->right[$v] != $leftSet[0]){
63+
return false;
64+
}else{
65+
//成功匹配一对括号,推走左集合头部
66+
array_shift($leftSet);
67+
}
68+
}
69+
}
70+
71+
//左括号未清除完毕
72+
if($leftSet){
73+
return false;
74+
}
75+
return true;
76+
}
77+
}
78+
79+
$s = '{]';
80+
$m = new L20_ValidParentheses();
81+
var_dump($m->isValid($s));

0 commit comments

Comments
 (0)