Skip to content
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

Renamed packages #2

Merged
merged 12 commits into from
Dec 22, 2023
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 2 additions & 5 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
Expand All @@ -31,6 +31,3 @@ jobs:
- name: Run test suite
run: vendor/bin/phpunit

- name: phpunit-coverage-badge
uses: timkrase/[email protected]

7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
"license": "MIT",
"autoload": {
"psr-4": {
"Javadev\\LeetCodePhp\\": ""
"\\": "src/Algorithms/s0001_two_sum"
}
},
"autoload-dev": {
"psr-4": {
"\\": "tests"
}
},
"authors": [
Expand Down
10 changes: 9 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<coverage includeUncoveredFiles="true" processUncoveredFiles="true">
<include>
<directory suffix=".php">./tests</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory suffix=".php">./tests</directory>
Expand All @@ -17,7 +25,7 @@

<filter>
<whitelist>
<directory suffix=".php">./src</directory>
<directory suffix=".php">./tests</directory>
</whitelist>
</filter>
</phpunit>
59 changes: 0 additions & 59 deletions src/Algorithms/Calculator.php

This file was deleted.

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");
}
}
38 changes: 38 additions & 0 deletions src/Algorithms/s0001_two_sum/readme.md
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?
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;
}
}
35 changes: 35 additions & 0 deletions src/Algorithms/s0002_add_two_numbers/readme.md
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.
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;
}
}
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.
Loading