Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions solution/0000-0099/0029.Divide Two Integers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,44 @@ public class Solution {
}
```

```php
class Solution {
/**
* @param integer $a
* @param integer $b
* @return integer
*/

function divide($a, $b) {
if ($b == 0) {
throw new Exception('Can not divide by 0');
} elseif ($a == 0) {
return 0;
}
if ($a == -2147483648 && $b == -1) {
return 2147483647;
}
$sign = $a < 0 != $b < 0;

$a = abs($a);
$b = abs($b);
$ans = 0;
while ($a >= $b) {
$x = $b;
$cnt = 1;
while ($a >= $x << 1) {
$x <<= 1;
$cnt <<= 1;
}
$a -= $x;
$ans += $cnt;
}

return $sign ? -$ans : $ans;
}
}
```

<!-- tabs:end -->

<!-- end -->
38 changes: 38 additions & 0 deletions solution/0000-0099/0029.Divide Two Integers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,44 @@ public class Solution {
}
```

```php
class Solution {
/**
* @param integer $a
* @param integer $b
* @return integer
*/

function divide($a, $b) {
if ($b == 0) {
throw new Exception('Can not divide by 0');
} elseif ($a == 0) {
return 0;
}
if ($a == -2147483648 && $b == -1) {
return 2147483647;
}
$sign = $a < 0 != $b < 0;

$a = abs($a);
$b = abs($b);
$ans = 0;
while ($a >= $b) {
$x = $b;
$cnt = 1;
while ($a >= $x << 1) {
$x <<= 1;
$cnt <<= 1;
}
$a -= $x;
$ans += $cnt;
}

return $sign ? -$ans : $ans;
}
}
```

<!-- tabs:end -->

<!-- end -->
35 changes: 35 additions & 0 deletions solution/0000-0099/0029.Divide Two Integers/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
/**
* @param integer $a
* @param integer $b
* @return integer
*/

function divide($a, $b) {
if ($b == 0) {
throw new Exception('Can not divide by 0');
} elseif ($a == 0) {
return 0;
}
if ($a == -2147483648 && $b == -1) {
return 2147483647;
}
$sign = $a < 0 != $b < 0;

$a = abs($a);
$b = abs($b);
$ans = 0;
while ($a >= $b) {
$x = $b;
$cnt = 1;
while ($a >= $x << 1) {
$x <<= 1;
$cnt <<= 1;
}
$a -= $x;
$ans += $cnt;
}

return $sign ? -$ans : $ans;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,62 @@ public class Solution {
}
```

```php
class Solution {
/**
* @param String $s
* @param String[] $words
* @return Integer[]
*/
function findSubstring($s, $words) {
$cnt = [];
foreach ($words as $w) {
if (!isset($cnt[$w])) {
$cnt[$w] = 1;
} else {
$cnt[$w]++;
}
}
$m = strlen($s);
$n = count($words);
$k = strlen($words[0]);
$ans = [];
for ($i = 0; $i < $k; ++$i) {
$cnt1 = [];
$l = $i;
$r = $i;
$t = 0;
while ($r + $k <= $m) {
$w = substr($s, $r, $k);
$r += $k;
if (!array_key_exists($w, $cnt)) {
$cnt1 = [];
$l = $r;
$t = 0;
continue;
}
if (!isset($cnt1[$w])) {
$cnt1[$w] = 1;
} else {
$cnt1[$w]++;
}
++$t;
while ($cnt1[$w] > $cnt[$w]) {
$remove = substr($s, $l, $k);
$l += $k;
$cnt1[$remove]--;
$t--;
}
if ($t == $n) {
$ans[] = $l;
}
}
}
return $ans;
}
}
```

<!-- tabs:end -->

### 方法二
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,62 @@ public class Solution {
}
```

```php
class Solution {
/**
* @param String $s
* @param String[] $words
* @return Integer[]
*/
function findSubstring($s, $words) {
$cnt = [];
foreach ($words as $w) {
if (!isset($cnt[$w])) {
$cnt[$w] = 1;
} else {
$cnt[$w]++;
}
}
$m = strlen($s);
$n = count($words);
$k = strlen($words[0]);
$ans = [];
for ($i = 0; $i < $k; ++$i) {
$cnt1 = [];
$l = $i;
$r = $i;
$t = 0;
while ($r + $k <= $m) {
$w = substr($s, $r, $k);
$r += $k;
if (!array_key_exists($w, $cnt)) {
$cnt1 = [];
$l = $r;
$t = 0;
continue;
}
if (!isset($cnt1[$w])) {
$cnt1[$w] = 1;
} else {
$cnt1[$w]++;
}
++$t;
while ($cnt1[$w] > $cnt[$w]) {
$remove = substr($s, $l, $k);
$l += $k;
$cnt1[$remove]--;
$t--;
}
if ($t == $n) {
$ans[] = $l;
}
}
}
return $ans;
}
}
```

<!-- tabs:end -->

### Solution 2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Solution {
/**
* @param String $s
* @param String[] $words
* @return Integer[]
*/
function findSubstring($s, $words) {
$cnt = [];
foreach ($words as $w) {
if (!isset($cnt[$w])) {
$cnt[$w] = 1;
} else {
$cnt[$w]++;
}
}
$m = strlen($s);
$n = count($words);
$k = strlen($words[0]);
$ans = [];
for ($i = 0; $i < $k; ++$i) {
$cnt1 = [];
$l = $i;
$r = $i;
$t = 0;
while ($r + $k <= $m) {
$w = substr($s, $r, $k);
$r += $k;
if (!array_key_exists($w, $cnt)) {
$cnt1 = [];
$l = $r;
$t = 0;
continue;
}
if (!isset($cnt1[$w])) {
$cnt1[$w] = 1;
} else {
$cnt1[$w]++;
}
++$t;
while ($cnt1[$w] > $cnt[$w]) {
$remove = substr($s, $l, $k);
$l += $k;
$cnt1[$remove]--;
$t--;
}
if ($t == $n) {
$ans[] = $l;
}
}
}
return $ans;
}
}