mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
@ -224,6 +224,31 @@ func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer[] $nums
|
||||
* @param Integer $target
|
||||
* @return Integer[]
|
||||
*/
|
||||
function twoSum($nums, $target) {
|
||||
if (count($nums) == 0) {
|
||||
return [];
|
||||
}
|
||||
$table = [];
|
||||
for ($i = 0; $i < count($nums); $i++) {
|
||||
$temp = $target - $nums[$i];
|
||||
if (isset($table[$temp])) {
|
||||
return [$table[$temp], $i];
|
||||
}
|
||||
$table[$nums[$i]] = $i;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
|
@ -393,6 +393,46 @@ function threeSum(array $nums): array
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer[] $nums
|
||||
* @return Integer[][]
|
||||
*/
|
||||
function threeSum($nums) {
|
||||
$res = [];
|
||||
sort($nums);
|
||||
for ($i = 0; $i < count($nums); $i++) {
|
||||
if ($nums[$i] > 0) {
|
||||
return $res;
|
||||
}
|
||||
if ($i > 0 && $nums[$i] == $nums[$i - 1]) {
|
||||
continue;
|
||||
}
|
||||
$left = $i + 1;
|
||||
$right = count($nums) - 1;
|
||||
while ($left < $right) {
|
||||
$sum = $nums[$i] + $nums[$left] + $nums[$right];
|
||||
if ($sum < 0) {
|
||||
$left++;
|
||||
}
|
||||
else if ($sum > 0) {
|
||||
$right--;
|
||||
}
|
||||
else {
|
||||
$res[] = [$nums[$i], $nums[$left], $nums[$right]];
|
||||
while ($left < $right && $nums[$left] == $nums[$left + 1]) $left++;
|
||||
while ($left < $right && $nums[$right] == $nums[$right - 1]) $right--;
|
||||
$left++;
|
||||
$right--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
@ -310,6 +310,49 @@ var fourSum = function(nums, target) {
|
||||
};
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer[] $nums
|
||||
* @param Integer $target
|
||||
* @return Integer[][]
|
||||
*/
|
||||
function fourSum($nums, $target) {
|
||||
$res = [];
|
||||
sort($nums);
|
||||
for ($i = 0; $i < count($nums); $i++) {
|
||||
if ($i > 0 && $nums[$i] == $nums[$i - 1]) {
|
||||
continue;
|
||||
}
|
||||
for ($j = $i + 1; $j < count($nums); $j++) {
|
||||
if ($j > $i + 1 && $nums[$j] == $nums[$j - 1]) {
|
||||
continue;
|
||||
}
|
||||
$left = $j + 1;
|
||||
$right = count($nums) - 1;
|
||||
while ($left < $right) {
|
||||
$sum = $nums[$i] + $nums[$j] + $nums[$left] + $nums[$right];
|
||||
if ($sum < $target) {
|
||||
$left++;
|
||||
}
|
||||
else if ($sum > $target) {
|
||||
$right--;
|
||||
}
|
||||
else {
|
||||
$res[] = [$nums[$i], $nums[$j], $nums[$left], $nums[$right]];
|
||||
while ($left < $right && $nums[$left] == $nums[$left+1]) $left++;
|
||||
while ($left < $right && $nums[$right] == $nums[$right-1]) $right--;
|
||||
$left++;
|
||||
$right--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
|
@ -223,6 +223,37 @@ func isHappy(_ n: Int) -> Bool {
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer $n
|
||||
* @return Boolean
|
||||
*/
|
||||
function isHappy($n) {
|
||||
// use a set to record sum
|
||||
// whenever there is a duplicated, stop
|
||||
// == 1 return true, else false
|
||||
$table = [];
|
||||
while ($n != 1 && !isset($table[$n])) {
|
||||
$table[$n] = 1;
|
||||
$n = self::getNextN($n);
|
||||
}
|
||||
return $n == 1;
|
||||
}
|
||||
|
||||
function getNextN(int $n) {
|
||||
$res = 0;
|
||||
while ($n > 0) {
|
||||
$temp = $n % 10;
|
||||
$res += $temp * $temp;
|
||||
$n = floor($n / 10);
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
|
@ -331,8 +331,32 @@ func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
PHP:
|
||||
```php
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* type ListNode struct {
|
||||
* Val int
|
||||
* Next *ListNode
|
||||
* }
|
||||
*/
|
||||
// 虚拟头+双指针
|
||||
func removeElements(head *ListNode, val int) *ListNode {
|
||||
dummyHead := &ListNode{}
|
||||
dummyHead.Next = head
|
||||
pred := dummyHead
|
||||
cur := head
|
||||
for cur != nil {
|
||||
if cur.Val == val {
|
||||
pred.Next = cur.Next
|
||||
} else {
|
||||
pred = cur
|
||||
}
|
||||
cur = cur.Next
|
||||
}
|
||||
return dummyHead.Next
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
|
@ -221,6 +221,41 @@ func isAnagram(_ s: String, _ t: String) -> Bool {
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param String $s
|
||||
* @param String $t
|
||||
* @return Boolean
|
||||
*/
|
||||
function isAnagram($s, $t) {
|
||||
if (strlen($s) != strlen($t)) {
|
||||
return false;
|
||||
}
|
||||
$table = [];
|
||||
for ($i = 0; $i < strlen($s); $i++) {
|
||||
if (!isset($table[$s[$i]])) {
|
||||
$table[$s[$i]] = 1;
|
||||
} else {
|
||||
$table[$s[$i]]++;
|
||||
}
|
||||
if (!isset($table[$t[$i]])) {
|
||||
$table[$t[$i]] = -1;
|
||||
} else {
|
||||
$table[$t[$i]]--;
|
||||
}
|
||||
}
|
||||
foreach ($table as $record) {
|
||||
if ($record != 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 相关题目
|
||||
|
||||
* 383.赎金信
|
||||
|
@ -209,6 +209,35 @@ func intersection(_ nums1: [Int], _ nums2: [Int]) -> [Int] {
|
||||
}
|
||||
```
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer[] $nums1
|
||||
* @param Integer[] $nums2
|
||||
* @return Integer[]
|
||||
*/
|
||||
function intersection($nums1, $nums2) {
|
||||
if (count($nums1) == 0 || count($nums2) == 0) {
|
||||
return [];
|
||||
}
|
||||
$counts = [];
|
||||
$res = [];
|
||||
foreach ($nums1 as $num) {
|
||||
$counts[$num] = 1;
|
||||
}
|
||||
foreach ($nums2 as $num) {
|
||||
if (isset($counts[$num])) {
|
||||
$res[] = $num;
|
||||
}
|
||||
unset($counts[$num]);
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 相关题目
|
||||
|
||||
* 350.两个数组的交集 II
|
||||
|
@ -266,6 +266,32 @@ var canConstruct = function(ransomNote, magazine) {
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param String $ransomNote
|
||||
* @param String $magazine
|
||||
* @return Boolean
|
||||
*/
|
||||
function canConstruct($ransomNote, $magazine) {
|
||||
if (count($ransomNote) > count($magazine)) {
|
||||
return false;
|
||||
}
|
||||
$map = [];
|
||||
for ($i = 0; $i < strlen($magazine); $i++) {
|
||||
$map[$magazine[$i]] = ($map[$magazine[$i]] ?? 0) + 1;
|
||||
}
|
||||
for ($i = 0; $i < strlen($ransomNote); $i++) {
|
||||
if (!isset($map[$ransomNote[$i]]) || --$map[$ransomNote[$i]] < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
```
|
||||
|
||||
Swift:
|
||||
```swift
|
||||
func canConstruct(_ ransomNote: String, _ magazine: String) -> Bool {
|
||||
|
@ -220,6 +220,40 @@ var fourSumCount = function(nums1, nums2, nums3, nums4) {
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
PHP:
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
* @param Integer[] $nums1
|
||||
* @param Integer[] $nums2
|
||||
* @param Integer[] $nums3
|
||||
* @param Integer[] $nums4
|
||||
* @return Integer
|
||||
*/
|
||||
function fourSumCount($nums1, $nums2, $nums3, $nums4) {
|
||||
$map = [];
|
||||
foreach ($nums1 as $n1) {
|
||||
foreach ($nums2 as $n2) {
|
||||
$temp = $n1 + $n2;
|
||||
$map[$temp] = isset($map[$temp]) ? $map[$temp]+1 : 1;
|
||||
}
|
||||
}
|
||||
$count = 0;
|
||||
foreach ($nums3 as $n3) {
|
||||
foreach ($nums4 as $n4) {
|
||||
$temp = 0 - $n3 - $n4;
|
||||
if (isset($map[$temp])) {
|
||||
$count += $map[$temp];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $count;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Swift:
|
||||
```swift
|
||||
func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int]) -> Int {
|
||||
@ -248,6 +282,7 @@ func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int]
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
|
Reference in New Issue
Block a user