Skip to content

Commit

Permalink
Added tasks 3-10
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Dec 3, 2023
1 parent 05a3595 commit bd234ed
Show file tree
Hide file tree
Showing 25 changed files with 882 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/main/php/g0001_0100/s0001_two_sum/Solution.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// #2023_11_29_Time_9_ms_(97.47%)_Space_19.8_MB_(77.40%)

class Solution {

/**
* @param Integer[] $nums
* @param Integer $target
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace leetcode\g0001_0100\s0003_longest_substring_without_repeating_characters;

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
// #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
// #Big_O_Time_O(n)_Space_O(1) #2023_12_03_Time_4_ms_(99.33%)_Space_19.1_MB_(68.83%)

class Solution {
/**
* @param String $s
* @return Integer
*/
public function lengthOfLongestSubstring($s) {
$lastIndices = array_fill(0, 256, -1);
$maxLen = 0;
$curLen = 0;
$start = 0;

for ($i = 0; $i < strlen($s); $i++) {
$cur = $s[$i];
$curAscii = ord($cur);

if ($lastIndices[$curAscii] < $start) {
$lastIndices[$curAscii] = $i;
$curLen++;
} else {
$lastIndex = $lastIndices[$curAscii];
$start = $lastIndex + 1;
$curLen = $i - $start + 1;
$lastIndices[$curAscii] = $i;
}

if ($curLen > $maxLen) {
$maxLen = $curLen;
}
}

return $maxLen;
}
}
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace leetcode\g0001_0100\s0004_median_of_two_sorted_arrays;

// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
// #Big_O_Time_O(log(min(N,M)))_Space_O(1) #2023_12_03_Time_23_ms_(88.34%)_Space_19.1_MB_(87.47%)

class Solution {
/**
* @param Integer[] $nums1
* @param Integer[] $nums2
* @return Float
*/
function findMedianSortedArrays($nums1, $nums2) {
$nums3 = [];
$sum = count($nums1) + count($nums2);
$med = ($sum / 2);
if ($sum % 2 === 0) {
$index = [$med - 1, $med];
} else {
$index = [floor($med)];
}
$i = 0;
$i1 = 0;
$i2 = 0;
while ($i < $sum) {
if (!isset($nums1[$i1])) {
$nums3[] = $nums2[$i2];
$i2++;
$i++;
continue;
}
if (!isset($nums2[$i2])) {
$nums3[] = $nums1[$i1];
$i1++;
$i++;
continue;
}
if ($nums1[$i1] < $nums2[$i2]) {
$nums3[] = $nums1[$i1];
$i1++;
} else {
$nums3[] = $nums2[$i2];
$i2++;
}
$i++;
}
return isset($index[1])
? ($nums3[$index[0]] + $nums3[$index[1]]) / 2
: $nums3[$index[0]];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
4\. Median of Two Sorted Arrays

Hard

Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.

The overall run time complexity should be `O(log (m+n))`.

**Example 1:**

**Input:** nums1 = [1,3], nums2 = [2]

**Output:** 2.00000

**Explanation:** merged array = [1,2,3] and median is 2.

**Example 2:**

**Input:** nums1 = [1,2], nums2 = [3,4]

**Output:** 2.50000

**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

**Example 3:**

**Input:** nums1 = [0,0], nums2 = [0,0]

**Output:** 0.00000

**Example 4:**

**Input:** nums1 = [], nums2 = [1]

**Output:** 1.00000

**Example 5:**

**Input:** nums1 = [2], nums2 = []

**Output:** 2.00000

**Constraints:**

* `nums1.length == m`
* `nums2.length == n`
* `0 <= m <= 1000`
* `0 <= n <= 1000`
* `1 <= m + n <= 2000`
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace leetcode\g0001_0100\s0005_longest_palindromic_substring;

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
// #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
// #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
// #2023_12_03_Time_13_ms_(98.57%)_Space_19.4_MB_(12.54%)

class Solution {
/**
* @param String $s
* @return String
*/
function longestPalindrome($s): string {
if (($length = strlen($s)) <= 1) {
return $s;
}
if (strrev($s) === $s) {
return $s;
}

$max_length = 1;
for ($i = 0; $i < $length; ++$i) {
for ($len = $max_length; $len <= $length; ++$len) {
$start = $i - ($len >> 1);
if ($start < 0 || $start + $len > $length) {
break;
}
$substr = substr($s, $start, $len);
if ($substr === strrev($substr)) {
$str = $substr;
$max_length = $len;
} elseif ($max_length + 1 < $len) {
break;
}
}
}

return $str;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
5\. Longest Palindromic Substring

Medium

Given a string `s`, return _the longest palindromic substring_ in `s`.

**Example 1:**

**Input:** s = "babad"

**Output:** "bab" **Note:** "aba" is also a valid answer.

**Example 2:**

**Input:** s = "cbbd"

**Output:** "bb"

**Example 3:**

**Input:** s = "a"

**Output:** "a"

**Example 4:**

**Input:** s = "ac"

**Output:** "a"

**Constraints:**

* `1 <= s.length <= 1000`
* `s` consist of only digits and English letters.
38 changes: 38 additions & 0 deletions src/main/php/g0001_0100/s0006_zigzag_conversion/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace leetcode\g0001_0100\s0006_zigzag_conversion;

// #Medium #String #2023_12_03_Time_12_ms_(80.63%)_Space_19.3_MB_(57.66%)

class Solution {
/**
* @param String $s
* @param Integer $numRows
* @return String
*/
function convert($s, $numRows) {
if ($numRows == 1) {
return $s;
}
$row = 0;
$col = 0;
$dir = 'down';
for ($i = 0; $i < strlen($s); $i++) {
$string[$row][] = $s[$i];
if ($row < $numRows - 1 && $dir == 'down') {
$row++;
} elseif ($row == $numRows - 1 && $dir == 'down') {
$row--;
$dir = 'up';
$col++;
} elseif ($row > 0) {
$row--;
$col++;
} else {
$dir = 'down';
$row++;
}
}
return implode(array_merge(...$string));
}
}
39 changes: 39 additions & 0 deletions src/main/php/g0001_0100/s0006_zigzag_conversion/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
6\. Zigzag Conversion

Medium

The string `"PAYPALISHIRING"` is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P A H N A P L S I I G Y I R

And then read line by line: `"PAHNAPLSIIGYIR"`

Write the code that will take a string and make this conversion given a number of rows:

string convert(string s, int numRows);

**Example 1:**

**Input:** s = "PAYPALISHIRING", numRows = 3

**Output:** "PAHNAPLSIIGYIR"

**Example 2:**

**Input:** s = "PAYPALISHIRING", numRows = 4

**Output:** "PINALSIGYAHRPI"

**Explanation:** P I N A L S I G Y A H R P I

**Example 3:**

**Input:** s = "A", numRows = 1

**Output:** "A"

**Constraints:**

* `1 <= s.length <= 1000`
* `s` consists of English letters (lower-case and upper-case), `','` and `'.'`.
* `1 <= numRows <= 1000`
23 changes: 23 additions & 0 deletions src/main/php/g0001_0100/s0007_reverse_integer/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace leetcode\g0001_0100\s0007_reverse_integer;

// #Medium #Top_Interview_Questions #Math #Udemy_Integers
// #2023_12_03_Time_3_ms_(90.99%)_Space_19.2_MB_(44.23%)

class Solution {
function reverse($x) {
$ls_negative = '';
if (strpos($x, '-') === 0) {
$ls_negative = substr($x, 0, 1);
$x = substr($x, 1);
}
$la_chars = str_split($x);
$la_chars = array_reverse($la_chars);
$li_result = intval($ls_negative . implode('', $la_chars));
if ($li_result > 2147483647 || $li_result < -2147483647) {
$li_result = 0;
}
return $li_result;
}
}
Loading

0 comments on commit bd234ed

Please sign in to comment.