GitHub

# 3.18

### Two Sum

##### 尝试解法
``````class Solution
{

/**
* @param Integer[] \$nums
* @param Integer \$target
* @return Integer[]
*/
function twoSum(\$nums, \$target)
{
if (!isset(\$nums[1])) return;
foreach (\$nums as \$key => \$num) {
unset(\$nums[\$key]);
\$result = array_search(\$target - \$num, \$nums);
if (\$result !== false) return [\$key, \$result];
}
}
}``````
##### 目前最优解法
``````class Solution {

function twoSum(\$nums, \$target) {
\$numsCopy = \$nums;
sort(\$numsCopy);
\$num = count(\$numsCopy);
\$low = 0;
\$high = \$num-1;

while(\$high > \$low){
\$sum = \$numsCopy[\$low] + \$numsCopy[\$high];
if(\$sum == \$target){
\$a = \$numsCopy[\$low];
\$b = \$numsCopy[\$high];
break;
}else if(\$sum > \$target){
\$high--;
}else{
\$low++;
}
}
for (\$i = 0; \$i < \$num; \$i++){
if(\$a == \$nums[\$i]){
\$keyLow = \$i;
break;
}
}

for (\$i = \$num-1; \$i >= 0; \$i--){
if(\$b == \$nums[\$i]){
\$keyHigh = \$i;
break;
}
}
return array(\$keyLow,\$keyHigh);
}
}``````

### Reverse Integer

##### 尝试解法
``````class Solution
{

/**
* @param Integer \$x
* @return Integer
*/
function reverse(\$x) {
\$result = 0;
while (abs(\$x) > 9) {
\$num = \$x % 10;
\$x = (int)(\$x / 10);
\$result = \$result * 10 + \$num;
}
\$result = \$result * 10 + \$x;
if (strlen(base_convert(\$result, 10, 2)) > 31) return 0;
return \$result;
}
}``````

# 3.19

### Palindrome Number

##### 尝试解法
``````class Solution
{

/**
* @param Integer \$x
* @return Boolean
*/
function isPalindrome(\$x)
{
if (\$x < 0) return false;
if (\$x % 10 === 0 && \$x != 0) return false;
\$reverse = 0;
while (\$x > \$reverse) {
\$reverse = (int)(\$reverse * 10 + \$x % 10);
\$x = (int)(\$x / 10);
}
return \$x === \$reverse || \$x === (int)(\$reverse / 10);
}
}``````

# 3.20

### Roman to Integer

##### 尝试解法
``````class Solution
{

/**
* @param String \$s
* @return Integer
*/
function romanToInt(\$s)
{
\$romanMap = [
1 => 'I',
5 => 'V',
10 => 'X',
50 => 'L',
100 => 'C',
500 => 'D',
1000 => 'M',
];

\$nums = [];
\$s = str_split(\$s, 1);

foreach (\$s as \$key => \$item) {
array_push(\$nums, array_search(\$item, \$romanMap));
}

\$num = 0;
for (\$i = 0; \$i < count(\$nums) - 1; \$i++) {
if (\$nums[\$i + 1] > \$nums[\$i]) {
\$num += \$nums[\$i + 1] - \$nums[\$i];
\$i++;
} else {
\$num += \$nums[\$i];
}
}

if (isset(\$nums[\$i])) {
return \$num + \$nums[\$i];
}

return \$num;
}
}
``````

# 3.21

### Longest Common Prefix

##### 尝试解法
``````class Solution

{

/**
* @param String[] \$strs
* @return String
*/
function longestCommonPrefix(\$strs)
{
usort(\$strs, function (\$current, \$next) {
return strlen(\$current) > strlen(\$next);
});
\$result = '';
\$temp = '';
foreach (str_split(\$strs[0], 1) as \$str) {
\$temp .= \$str;
for (\$i = 1; \$i < count(\$strs); \$i++) {
if (strpos(\$strs[\$i], \$temp) !== 0) break 2;
}
\$result .= \$str;
}
return \$result;
}
}``````

# 3.22

### Valid Parentheses

##### 尝试解法
``````class Solution
{
/**
* @param String \$s
* @return Boolean
*/
function isValid(\$s)
{
if (empty(\$s)) return true;
\$s = str_split(\$s);
if (count(\$s) % 2 !== 0) return false;
\$leftMap = [
')' => '(',
'}' => '{',
']' => '['
];
\$leftStack = [];
while (1) {
if (in_array(current(\$s), \$leftMap)) {
array_push(\$leftStack, current(\$s));
} else {
if (array_pop(\$leftStack) !== \$leftMap[current(\$s)]) {
return false;
}
}
if(!next(\$s)) break;
}
return \$leftStack === [];
}
}``````

# 3.23

### Merge two sorted lists

##### 尝试解法
``````class Solution
{
/**
* @param ListNode \$l1
* @param ListNode \$l2
* @return ListNode
*/
function mergeTwoLists(\$l1, \$l2)
{
if (!\$l1 || !\$l2) return \$l1 ?? \$l2;
if (\$l1->val < \$l2->val) {
\$list = \$l1;
\$list->next = \$this->mergeTwoLists(\$l1->next, \$l2);
} else {
\$list = \$l2;
\$list->next = \$this->mergeTwoLists(\$l1, \$l2->next);
}
return \$list;
}
}``````

# 3.24

### Remove Duplicates from Sorted Array

##### 尝试解法
``````class Solution
{
/**
* @param Integer[] \$nums
* @return Integer
*/
function removeDuplicates(&\$nums)
{
\$i = 0;
for (\$j = 1; \$j < count(\$nums); \$j++) {
if (\$nums[\$j] != \$nums[\$i]) {
\$i++;
\$nums[\$i] = \$nums[\$j];
}
}
return \$i + 1;
}
}``````

# 3.25

### Remove Element

##### 尝试解法
``````class Solution
{
/**
* @param Integer[] \$nums
* @param Integer \$val
* @return Integer
*/
function removeElement(&\$nums, \$val)
{
\$i = 0;
for (\$j = 0; \$j < count(\$nums); \$j++) {
if (\$nums[\$j] !== \$val) {
\$nums[\$i] = \$nums[\$j];
\$i++;
}
}
return \$i;
}
}``````

# 3.26

### Implement strstr

##### 尝试解法
``````class Solution
{
/**
* @param String \$haystack
* @param String \$needle
* @return Integer
*/
function strStr(\$haystack, \$needle)
{
if (!\$needle) {
return 0;
}
if (strlen(\$needle) > strlen(\$haystack)) {
return -1;
}
if (strlen(\$needle) === strlen(\$haystack)) {
for (\$i = 0; \$i < strlen(\$needle); \$i++) {
if (\$needle[\$i] != \$haystack[\$i]) {
return -1;
}
}
return 0;
}
\$i = 0;
for (\$j = 0; \$j < strlen(\$haystack) - strlen(\$needle) + 1; \$j++) {
if (\$haystack[\$j] === \$needle[\$i]) {
\$i += 1;
while (\$i < strlen(\$needle)) {
if (\$needle[\$i] !== \$haystack[\$j + \$i]) {
\$i = 0;
break;
}
\$i += 1;
}
if (\$i === strlen(\$needle)) {
return \$j;
}
}
}
return -1;
}
}``````

# 3.27

### Search Insert Position

##### 尝试解法
``````class Solution
{
/**
* @param Integer[] \$nums
* @param Integer \$target
* @return Integer
*/
function searchInsert(\$nums, \$target)
{
if (in_array(\$target, \$nums)) {
return array_search(\$target, \$nums);
}

if (current(\$nums) > \$target) {
array_unshift(\$nums, \$target);
return 0;
}

if (end(\$nums) < \$target) {
array_push(\$nums, \$target);
return count(\$nums) - 1;
}

array_push(\$nums, \$target);
sort(\$nums);
return array_search(\$target, \$nums);
}
}``````

# 3.29

### Length of Last Word

##### 尝试解法
``````class Solution
{
/**
* @param String \$s
* @return Integer
*/
function lengthOfLastWord(\$s)
{
\$s = strrev(trim(\$s));
\$length = 0;
for (\$i = 0; \$i < strlen(\$s); \$i++) {
if (\$s[\$i] === ' ') {
return \$length;
}
\$length++;
}
return \$length;
}
}

class Solution
{
function lengthOfLastWord(\$s)
{
\$s = explode(' ', trim(\$s));
return strlen(end(\$s));
}
}``````

# 3.30

### Plus One

##### 尝试解法
``````class Solution
{
/**
* @param Integer[] \$digits
* @return Integer[]
*/
function plusOne(\$digits)
{
for (\$i = array_key_last(\$digits); \$i > -1; \$i--) {
if (\$digits[\$i] === 9) {
\$digits[\$i] = 0;
continue;
}
if (\$digits[\$i] < 9) {
\$digits[\$i] = \$digits[\$i] + 1;
return \$digits;
}
}
array_unshift(\$digits, 1);
return \$digits;
}
}``````

# 4.2

##### 尝试解法
``````class Solution
{
/**
* @param String \$a
* @param String \$b
* @return String
*/
{
\$result = '';

if (strlen(\$a) > strlen(\$b)) {
} else {
}

for (\$i = 0; \$i < strlen(\$a); \$i++) {
\$result .= \$a[\$i] + \$b[\$i];
}

\$result = strrev(\$result);

for (\$i = 0; \$i < strlen(\$result); \$i++) {
if (\$result[\$i] < 2) {
continue;
}

\$result[\$i] = \$result[\$i] - 2;
if (isset(\$result[\$i + 1])) {
\$result[\$i + 1] = \$result[\$i + 1] + 1;
continue;
}

\$result .= 1;
}

return strrev(\$result);
}
}``````

# 4.3

### Combine Two Tables

``````select FirstName, LastName, City, State

# 4.10

### Single Number

``````class Solution
{

/**
* @param Integer[] \$nums
* @return Integer
*/
function singleNumber(\$nums)
{
\$result = 0;
foreach (\$nums as \$num) {
\$result ^= \$num;
}

return \$result;
}
}``````

Hello。

《L04 微信小程序从零到发布》

《L05 电商实战》

``````class Solution {

function twoSum(\$nums, \$target) {
\$numsCopy = \$nums;
sort(\$numsCopy);
\$num = count(\$numsCopy);
\$low = 0;
\$high = \$num-1;

while(\$high > \$low){
\$sum = \$numsCopy[\$low] + \$numsCopy[\$high];
if(\$sum == \$target){
\$a = \$numsCopy[\$low];
\$b = \$numsCopy[\$high];
break;
}else if(\$sum > \$target){
\$high--;
}else{
\$low++;
}
}
for (\$i = 0; \$i < \$num; \$i++){
if(\$a == \$nums[\$i]){
\$keyLow = \$i;
break;
}
}

for (\$i = \$num-1; \$i >= 0; \$i--){
if(\$b == \$nums[\$i]){
\$keyHigh = \$i;
break;
}
}
return array(\$keyLow,\$keyHigh);
}
}``````

4个月前 评论

@Egfly 很巧妙！

4个月前 评论

``````public function twoSum(\$nums, \$target)
{
\$outArr = [];
foreach (\$nums as \$key => \$num) {
\$num1 = \$target - \$num;
foreach (\$nums as \$k => \$n) {
if (\$k != \$key) {
if (\$num1 == \$n) {
\$outArr = [
\$key,
\$k,
];
if (!empty(\$outArr)) {
return \$outArr;
}
}
}
}
}
return \$outArr;
}``````
4个月前 评论

@Ali 我大一的时候，学 `c` 的时候，考试就用这种暴力算法 ！ :cow: :beer:

4个月前 评论
``````class Solution {

/**
* @param Integer[] \$nums
* @param Integer \$target
* @return Integer[]
*/
function twoSum(\$nums, \$target) {
foreach (\$nums as \$k=>\$v){
\$t=\$target-\$v;
if(in_array(\$t,\$nums)){
if(\$k!=array_keys(\$nums,\$t)[0]) {
\$arr = [array_keys(\$nums, \$t)[0], \$k];
}
}
}
return \$arr;
}
}``````
3个月前 评论

@Ali O(n2) 时间复杂度啊

3个月前 评论

10

15

138

76