forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add csharp solutions to lcof problems (doocs#826)
* 《剑指 Offer(第 2 版)》添加 C# 题解
- Loading branch information
1 parent
c793e10
commit 8dcceb9
Showing
142 changed files
with
3,515 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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
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,16 @@ | ||
public class Solution { | ||
public int FindRepeatNumber(int[] nums) { | ||
int temp; | ||
for (int i = 0; i < nums.Length; i++) { | ||
while (i != nums[i]) { | ||
if (nums[i] == nums[nums[i]]) { | ||
return nums[i]; | ||
} | ||
temp = nums[i]; | ||
nums[i] = nums[temp]; | ||
nums[temp] = temp; | ||
} | ||
} | ||
return -1; | ||
} | ||
} |
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
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,18 @@ | ||
public class Solution { | ||
public bool FindNumberIn2DArray(int[][] matrix, int target) { | ||
if (matrix.Length == 0 || matrix[0].Length == 0) { | ||
return false; | ||
} | ||
int i = 0, j = matrix[0].Length - 1; | ||
while (i < matrix.Length && j >= 0) { | ||
if (target == matrix[i][j]) { | ||
return true; | ||
} else if (target > matrix[i][j]) { | ||
i += 1; | ||
} else { | ||
j -= 1; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
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
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,5 @@ | ||
public class Solution { | ||
public string ReplaceSpace(string s) { | ||
return s.Replace(" ", "%20"); | ||
} | ||
} |
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
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,19 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* public class ListNode { | ||
* public int val; | ||
* public ListNode next; | ||
* public ListNode(int x) { val = x; } | ||
* } | ||
*/ | ||
public class Solution { | ||
public int[] ReversePrint(ListNode head) { | ||
List<int> ans = new List<int>(); | ||
while (head != null) { | ||
ans.Add(head.val); | ||
head = head.next; | ||
} | ||
ans.Reverse(); | ||
return ans.ToArray(); | ||
} | ||
} |
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
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,21 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* public int val; | ||
* public TreeNode left; | ||
* public TreeNode right; | ||
* public TreeNode(int x) { val = x; } | ||
* } | ||
*/ | ||
public class Solution { | ||
public TreeNode BuildTree(int[] preorder, int[] inorder) { | ||
if (preorder.Length == 0) { | ||
return null; | ||
} | ||
TreeNode root = new TreeNode(preorder[0]); | ||
int idx = Array.IndexOf(inorder, root.val); | ||
root.left = BuildTree(preorder[1..(index+1)], inorder[0..idx]); | ||
root.right = BuildTree(preorder[(index+1)..], inorder[(idx+1)..]); | ||
return root; | ||
} | ||
} |
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
Oops, something went wrong.