0% found this document useful (0 votes)
10 views10 pages

LeetCode Problems CSharp

The document is a compilation of over 90 LeetCode problems with solutions in C# .NET, aimed at preparing for software engineering interviews. It covers various topics including Linked Lists, Bit Manipulation, Database/SQL, and advanced patterns like Divide and Conquer. Each section includes problem descriptions and example code implementations.

Uploaded by

Trung Phạm
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

LeetCode Problems CSharp

The document is a compilation of over 90 LeetCode problems with solutions in C# .NET, aimed at preparing for software engineering interviews. It covers various topics including Linked Lists, Bit Manipulation, Database/SQL, and advanced patterns like Divide and Conquer. Each section includes problem descriptions and example code implementations.

Uploaded by

Trung Phạm
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Tổng Hợp Bài Toán LeetCode

Hướng Dẫn Giải Quyết Bằng C# .NET

Tổng Quan
Tài liệu này tổng hợp 90+ bài toán từ các ghi chú chuẩn bị phỏng vấn kỹ sư phần mềm cấp cao, cùng với các ví dụ
mã C# .NET để giải quyết.

Nền Tảng
Năm 2020: Vượt qua phỏng vấn Amazon (SDE)
Năm 2022: Vượt qua phỏng vấn Google (SWE)
Năm 2024: Vượt qua phỏng vấn LinkedIn/Microsoft (Sr. SWE)
Năm 2025: Làm việc tại Meta như Sr. Software Engineer

Linked List
Bài Toán
Rotate List
All O(1) one Data Structure
Insert into a Sorted Circular Linked List
Design Linked List
Reverse Pairs
Closest Room
Nth Highest Salary
Super Egg Drop
Minimum Space Wasted From Packaging
Frequency of the Most Frequent Element
Longest Duplicate Substring
Longest Common Subpath
Pow(x, n)

Bit Manipulation / Bitmask


Bài Toán
Divide Two Integers
Split Array With Same Average
Integer Replacement
Design / System Design
Bài Toán
All O(1) one Data Structure
Design Twitter
Design Linked List
Kth Ancestor of a Tree Node

Database / SQL
Bài Toán
Nth Highest Salary
Word Frequency

Advanced Patterns - Rolling Hash / Suffix Array / Hash Function


Bài Toán
Longest Duplicate Substring
Shortest Palindrome
Longest Common Subpath

Advanced Patterns - Divide and Conquer


Bài Toán
Median of Two Sorted Arrays
Reverse Pairs
Wiggle Sort II

Advanced Patterns - Union Find


Bài Toán
Surrounded Regions
Redundant Connection II

Advanced Patterns - Geometry


Bài Toán
Redundant Connection II
Kth Ancestor of a Tree Node
Surrounded Regions
Water and Jug Problem

Greedy Algorithms
Bài Toán
Maximum Product Subarray
Largest Number
Strong Password Checker
Valid Parenthesis String
Remove K Digits
Maximum Number of Events That Can Be Attended
Split Array With Same Average
Create Maximum Number
Frequency of the Most Frequent Element
Sell Diminishing-Valued Colored Balls

Math & Number Theory


Bài Toán
Integer to English Words
Find the Closest Palindrome
Prime Palindrome
Pow(x, n)
Fraction to Recurring Decimal
Next Greater Element III
Sum of Square Numbers
Nth Digit
Self Crossing
Maximum Number of Visible Points
Make Sum Divisible by P
Number of Digit One
Sell Diminishing-Valued Colored Balls

Heap / Priority Queue


Bài Toán
Maximum Number of Events That Can Be Attended
Sell Diminishing-Valued Colored Balls
Design Twitter
Strong Password Checker

String & String Algorithms


Bài Toán
Longest Palindromic Substring
Integer to English Words
Compare Version Numbers
Longest Substring Without Repeating Characters
Find the Closest Palindrome
Longest Duplicate Substring
String to Integer (atoi)
Regular Expression Matching
Largest Number
Longest Valid Parentheses
Interleaving String
Decode Ways
Reverse Words in a String
Wildcard Matching
Valid Parenthesis String
Remove K Digits
Palindrome Partitioning II
Decode Ways II
Repeated String Match
Validate IP Address
One Edit Distance
Valid Number
Substring with Concatenation of All Words
Strong Password Checker
Fraction to Recurring Decimal
Text Justification
Shortest Palindrome
Making File Names Unique
Decoded String at Index
Regular Expression Matching
Split Array With Same Average
Count The Repetitions

Array & Array Manipulation


Bài Toán
Median of Two Sorted Arrays
3Sum
Jump Game II
Maximum Product Subarray
Next Permutation
132 Pattern
Maximum Number of Events That Can Be Attended
Surrounded Regions
Max Points on a Line
Next Greater Element III
Text Justification
Shortest Unsorted Continuous Subarray
Reverse Pairs
Shortest Subarray with Sum at Least K
Shortest Subarray to be Removed to Make Array Sorted
Water and Jug Problem
Making File Names Unique
Self Crossing
Non-decreasing Array
Continuous Subarray Sum
Dungeon Game
Wiggle Sort II
Ways to Split Array Into Three Subarrays
Maximum Non Negative Product in a Matrix
Frequency of the Most Frequent Element
Maximum Number of Visible Points
Closest Room
Minimize the Difference Between Target and Chosen Elements
Ví Dụ Mã C# .NET
Rotate List
public class ListNode {
public int val;
public ListNode next;
}

public class Solution {


public ListNode RotateRight(ListNode head, int k) {
if (head == null || [Link] == null) return head;

int length = 0;
ListNode curr = head;
ListNode tail = null;

while (curr != null) {


length++;
tail = curr;
curr = [Link];
}

k = k % length;
if (k == 0) return head;

ListNode newTail = head;


for (int i = 0; i < length - k - 1; i++) {
newTail = [Link];
}

ListNode newHead = [Link];


[Link] = null;
[Link] = head;

return newHead;
}
}

Median of Two Sorted Arrays


public class Solution {
public double FindMediaSortedArrays(int[] nums1, int[] nums2) {
if ([Link] > [Link]) {
return FindMediaSortedArrays(nums2, nums1);
}

int m = [Link];
int n = [Link];
int left = 0, right = m;

while (left <= right) {


int cut1 = (left + right) / 2;
int cut2 = (m + n + 1) / 2 - cut1;

int left1 = cut1 == 0 ? [Link] : nums1[cut1 - 1];


int left2 = cut2 == 0 ? [Link] : nums2[cut2 - 1];
int right1 = cut1 == m ? [Link] : nums1[cut1];
int right2 = cut2 == n ? [Link] : nums2[cut2];

if (left1 <= right2 && left2 <= right1) {


if ((m + n) % 2 == 0) {
return ([Link](left1, left2) + [Link](right1, right2)) / 2.0;
}
return [Link](left1, left2);
} else if (left1 > right2) {
right = cut1 - 1;
} else {
left = cut1 + 1;
}
}
return -1;
}
}

Longest Palindromic Substring


public class Solution {
public string LongestPalindrome(string s) {
if ([Link](s)) return "";

int start = 0;
int maxLen = 1;

for (int i = 0; i < [Link]; i++) {


// Odd length palindromes
int len1 = ExpandAroundCenter(s, i, i);
// Even length palindromes
int len2 = ExpandAroundCenter(s, i, i + 1);

int len = [Link](len1, len2);


if (len > maxLen) {
maxLen = len;
start = i - (len - 1) / 2;
}
}

return [Link](start, maxLen);


}

private int ExpandAroundCenter(string s, int left, int right) {


while (left >= 0 && right < [Link] && s[left] == s[right]) {
left--;
right++;
}
return right - left - 1;
}
}

Integer to English Words


public class Solution {
private string[] ones = { "", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine" };
private string[] teens = { "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
"Fifteen",
"Sixteen", "Seventeen", "Eighteen", "Nineteen" };
private string[] tens = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty",
"Seventy", "Eighty", "Ninety" };
private string[] thousands = { "", "Thousand", "Million", "Billion" };

public string NumberToWords(int num) {


if (num == 0) return "Zero";

string result = "";


int groupIndex = 0;

while (num > 0) {


if (num % 1000 != 0) {
result = Helper(num % 1000) + thousands[groupIndex] + " " + result;
}
num /= 1000;
groupIndex++;
}

return [Link]();
}

private string Helper(int num) {


if (num == 0) return "";
else if (num < 10) return ones[num] + " ";
else if (num < 20) return teens[num - 10] + " ";
else if (num < 100) return tens[num / 10] + " " + Helper(num % 10);
else return ones[num / 100] + " Hundred " + Helper(num % 100);
}
}

Regular Expression Matching


public class Solution {
public bool IsMatch(string s, string p) {
bool[,] dp = new bool[[Link] + 1, [Link] + 1];
dp[0, 0] = true;

for (int j = 1; j <= [Link]; j++) {


if (p[j - 1] == '*') {
dp[0, j] = dp[0, j - 2];
}
}

for (int i = 1; i <= [Link]; i++) {


for (int j = 1; j <= [Link]; j++) {
if (p[j - 1] == '*') {
dp[i, j] = dp[i, j - 2];
if (p[j - 2] == '.' || p[j - 2] == s[i - 1]) {
dp[i, j] = dp[i, j] || dp[i - 1, j];
}
} else if (p[j - 1] == '.' || p[j - 1] == s[i - 1]) {
dp[i, j] = dp[i - 1, j - 1];
}
}
}

return dp[[Link], [Link]];


}
}

Longest Valid Parentheses


public class Solution {
public int LongestValidParentheses(string s) {
int maxLen = 0;
int[] dp = new int[[Link]];

for (int i = 1; i < [Link]; i++) {


if (s[i] == ')') {
if (s[i - 1] == '(') {
dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2;
} else if (dp[i - 1] > 0) {
int j = i - dp[i - 1] - 1;
if (j >= 0 && s[j] == '(') {
dp[i] = dp[i - 1] + 2 + (j > 0 ? dp[j - 1] : 0);
}
}
maxLen = [Link](maxLen, dp[i]);
}
}

return maxLen;
}
}

You might also like