Цена за 48 часов в ленте | 450,00 |
Цена за 1 час закрепления | N/A |
Взаимопиар | Нет |
Дополнительные условия рекламы | Отсутствуют |
Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
class Solution {
function deleteNode($node) {
$node->val = $node->next->val;
$node->next = $node->next->next;
}
}
Input
["RandomizedCollection", "insert", "insert", "insert", "getRandom", "remove", "getRandom"]
[[], [1], [1], [2], [], [1], []]
Output
[null, true, false, true, 2, true, 1]
class RandomizedCollection {
private $dict;
private $list;
function __construct() {
$this->dict = [];
$this->list = [];
}
function insert($val) {
$exists = isset($this->dict[$val]);
if (!$exists) {
$this->dict[$val] = [];
}
$this->dict[$val][] = count($this->list);
$this->list[] = $val;
return !$exists;
}
function remove($val) {
if (!isset($this->dict[$val]) || count($this->dict[$val]) === 0) {
return false;
}
$index = array_pop($this->dict[$val]);
$lastElement = array_pop($this->list);
if ($index < count($this->list)) {
$this->list[$index] = $lastElement;
$this->dict[$lastElement][array_search(count($this->list), $this->dict[$lastElement])] = $index;
}
if (count($this->dict[$val]) === 0) {
unset($this->dict[$val]);
}
return true;
}
function getRandom() {
return $this->list[array_rand($this->list)];
}
}
Input: nestedList = [1,[4,[6]]]
Output: 27
Explanation: One 1 at depth 1, one 4 at depth 2, and one 6 at depth 3. 1*1 + 4*2 + 6*3 = 27.
class Solution {
function depthSum($nestedList) {
return $this->dfs($nestedList, 1);
}
private function dfs($list, $depth) {
$total = 0;
foreach ($list as $nested) {
if ($nested->isInteger()) {
$total += $nested->getInteger() * $depth;
} else {
$total += $this->dfs($nested->getList(), $depth + 1);
}
}
return $total;
}
}
Input
["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
[[], [1], [2], [2], [], [1], [2], []]
Output
[null, true, false, true, 2, true, false, 2]
class RandomizedSet {
private $dict;
private $list;
function __construct() {
$this->dict = [];
$this->list = [];
}
function insert($val) {
if (array_key_exists($val, $this->dict)) {
return false;
}
$this->dict[$val] = count($this->list);
$this->list[] = $val;
return true;
}
function remove($val) {
if (!array_key_exists($val, $this->dict)) {
return false;
}
$index = $this->dict[$val];
$lastElement = array_pop($this->list);
if ($index < count($this->list)) {
$this->list[$index] = $lastElement;
$this->dict[$lastElement] = $index;
}
unset($this->dict[$val]);
return true;
}
function getRandom() {
return $this->list[array_rand($this->list)];
}
}
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.
class Solution {
private $ans;
function __construct() {
$this->ans = null;
}
private function recurseTree($currentNode, $p, $q) {
if ($currentNode === null) {
return false;
}
$left = $this->recurseTree($currentNode->left, $p, $q) ? 1 : 0;
$right = $this->recurseTree($currentNode->right, $p, $q) ? 1 : 0;
$mid = ($currentNode === $p || $currentNode === $q) ? 1 : 0;
if ($mid + $left + $right >= 2) {
$this->ans = $currentNode;
}
return ($mid + $left + $right > 0);
}
function lowestCommonAncestor($root, $p, $q) {
$this->recurseTree($root, $p, $q);
return $this->ans;
}
}
Input
["PhoneDirectory", "get", "get", "check", "get", "check", "release", "check"]
[[3], [], [], [2], [], [2], [2], [2]]
Output
[null, 0, 1, true, 2, false, null, true]
class PhoneDirectory {
private $isSlotAvailable;
function __construct($maxNumbers) {
$this->isSlotAvailable = array_fill(0, $maxNumbers, true);
}
function get() {
for ($i = 0; $i < count($this->isSlotAvailable); $i++) {
if ($this->isSlotAvailable[$i]) {
$this->isSlotAvailable[$i] = false;
return $i;
}
}
return -1;
}
function check($number) {
return $this->isSlotAvailable[$number];
}
function release($number) {
$this->isSlotAvailable[$number] = true;
}
}
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes 2 and 8 is 6.
class Solution {
function lowestCommonAncestor($root, $p, $q) {
$parentVal = $root->val;
$pVal = $p->val;
$qVal = $q->val;
if ($pVal > $parentVal && $qVal > $parentVal) {
return $this->lowestCommonAncestor($root->right, $p, $q);
} else if ($pVal < $parentVal && $qVal < $parentVal) {
return $this->lowestCommonAncestor($root->left, $p, $q);
} else {
return $root;
}
}
}
Input: matrix = [[-5]], k = 1
Output: -5
class Solution {
private function dfs($word, $length, &$visited, $dictionary) {
if ($length == strlen($word)) {
return true;
}
if ($visited[$length]) {
return false;
}
$visited[$length] = true;
for ($i = strlen($word) - ($length == 0 ? 1 : 0); $i > $length; $i--) {
if (isset($dictionary[substr($word, $length, $i - $length)]) &&
$this->dfs($word, $i, $visited, $dictionary)) {
return true;
}
}
return false;
}
function findAllConcatenatedWordsInADict($words) {
$dictionary = array_flip($words);
$answer = [];
foreach ($words as $word) {
$visited = array_fill(0, strlen($word), false);
if ($this->dfs($word, 0, $visited, $dictionary)) {
$answer[] = $word;
}
}
return $answer;
}
}
$solution = new Solution();
$words = ["cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"];
print_r($solution->findAllConcatenatedWordsInADict($words));
Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]
Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".
class Solution {
private function dfs($word, $length, &$visited, $dictionary) {
if ($length == strlen($word)) {
return true;
}
if ($visited[$length]) {
return false;
}
$visited[$length] = true;
for ($i = strlen($word) - ($length == 0 ? 1 : 0); $i > $length; $i--) {
if (isset($dictionary[substr($word, $length, $i - $length)]) &&
$this->dfs($word, $i, $visited, $dictionary)) {
return true;
}
}
return false;
}
function findAllConcatenatedWordsInADict($words) {
$dictionary = array_flip($words);
$answer = [];
foreach ($words as $word) {
$visited = array_fill(0, strlen($word), false);
if ($this->dfs($word, 0, $visited, $dictionary)) {
$answer[] = $word;
}
}
return $answer;
}
}
// Example usage
$solution = new Solution();
$words = ["cat", "cats", "catsdogcats", "dog", "dogcatsdog", "hippopotamuses", "rat", "ratcatdogcat"];
print_r($solution->findAllConcatenatedWordsInADict($words));
Input: n = 1
Output: [2]
class Solution {
public function rand10() {
do {
$row = rand7();
$col = rand7();
$idx = $col + ($row - 1) * 7;
} while ($idx > 40);
return 1 + ($idx - 1) % 10;
}
}
Input: n = 5
Output: [0,1,1,2,1,2]
Explanation:
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101
class Solution {
function countBits($num) {
$ans = array_fill(0, $num + 1, 0);
for ($x = 1; $x <= $num; $x++) {
$ans[$x] = $ans[$x & ($x - 1)] + 1;
}
return $ans;
}
}
Input: queryIP = "172.16.254.1"
Output: "IPv4"
Explanation: This is a valid IPv4 address, return "IPv4".
class Solution {
public function validateIPv4($IP) {
$nums = explode('.', $IP);
if (count($nums) != 4) return "Neither";
foreach ($nums as $x) {
if (strlen($x) == 0 || strlen($x) > 3) return "Neither";
if ($x[0] == '0' && strlen($x) != 1) return "Neither";
if (!ctype_digit($x)) return "Neither";
if (intval($x) > 255) return "Neither";
}
return "IPv4";
}
public function validateIPv6($IP) {
$nums = explode(':', $IP);
$hexdigits = "0123456789abcdefABCDEF";
if (count($nums) != 8) return "Neither";
foreach ($nums as $x) {
if (strlen($x) == 0 || strlen($x) > 4) return "Neither";
for ($i = 0; $i < strlen($x); $i++) {
if (strpos($hexdigits, $x[$i]) === false) return "Neither";
}
}
return "IPv6";
}
public function validIPAddress($IP) {
if (substr_count($IP, '.') == 3) {
return $this->validateIPv4($IP);
} elseif (substr_count($IP, ':') == 7) {
return $this->validateIPv6($IP);
} else {
return "Neither";
}
}
}
Input: root = [3,4,5,1,3,null,1]
Output: 9
Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.
class TreeNode {
public $val;
public $left;
public $right;
function __construct($val = 0, $left = null, $right = null) {
$this->val = $val;
$this->left = $left;
$this->right = $right;
}
}
class Solution {
function rob($root) {
$answer = $this->helper($root);
return max($answer[0], $answer[1]);
}
function helper($node) {
if ($node === null) {
return [0, 0];
}
$left = $this->helper($node->left);
$right = $this->helper($node->right);
$rob = $node->val + $left[1] + $right[1];
$notRob = max($left[0], $left[1]) + max($right[0], $right[1]);
return [$rob, $notRob];
}
}
Input: transactions = [[0,1,10],[2,0,5]]
Output: 2
class Solution {
private $creditList;
function minTransfers($transactions) {
$creditMap = [];
foreach ($transactions as $t) {
if (!isset($creditMap[$t[0]])) $creditMap[$t[0]] = 0;
if (!isset($creditMap[$t[1]])) $creditMap[$t[1]] = 0;
$creditMap[$t[0]] += $t[2];
$creditMap[$t[1]] -= $t[2];
}
$this->creditList = [];
foreach ($creditMap as $amount) {
if ($amount != 0) {
$this->creditList[] = $amount;
}
}
$n = count($this->creditList);
return $this->dfs(0, $n);
}
private function dfs($cur, $n) {
while ($cur < $n && $this->creditList[$cur] == 0) {
$cur++;
}
if ($cur == $n) {
return 0;
}
$cost = PHP_INT_MAX;
for ($nxt = $cur + 1; $nxt < $n; $nxt++) {
if ($this->creditList[$nxt] * $this->creditList[$cur] < 0) {
$this->creditList[$nxt] += $this->creditList[$cur];
$cost = min($cost, 1 + $this->dfs($cur + 1, $n));
$this->creditList[$nxt] -= $this->creditList[$cur];
}
}
return $cost;
}
}
Input: words = ["abcd","dcba","lls","s","sssll"]
Output: [[0,1],[1,0],[3,2],[2,4]]
Explanation: The palindromes are ["abcddcba","dcbaabcd","slls","llssssll"]
class Solution {
/**
* @param String[] $words
* @return Integer[][]
*/
function palindromePairs($words) {
$pairs = [];
for ($i = 0; $i < count($words); $i++) {
for ($j = 0; $j < count($words); $j++) {
if ($i == $j) continue;
$combined = $words[$i] . $words[$j];
if ($combined == strrev($combined)) {
$pairs[] = [$i, $j];
}
}
}
return $pairs;
}
}
Input: nums = [9], target = 3
Output: 0
class Solution {
private $memo = [];
public function combinationSum4($nums, $target) {
return $this->combs($nums, $target);
}
private function combs($nums, $remain) {
if ($remain == 0) {
return 1;
}
if (isset($this->memo[$remain])) {
return $this->memo[$remain];
}
$result = 0;
foreach ($nums as $num) {
if ($remain - $num >= 0) {
$result += $this->combs($nums, $remain - $num);
}
}
$this->memo[$remain] = $result;
return $result;
}
}
class Solution {
public function islandPerimeter($grid) {
$rows = count($grid);
$cols = count($grid[0]);
$result = 0;
for ($r = 0; $r < $rows; $r++) {
for ($c = 0; $c < $cols; $c++) {
if ($grid[$r][$c] == 1) {
$up = ($r == 0) ? 0 : $grid[$r-1][$c];
$left = ($c == 0) ? 0 : $grid[$r][$c-1];
$down = ($r == $rows-1) ? 0 : $grid[$r+1][$c];
$right = ($c == $cols-1) ? 0 : $grid[$r][$c+1];
$result += 4 - ($up + $left + $right + $down);
}
}
}
return $result;
}
}
Input: nums = [1,7,4,9,2,5]
Output: 6
Explanation: The entire sequence is a wiggle sequence with differences (6, -3, 5, -7, 3).
class Solution {
public function islandPerimeter($grid) {
$rows = count($grid);
$cols = count($grid[0]);
$result = 0;
for ($r = 0; $r < $rows; $r++) {
for ($c = 0; $c < $cols; $c++) {
if ($grid[$r][$c] == 1) {
$up = ($r == 0) ? 0 : $grid[$r-1][$c];
$left = ($c == 0) ? 0 : $grid[$r][$c-1];
$down = ($r == $rows-1) ? 0 : $grid[$r+1][$c];
$right = ($c == $cols-1) ? 0 : $grid[$r][$c+1];
$result += 4 - ($up + $left + $right + $down);
}
}
}
return $result;
}
}