Skip to content

Added tasks 1-46 #1

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 1 commit into from
Dec 28, 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions LeetCodeNet/G0001_0100/S0001_two_sum/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)

## 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?

## Solution

```csharp
public class Solution {
public int[] TwoSum(int[] numbers, int target) {
Dictionary<int, int> indexMap = new Dictionary<int, int>();
for (int i = 0; i < numbers.Length; i++) {
int requiredNum = target - numbers[i];
if (indexMap.ContainsKey(requiredNum)) {
return new int[] {indexMap[requiredNum], i};
}
indexMap[numbers[i]] = i;
}
return new int[] {-1, -1};
}
}
```
66 changes: 66 additions & 0 deletions LeetCodeNet/G0001_0100/S0002_add_two_numbers/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)

## 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.

## Solution

```csharp
using LeetCodeNet.Com_github_leetcode;

public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode curr = dummyHead;
int carry = 0;
while (l1 != null || l2 != null) {
int x = (l1 != null) ? l1.val : 0;
int y = (l2 != null) ? l2.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (l1 != null) l1 = l1.next;
if (l2 != null) l2 = l2.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)

## 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.

## Solution

```csharp
public class Solution {
public int LengthOfLongestSubstring(string s) {
int[] lastIndices = new int[256];
for (int i = 0; i < 256; i++) {
lastIndices[i] = -1;
}
int maxLen = 0;
int curLen = 0;
int start = 0;
for (int i = 0; i < s.Length; i++) {
char cur = s[i];
if (lastIndices[cur] < start) {
lastIndices[cur] = i;
curLen++;
} else {
int lastIndex = lastIndices[cur];
start = lastIndex + 1;
curLen = i - start + 1;
lastIndices[cur] = i;
}
if (curLen > maxLen) {
maxLen = curLen;
}
}
return maxLen;
}
}
```
90 changes: 90 additions & 0 deletions LeetCodeNet/G0001_0100/S0004_median_of_two_sorted_arrays/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)

## 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>

## Solution

```csharp
public class Solution {
public double FindMedianSortedArrays(int[] nums1, int[] nums2) {
if (nums2.Length < nums1.Length) {
return FindMedianSortedArrays(nums2, nums1);
}
int cut1;
int cut2;
int n1 = nums1.Length;
int n2 = nums2.Length;
int low = 0;
int high = n1;
while (low <= high) {
cut1 = (low + high) / 2;
cut2 = ((n1 + n2 + 1) / 2) - cut1;
int l1 = cut1 == 0 ? int.MinValue : nums1[cut1 - 1];
int l2 = cut2 == 0 ? int.MinValue : nums2[cut2 - 1];
int r1 = cut1 == n1 ? int.MaxValue : nums1[cut1];
int r2 = cut2 == n2 ? int.MaxValue : nums2[cut2];
if (l1 <= r2 && l2 <= r1) {
if ((n1 + n2) % 2 == 0) {
return (Math.Max(l1, l2) + Math.Min(r1, r2)) / 2.0;
}
return Math.Max(l1, l2);
} else if (l1 > r2) {
high = cut1 - 1;
} else {
low = cut1 + 1;
}
}
return 0.0;
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)

## 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.

## Solution

```csharp
public class Solution {
public string LongestPalindrome(string s) {
if (s.Length == 1) return s;
int res = 0;
int l = 0;
int r = 0;
int len = s.Length;
for (int i = 0; i < len; i ++) {
for (int diff = 1; i - diff + 1 >= 0 && i + diff < len; diff ++) {
if (s[i - diff + 1] != s[i + diff]) break;
else if (res < diff * 2) {
res = diff * 2;
l = i - diff + 1;
r = i + diff;
}
}
}
for (int i = 0; i < len; i ++) {
for (int diff = 1; i - diff >= 0 && i + diff < len; diff ++) {
if (s[i - diff] != s[i + diff]) break;
else if (res < diff * 2 + 1) {
res = diff * 2 + 1;
l = i - diff;
r = i + diff;
}
}
}
return s.Substring(l, r - l + 1);
}
}
```
Loading