-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat: add php solution to lc problems: No.0029, 0030 #2413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
263fd37
PHP solution of problems 0011,0012
1f9c25e
style: format code and docs with prettier
ZylalMinollari d79fef4
Update Solution.php
yanglbme e19bac3
Update Solution.php
yanglbme 2ecf5ec
Merge branch 'doocs:main' into main
ZylalMinollari 1c2b471
Merge branch 'doocs:main' into main
ZylalMinollari 92e2da6
Merge branch 'doocs:main' into main
ZylalMinollari 062524c
Merge branch 'doocs:main' into main
ZylalMinollari ed22277
Php solution for problems 0029, 0030
fe30b99
style: format code and docs with prettier
ZylalMinollari 0920203
Merge branch 'doocs:main' into problems-29-30
ZylalMinollari 6104508
Changes in naming variables
9a36092
style: format code and docs with prettier
ZylalMinollari aace25e
Removing php tag
21db7e6
Update README.md
yanglbme 5718110
style: format code and docs with prettier
yanglbme a3220ec
Update Solution.php
yanglbme 4884683
Update README_EN.md
yanglbme dfe9a1f
style: format code and docs with prettier
yanglbme d78450f
Update Solution.php
yanglbme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
solution/0000-0099/0029.Divide Two Integers/Solution.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.