-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad26f6f
commit a682d20
Showing
7 changed files
with
437 additions
and
34 deletions.
There are no files selected for viewing
Empty file.
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,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 |
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,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 |
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,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 |
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.