Цена за 48 часов в ленте | 450,00 |
Цена за 1 час закрепления | N/A |
Взаимопиар | Нет |
Дополнительные условия рекламы | Отсутствуют |
Input: n = 5
Output: 5
Explanation:
Here are the non-negative integers <= 5 with their corresponding binary representations:
0 : 0
1 : 1
2 : 10
3 : 11
4 : 100
5 : 101
Among them, only integer 3 disobeys the rule (two consecutive ones) and the other 5 satisfy the rule.
class Solution {
function findIntegers($num) {
$count = 0;
for ($i = 0; $i <= $num; $i++) {
if ($this->check($i)) {
$count++;
}
}
return $count;
}
function check($n) {
$i = 31;
while ($i > 0) {
if (($n & (1 << $i)) != 0 && ($n & (1 << ($i - 1))) != 0) {
return false;
}
$i--;
}
return true;
}
}
Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
Output: ["Shogun"]
Explanation: The only common string is "Shogun".
class Solution {
function findRestaurant($list1, $list2) {
$map = [];
for ($i = 0; $i < count($list1); $i++) {
for ($j = 0; $j < count($list2); $j++) {
if ($list1[$i] == $list2[$j]) {
if (!isset($map[$i + $j])) {
$map[$i + $j] = [];
}
$map[$i + $j][] = $list1[$i];
}
}
}
$minIndexSum = min(array_keys($map));
return $map[$minIndexSum];
}
}
Input: nums = [1,3,2,2,5,2,3,7]
Output: 5
Explanation: The longest harmonious subsequence is [3,2,2,2,3].
class Solution {
function findLHS($nums) {
$count = [];
$res = 0;
foreach ($nums as $num) {
if (isset($count[$num])) {
$count[$num]++;
} else {
$count[$num] = 1;
}
if (isset($count[$num + 1])) {
$res = max($res, $count[$num] + $count[$num + 1]);
}
if (isset($count[$num - 1])) {
$res = max($res, $count[$num] + $count[$num - 1]);
}
}
return $res;
}
}
Input: m = 3, n = 3, ops = [[2,2],[3,3]]
Output: 4
Explanation: The maximum integer in M is 2, and there are four of it in M. So return 4.
class Solution {
function maxCount($m, $n, $ops) {
$minA = $m;
$minB = $n;
foreach ($ops as $op) {
$minA = min($minA, $op[0]);
$minB = min($minB, $op[1]);
}
return $minA * $minB;
}
}
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: true
class Solution {
private function dist($p1, $p2) {
return pow($p2[1] - $p1[1], 2) + pow($p2[0] - $p1[0], 2);
}
private function check($p1, $p2, $p3, $p4) {
return $this->dist($p1, $p2) > 0 &&
$this->dist($p1, $p2) == $this->dist($p2, $p3) &&
$this->dist($p2, $p3) == $this->dist($p3, $p4) &&
$this->dist($p3, $p4) == $this->dist($p4, $p1) &&
$this->dist($p1, $p3) == $this->dist($p2, $p4);
}
public function validSquare($p1, $p2, $p3, $p4) {
return $this->check($p1, $p2, $p3, $p4) ||
$this->check($p1, $p3, $p2, $p4) ||
$this->check($p1, $p2, $p4, $p3);
}
}
Input: expression = "-1/2+1/2+1/3"
Output: "1/3"
class Solution {
function fractionAddition($expression) {
$sign = [];
if ($expression[0] !== '-') $sign[] = '+';
for ($i = 0; $i < strlen($expression); $i++) {
if ($expression[$i] == '+' || $expression[$i] == '-') {
$sign[] = $expression[$i];
}
}
$fractions = preg_split('/(?=[+-])/', $expression);
$prev_num = 0;
$prev_den = 1;
$i = 0;
foreach ($fractions as $sub) {
if ($sub == "") continue;
list($num, $den) = sscanf($sub, '%d/%d');
$g = abs($this->gcd($prev_den, $den));
if ($sign[$i++] == '+') $prev_num = $prev_num * $den / $g + $num * $prev_den / $g;
else $prev_num = $prev_num * $den / $g - $num * $prev_den / $g;
$prev_den = $prev_den * $den / $g;
$g = abs($this->gcd($prev_den, $prev_num));
$prev_num /= $g;
$prev_den /= $g;
}
return "$prev_num/$prev_den";
}
private function gcd($a, $b) {
while ($b != 0) {
$t = $b;
$b = $a % $b;
$a = $t;
}
return $a;
}
}
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
class Node {
public $val;
public $children;
public function __construct($val = 0, $children = []) {
$this->val = $val;
$this->children = $children;
}
}
class Solution {
function postorder($root) {
if ($root === null) return [];
$stack = [$root];
$output = [];
while (!empty($stack)) {
$node = array_pop($stack);
array_unshift($output, $node->val);
foreach ($node->children as $child) {
array_push($stack, $child);
}
}
return $output;
}
}
Input: s = "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
class Solution {
function reverseWords($s) {
$chars = str_split($s);
$lastSpaceIndex = -1;
$len = count($chars);
for ($strIndex = 0; $strIndex <= $len; $strIndex++) {
if ($strIndex == $len || $chars[$strIndex] == ' ') {
$startIndex = $lastSpaceIndex + 1;
$endIndex = $strIndex - 1;
while ($startIndex < $endIndex) {
$temp = $chars[$startIndex];
$chars[$startIndex] = $chars[$endIndex];
$chars[$endIndex] = $temp;
$startIndex++;
$endIndex--;
}
$lastSpaceIndex = $strIndex;
}
}
return implode('', $chars);
}
}
Input: n = 12
Output: 21
class Solution {
private function swap($s, $i0, $i1) {
if ($i0 == $i1) return $s;
$chars = str_split($s);
$temp = $chars[$i0];
$chars[$i0] = $chars[$i1];
$chars[$i1] = $temp;
return implode('', $chars);
}
private $list = [];
private function permute($a, $l, $r) {
if ($l == $r) {
$this->list[] = $a;
} else {
for ($i = $l; $i <= $r; $i++) {
$a = $this->swap($a, $l, $i);
$this->permute($a, $l + 1, $r);
$a = $this->swap($a, $l, $i);
}
}
}
public function nextGreaterElement($n) {
$s = strval($n);
$this->permute($s, 0, strlen($s) - 1);
sort($this->list);
$index = array_search($s, $this->list);
if ($index !== false && $index < count($this->list) - 1) {
$result = intval($this->list[$index + 1]);
if ($result <= 2147483647) {
return $result;
}
}
return -1;
}
}
Input: wall = [[1,2,2,1],[3,1,2],[1,3,2],[2,4],[3,1,2],[1,3,1,1]]
Output: 2
class Solution {
function leastBricks($wall) {
$pos = array_fill(0, count($wall), 0);
$sum = array_sum($wall[0]);
$res = PHP_INT_MAX;
while ($sum != 0) {
$count = 0;
foreach ($wall as $i => &$row) {
if ($row[$pos[$i]] != 0) {
$count++;
} else {
$pos[$i]++;
}
$row[$pos[$i]]--;
}
$sum--;
$res = min($res, $count);
}
return $res;
}
}
Input: nums = [1000,100,10,2]
Output: "1000/(100/10/2)"
Explanation: 1000/(100/10/2) = 1000/((100/10)/2) = 200
However, the bold parenthesis in "1000/((100/10)/2)" are redundant since they do not influence the operation priority.
So you should return "1000/(100/10/2)".
Other cases:
1000/(100/10)/2 = 50
1000/(100/(10/2)) = 50
1000/100/10/2 = 0.5
1000/100/(10/2) = 2
class Solution {
private function addStrings($num1, $num2) {
$ans = [];
$carry = 0;
$n1 = count($num1);
$n2 = count($num2);
for ($i = 0; $i < max($n1, $n2) || $carry; $i++) {
$digit1 = $i < $n1 ? $num1[$i] : 0;
$digit2 = $i < $n2 ? $num2[$i] : 0;
$sum = $digit1 + $digit2 + $carry;
$carry = intdiv($sum, 10);
$ans[] = $sum % 10;
}
return $ans;
}
private function multiplyOneDigit($firstNumber, $secondNumberDigit, $numZeros) {
$currentResult = array_fill(0, $numZeros, 0);
$carry = 0;
for ($i = 0; $i < strlen($firstNumber); $i++) {
$multiplication = (intval($secondNumberDigit) * intval($firstNumber[$i])) + $carry;
$carry = intdiv($multiplication, 10);
$currentResult[] = $multiplication % 10;
}
if ($carry != 0) {
$currentResult[] = $carry;
}
return $currentResult;
}
public function multiply($firstNumber, $secondNumber) {
if ($firstNumber === "0" || $secondNumber === "0") {
return "0";
}
$firstNumber = strrev($firstNumber);
$secondNumber = strrev($secondNumber);
$ans = array_fill(0, strlen($firstNumber) + strlen($secondNumber), 0);
for ($i = 0; $i < strlen($secondNumber); $i++) {
$ans = $this->addStrings($this->multiplyOneDigit($firstNumber, $secondNumber[$i], $i), $ans);
}
while (end($ans) === 0) {
array_pop($ans);
}
return implode('', array_reverse($ans));
}
}
Input: board = [["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]
Output: 2
function countBattleships($board) {
$m = count($board);
$n = count($board[0]);
$count = 0;
for ($i = 0; $i < $m; $i++) {
for ($j = 0; $j < $n; $j++) {
if ($board[$i][$j] == 'X') {
if (($i == 0 || $board[$i-1][$j] != 'X') && ($j == 0 || $board[$i][$j-1] != 'X')) {
$count++;
}
}
}
}
return $count;
}
Input: sentence = ["hello","world"], rows = 2, cols = 8
Output: 1
function wordsTyping($sentence, $rows, $cols) {
$sentenceStr = implode(" ", $sentence) . " ";
$length = strlen($sentenceStr);
$pos = 0;
for ($i = 0; $i < $rows; $i++) {
$pos += $cols;
if ($sentenceStr[$pos % $length] == " ") {
$pos++;
} else {
while ($pos > 0 && $sentenceStr[($pos - 1) % $length] != " ") {
$pos--;
}
}
}
return intdiv($pos, $length);
}
Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]
function pacificAtlantic($heights) {
$m = count($heights);
$n = count($heights[0]);
$pacific = array_fill(0, $m, array_fill(0, $n, false));
$atlantic = array_fill(0, $m, array_fill(0, $n, false));
$directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
function dfs($r, $c, &$ocean, $heights, $directions) {
$ocean[$r][$c] = true;
foreach ($directions as [$dr, $dc]) {
$nr = $r + $dr;
$nc = $c + $dc;
if ($nr >= 0 && $nc >= 0 && $nr < count($heights) && $nc < count($heights[0]) && !$ocean[$nr][$nc] && $heights[$nr][$nc] >= $heights[$r][$c]) {
dfs($nr, $nc, $ocean, $heights, $directions);
}
}
}
for ($i = 0; $i < $m; $i++) {
dfs($i, 0, $pacific, $heights, $directions);
dfs($i, $n - 1, $atlantic, $heights, $directions);
}
for ($j = 0; $j < $n; $j++) {
dfs(0, $j, $pacific, $heights, $directions);
dfs($m - 1, $j, $atlantic, $heights, $directions);
}
$result = [];
for ($i = 0; $i < $m; $i++) {
for ($j = 0; $j < $n; $j++) {
if ($pacific[$i][$j] && $atlantic[$i][$j]) {
$result[] = [$i, $j];
}
}
}
return $result;
}
Input: nums = [1,5,11,5]
Output: true
function canPartition($nums) {
$sum = array_sum($nums);
if ($sum % 2 != 0) return false;
$target = $sum / 2;
$dp = array_fill(0, $target + 1, false);
$dp[0] = true;
foreach ($nums as $num) {
for ($j = $target; $j >= $num; $j--) {
$dp[$j] = $dp[$j] || $dp[$j - $num];
}
}
return $dp[$target];
}
Input: num1 = "11", num2 = "123"
Output: "134"
function thirdMax($nums) {
$first = null;
$second = null;
$third = null;
foreach ($nums as $num) {
if ($num === $first || $num === $second || $num === $third) {
continue;
}
if ($first === null || $num > $first) {
$third = $second;
$second = $first;
$first = $num;
} elseif ($second === null || $num > $second) {
$third = $second;
$second = $num;
} elseif ($third === null || $num > $third) {
$third = $num;
}
}
return $third !== null ? $third : $first;
}
Input: nums = [3,2,1]
Output: 1
function thirdMax($nums) {
$first = null;
$second = null;
$third = null;
foreach ($nums as $num) {
if ($num === $first || $num === $second || $num === $third) {
continue;
}
if ($first === null || $num > $first) {
$third = $second;
$second = $first;
$first = $num;
} elseif ($second === null || $num > $second) {
$third = $second;
$second = $num;
} elseif ($third === null || $num > $third) {
$third = $num;
}
}
return $third !== null ? $third : $first;
}
Input: nums = [1,2,3,4]
Output: 3
function numberOfArithmeticSlices($nums) {
$count = 0;
$currentLength = 0;
for ($i = 2; $i < count($nums); $i++) {
if ($nums[$i] - $nums[$i - 1] == $nums[$i - 1] - $nums[$i - 2]) {
$currentLength++;
$count += $currentLength;
} else {
$currentLength = 0;
}
}
return $count;
}