Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
delevinyingc committed Mar 14, 2021
1 parent ad26f6f commit a682d20
Show file tree
Hide file tree
Showing 7 changed files with 437 additions and 34 deletions.
Empty file.
90 changes: 90 additions & 0 deletions 01.计算机科学/算法/leetcode/1184.公交站间的距离.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* @lc app=leetcode.cn id=1184 lang=csharp
*
* [1184] 公交站间的距离
*
* https://leetcode-cn.com/problems/distance-between-bus-stops/description/
*
* algorithms
* Easy (57.84%)
* Likes: 39
* Dislikes: 0
* Total Accepted: 10.5K
* Total Submissions: 18.1K
* Testcase Example: '[1,2,3,4]\n0\n1'
*
* 环形公交路线上有 n 个站,按次序从 0 到 n - 1 进行编号。我们已知每一对相邻公交站之间的距离,distance[i] 表示编号为 i
* 的车站和编号为 (i + 1) % n 的车站之间的距离。
*
* 环线上的公交车都可以按顺时针和逆时针的方向行驶。
*
* 返回乘客从出发点 start 到目的地 destination 之间的最短距离。
*
*
*
* 示例 1:
*
*
*
* 输入:distance = [1,2,3,4], start = 0, destination = 1
* 输出:1
* 解释:公交站 0 和 1 之间的距离是 1 或 9,最小值是 1。
*
*
*
* 示例 2:
*
*
*
* 输入:distance = [1,2,3,4], start = 0, destination = 2
* 输出:3
* 解释:公交站 0 和 2 之间的距离是 3 或 7,最小值是 3。
*
*
*
*
* 示例 3:
*
*
*
* 输入:distance = [1,2,3,4], start = 0, destination = 3
* 输出:4
* 解释:公交站 0 和 3 之间的距离是 6 或 4,最小值是 4。
*
*
*
*
* 提示:
*
*
* 1 <= n <= 10^4
* distance.length == n
* 0 <= start, destination < n
* 0 <= distance[i] <= 10^4
*
*
*/

// @lc code=start
public class Solution {
public int DistanceBetweenBusStops (int[] distance, int start, int destination) {
if (start == destination) return 0;
int min = Math.Min (start, destination);
int max = Math.Max (start, destination);
int dis1 = 0;
int dis2 = 0;
for (int i = min; i < max; i++) {
dis1 += distance[i];
}

int j = max;
while (max != min) {
if (j < distance.Length - 1) {
j++;
} else if (j == distance.Length - 1) {
j = 0;
}
}
}
}
// @lc code=end
21 changes: 17 additions & 4 deletions 01.计算机科学/算法/leetcode/133.克隆图.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,22 @@ public Node(int _val, List<Node> _neighbors) {
*/

public class Solution {
public Node CloneGraph(Node node) {

//TODO:
public Node CloneGraph (Node node) {
if (node == null) return node;
Dictionary<Node, Node> dic = new Dictionary<Node, Node> ();
return dfs (node, dic);
}
}
// @lc code=end

private Node dfs (Node node, Dictionary<Node, Node> dic) {
if (node == null) return node;
if (dic.ContainsKey (node)) return dic[node];
Node copy = new Node (node.val);
dic.Add (node, copy);
foreach (var item in node.neighbors) {
copy.neighbors.Add (dfs (item, dic));
}
return copy;
}
}
// @lc code=end
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* @lc app=leetcode.cn id=138 lang=csharp
*
* [138] 复制带随机指针的链表
*
* https://leetcode-cn.com/problems/copy-list-with-random-pointer/description/
*
* algorithms
* Medium (57.71%)
* Likes: 414
* Dislikes: 0
* Total Accepted: 49.8K
* Total Submissions: 86.2K
* Testcase Example: '[[7,null],[13,0],[11,4],[10,2],[1,0]]'
*
* 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。
*
* 要求返回这个链表的 深拷贝。 
*
* 我们用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:
*
*
* val:一个表示 Node.val 的整数。
* random_index:随机指针指向的节点索引(范围从 0 到 n-1);如果不指向任何节点,则为  null 。
*
*
*
*
* 示例 1:
*
*
*
* 输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
* 输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]
*
*
* 示例 2:
*
*
*
* 输入:head = [[1,1],[2,1]]
* 输出:[[1,1],[2,1]]
*
*
* 示例 3:
*
*
*
* 输入:head = [[3,null],[3,0],[3,null]]
* 输出:[[3,null],[3,0],[3,null]]
*
*
* 示例 4:
*
* 输入:head = []
* 输出:[]
* 解释:给定的链表为空(空指针),因此返回 null。
*
*
*
*
* 提示:
*
*
* -10000 <= Node.val <= 10000
* Node.random 为空(null)或指向链表中的节点。
* 节点数目不超过 1000 。
*
*
*/

// @lc code=start
/*
// Definition for a Node.
public class Node {
public int val;
public Node next;
public Node random;
public Node(int _val) {
val = _val;
next = null;
random = null;
}
}
*/

public class Solution {
public Node CopyRandomList (Node head) {
if (node == null) return node;
Dictionary<Node, List<Node>> dic = new Dictionary<Node, List<Node>> ();
return dfs (node, dic);
}

private Node dfs (Node node, Dictionary<Node, List<Node>> dic) {
if (dic.ContainsKey (node)) return dic[node];
Node copy = new Node (node.val);
dic.Add (node, new List<Node> ());
if (node.next != null) {
dic[node].next = dfs (node.next, dic);
}

if (node.random != null) {
dic[node].random = dfs (node.random, dic);
}
return copy;
}
}
// @lc code=end
70 changes: 70 additions & 0 deletions 01.计算机科学/算法/leetcode/49.字母异位词分组.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* @lc app=leetcode.cn id=49 lang=csharp
*
* [49] 字母异位词分组
*
* https://leetcode-cn.com/problems/group-anagrams/description/
*
* algorithms
* Medium (63.90%)
* Likes: 496
* Dislikes: 0
* Total Accepted: 114.5K
* Total Submissions: 179.2K
* Testcase Example: '["eat","tea","tan","ate","nat","bat"]'
*
* 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
*
* 示例:
*
* 输入: ["eat", "tea", "tan", "ate", "nat", "bat"]
* 输出:
* [
* ⁠ ["ate","eat","tea"],
* ⁠ ["nat","tan"],
* ⁠ ["bat"]
* ]
*
* 说明:
*
*
* 所有输入均为小写字母。
* 不考虑答案输出的顺序。
*
*
*/

// @lc code=start
public class Solution {
public IList<IList<string>> GroupAnagrams (string[] strs) {
Dictionary<string, IList<string>> dic = new Dictionary<string, IList<string>> ();
foreach (var str in strs) {
int[] cs = new int[26];
foreach (var c in str) {
cs[c - 'a']++;
}
StringBuilder sb = new StringBuilder ();
for (int i = 0; i < 26; i++) {
if (cs[i] > 0) {
sb.Append ((char) ('a' + i));
sb.Append (cs[i]);
}
}
string s = sb.ToString ();
if (dic.ContainsKey (s)) {
dic[s].Add (str);
} else {
List<string> list = new List<string> ();
list.Add (str);
dic.Add (s, list.ToList ());
}
}
IList<IList<string>> res = new List<IList<string>> ();
foreach (var item in dic) {
var list = item.Value;
res.Add (list);
}
return res;
}
}
// @lc code=end
82 changes: 52 additions & 30 deletions 01.计算机科学/算法/leetcode/56.合并区间.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,64 @@

// @lc code=start
public class Solution {
//分治算法
//TODO:
public int[][] Merge (int[][] intervals) {
List<int[]> result = new List<int[]> ();
if(intervals.Length == 0)return result.ToArray();
for(int i = 0;i<intervals.Length - 1;i++){
var a = intervals[i];
bool has = false;
for(int j = i+1;j<intervals.Length;j++){
var b = intervals[j];
if(CanMerge(a,b)){
a = MergeFunc1 (a, b);
has = true;
if (intervals.Length == 0) {
return intervals;
}

intervals = intervals.OrderBy (p => p[0]).ToArray ();
List<int[]> list = new List<int[]> ();
for (int i = 0; i < intervals.Length - 1; i++) {
if (intervals[i][1] >= intervals[i + 1][0]) {
intervals[i + 1][0] = intervals[i][0];
if (intervals[i][1] >= intervals[i + 1][1]) {
intervals[i + 1][1] = intervals[i][1];
}
}
if(has == false){
result.Add (a);
} else {
list.Add (intervals[i]);
}
}
return result.ToArray();
list.Add (intervals[intervals.Length - 1]);
int[][] result = list.ToArray ();
return result;
}

private bool CanMerge (int[] a, int[] b) {
if (a == null || b == null) {
return true;
} else if (a[1] < b[0]) {
return false;
} else if (b[1] < a[0]) {
return false;
}
return true;
}
// private void Func (int[][] intervals) {
// List<int[]> result = new List<int[]> ();
// if (intervals.Length == 0) return result.ToArray ();
// for (int i = 0; i < intervals.Length - 1; i++) {
// var a = intervals[i];
// bool has = false;
// for (int j = i + 1; j < intervals.Length; j++) {
// var b = intervals[j];
// if (CanMerge (a, b)) {
// a = MergeFunc1 (a, b);
// has = true;
// }
// }
// if (has == false) {
// result.Add (a);
// }
// }
// return result.ToArray ();
// }

private int[] MergeFunc1 (int[] a, int[] b) {
int min = Math.Min (a[0], b[0]);
int max = Math.Max (a[1], b[1]);
return new int[2] { min, max };
}
// private bool CanMerge (int[] a, int[] b) {
// if (a == null || b == null) {
// return true;
// } else if (a[1] < b[0]) {
// return false;
// } else if (b[1] < a[0]) {
// return false;
// }
// return true;
// }

// private int[] MergeFunc1 (int[] a, int[] b) {
// int min = Math.Min (a[0], b[0]);
// int max = Math.Max (a[1], b[1]);
// return new int[2] { min, max };
// }
}
// @lc code=end
Loading

0 comments on commit a682d20

Please sign in to comment.