-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
330 changed files
with
10,267 additions
and
4,273 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
40 changes: 20 additions & 20 deletions
40
src/Algorithms/0001.two-sum/two-sum.php → src/Algorithms/s0001_two_sum/Solution.php
This file contains 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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
class Solution { | ||
|
||
/** | ||
* @param Integer[] $nums | ||
* @param Integer $target | ||
* @return Integer[] | ||
*/ | ||
function twoSum($nums, $target) { | ||
$map = []; | ||
for ($i = 0; $i < count($nums); $i++) { | ||
$map[$nums[$i]] = $i; | ||
} | ||
for ($i = 0; $i < count($nums); $i++) { | ||
$complement = $target - $nums[$i]; | ||
if (array_key_exists($complement, $map) && $map[$complement] != $i) { | ||
return [$i, $map[$complement] ]; | ||
} | ||
} | ||
throw new IllegalArgumentException("No two sum solution"); | ||
} | ||
class Solution { | ||
|
||
/** | ||
* @param Integer[] $nums | ||
* @param Integer $target | ||
* @return Integer[] | ||
*/ | ||
function twoSum($nums, $target) { | ||
$map = []; | ||
for ($i = 0; $i < count($nums); $i++) { | ||
$map[$nums[$i]] = $i; | ||
} | ||
for ($i = 0; $i < count($nums); $i++) { | ||
$complement = $target - $nums[$i]; | ||
if (array_key_exists($complement, $map) && $map[$complement] != $i) { | ||
return [$i, $map[$complement] ]; | ||
} | ||
} | ||
throw new IllegalArgumentException("No two sum solution"); | ||
} | ||
} |
This file contains 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,38 @@ | ||
1\. Two Sum | ||
|
||
Easy | ||
|
||
Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_. | ||
|
||
You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice. | ||
|
||
You can return the answer in any order. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [2,7,11,15], target = 9 | ||
|
||
**Output:** [0,1] | ||
|
||
**Explanation:** Because nums[0] + nums[1] == 9, we return [0, 1]. | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [3,2,4], target = 6 | ||
|
||
**Output:** [1,2] | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [3,3], target = 6 | ||
|
||
**Output:** [0,1] | ||
|
||
**Constraints:** | ||
|
||
* <code>2 <= nums.length <= 10<sup>4</sup></code> | ||
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code> | ||
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code> | ||
* **Only one valid answer exists.** | ||
|
||
**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>)</code> time complexity? |
68 changes: 34 additions & 34 deletions
68
.../0002.add-two-numbers/add-two-numbers.php → ...rithms/s0002_add_two_numbers/Solution.php
This file contains 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 |
---|---|---|
@@ -1,35 +1,35 @@ | ||
/** | ||
* Definition for a singly-linked list. | ||
* class ListNode { | ||
* public $val = 0; | ||
* public $next = null; | ||
* function __construct($val) { $this->val = $val; } | ||
* } | ||
*/ | ||
class Solution { | ||
|
||
/** | ||
* @param ListNode $l1 | ||
* @param ListNode $l2 | ||
* @return ListNode | ||
*/ | ||
function addTwoNumbers($l1, $l2) { | ||
$dummyHead = new ListNode(0); | ||
$p = $l1; $q = $l2; $curr = $dummyHead; | ||
$carry = 0; | ||
while ($p != null || $q != null) { | ||
$x = ($p != null) ? $p->val : 0; | ||
$y = ($q != null) ? $q->val : 0; | ||
$sum = $carry + $x + $y; | ||
$carry = intval($sum / 10); | ||
$curr->next = new ListNode($sum % 10); | ||
$curr = $curr->next; | ||
if ($p != null) $p = $p->next; | ||
if ($q != null) $q = $q->next; | ||
} | ||
if ($carry > 0) { | ||
$curr->next = new ListNode($carry); | ||
} | ||
return $dummyHead->next; | ||
} | ||
/** | ||
* Definition for a singly-linked list. | ||
* class ListNode { | ||
* public $val = 0; | ||
* public $next = null; | ||
* function __construct($val) { $this->val = $val; } | ||
* } | ||
*/ | ||
class Solution { | ||
|
||
/** | ||
* @param ListNode $l1 | ||
* @param ListNode $l2 | ||
* @return ListNode | ||
*/ | ||
function addTwoNumbers($l1, $l2) { | ||
$dummyHead = new ListNode(0); | ||
$p = $l1; $q = $l2; $curr = $dummyHead; | ||
$carry = 0; | ||
while ($p != null || $q != null) { | ||
$x = ($p != null) ? $p->val : 0; | ||
$y = ($q != null) ? $q->val : 0; | ||
$sum = $carry + $x + $y; | ||
$carry = intval($sum / 10); | ||
$curr->next = new ListNode($sum % 10); | ||
$curr = $curr->next; | ||
if ($p != null) $p = $p->next; | ||
if ($q != null) $q = $q->next; | ||
} | ||
if ($carry > 0) { | ||
$curr->next = new ListNode($carry); | ||
} | ||
return $dummyHead->next; | ||
} | ||
} |
This file contains 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 @@ | ||
2\. Add Two Numbers | ||
|
||
Medium | ||
|
||
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. | ||
|
||
You may assume the two numbers do not contain any leading zero, except the number 0 itself. | ||
|
||
**Example 1:** | ||
|
||
![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg) | ||
|
||
**Input:** l1 = [2,4,3], l2 = [5,6,4] | ||
|
||
**Output:** [7,0,8] | ||
|
||
**Explanation:** 342 + 465 = 807. | ||
|
||
**Example 2:** | ||
|
||
**Input:** l1 = [0], l2 = [0] | ||
|
||
**Output:** [0] | ||
|
||
**Example 3:** | ||
|
||
**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] | ||
|
||
**Output:** [8,9,9,9,0,0,0,1] | ||
|
||
**Constraints:** | ||
|
||
* The number of nodes in each linked list is in the range `[1, 100]`. | ||
* `0 <= Node.val <= 9` | ||
* It is guaranteed that the list represents a number that does not have leading zeros. |
38 changes: 19 additions & 19 deletions
38
...ubstring-without-repeating-characters.php → ...without_repeating_characters/Solution.php
This file contains 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 |
---|---|---|
@@ -1,20 +1,20 @@ | ||
class Solution { | ||
|
||
/** | ||
* @param String $s | ||
* @return Integer | ||
*/ | ||
function lengthOfLongestSubstring($s) { | ||
if (strlen($s)==0) return 0; | ||
$map = []; | ||
$max = 0; | ||
for ($i=0, $j=0; $i < strlen($s); ++$i) { | ||
if (array_key_exists($s[$i], $map)){ | ||
$j = max($j, $map[$s[$i]] + 1); | ||
} | ||
$map[$s[$i]] = $i; | ||
$max = max($max, $i - $j + 1); | ||
} | ||
return $max; | ||
} | ||
class Solution { | ||
|
||
/** | ||
* @param String $s | ||
* @return Integer | ||
*/ | ||
function lengthOfLongestSubstring($s) { | ||
if (strlen($s)==0) return 0; | ||
$map = []; | ||
$max = 0; | ||
for ($i=0, $j=0; $i < strlen($s); ++$i) { | ||
if (array_key_exists($s[$i], $map)){ | ||
$j = max($j, $map[$s[$i]] + 1); | ||
} | ||
$map[$s[$i]] = $i; | ||
$max = max($max, $i - $j + 1); | ||
} | ||
return $max; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/Algorithms/s0003_longest_substring_without_repeating_characters/readme.md
This file contains 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,40 @@ | ||
3\. Longest Substring Without Repeating Characters | ||
|
||
Medium | ||
|
||
Given a string `s`, find the length of the **longest substring** without repeating characters. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "abcabcbb" | ||
|
||
**Output:** 3 | ||
|
||
**Explanation:** The answer is "abc", with the length of 3. | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = "bbbbb" | ||
|
||
**Output:** 1 | ||
|
||
**Explanation:** The answer is "b", with the length of 1. | ||
|
||
**Example 3:** | ||
|
||
**Input:** s = "pwwkew" | ||
|
||
**Output:** 3 | ||
|
||
**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. | ||
|
||
**Example 4:** | ||
|
||
**Input:** s = "" | ||
|
||
**Output:** 0 | ||
|
||
**Constraints:** | ||
|
||
* <code>0 <= s.length <= 5 * 10<sup>4</sup></code> | ||
* `s` consists of English letters, digits, symbols and spaces. |
Oops, something went wrong.