Skip to content
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
39 changes: 39 additions & 0 deletions Week_02/id_7/LeetCode_101_7.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* @lc app=leetcode.cn id=101 lang=csharp
*
* [101] 对称二叉树
*/

using System.Collections;
/**
* 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 {
// 按层遍历,按顺序:左子树左节点、右子树右节点、左子树右节点、右子树左节点,进队列,然后每次出队列两个,比较是否相等,遇不等就直接返回false
public bool IsSymmetric (TreeNode root) {
Queue que = new Queue ();
que.Enqueue (root);
que.Enqueue (root);
while (que.Count > 0) {
TreeNode nodeL = (TreeNode) que.Dequeue ();
TreeNode nodeR = (TreeNode) que.Dequeue ();
if (nodeL == null && nodeR == null)
continue;
if (nodeL == null || nodeR == null)
return false;
if (nodeL.val != nodeR.val)
return false;
que.Enqueue (nodeL.left);
que.Enqueue (nodeR.right);
que.Enqueue (nodeL.right);
que.Enqueue (nodeR.left);
}
return true;
}
}
45 changes: 45 additions & 0 deletions Week_02/id_7/LeetCode_111_7.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* @lc app=leetcode.cn id=111 lang=csharp
*
* [111] 二叉树的最小深度
*/

using System;
using System.Collections;
using System.Collections.Generic;
/**
* 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 int MinDepth (TreeNode root) {
Stack sk = new Stack ();
if (root == null) {
return 0;
} else {
sk.Push (new KeyValuePair<TreeNode, int> (root, 1));
}
int nDepth = int.MaxValue;
while (sk.Count > 0) {
KeyValuePair<TreeNode, int> kvp = (KeyValuePair<TreeNode, int>) sk.Pop ();
root = kvp.Key;
int nTemp = kvp.Value;
if (root.left == null && root.right == null) {
nDepth = Math.Min (nDepth, nTemp);
}
if (root.left != null) {
sk.Push (new KeyValuePair<TreeNode, int> (root.left, nTemp + 1));
}
if (root.right != null) {
sk.Push (new KeyValuePair<TreeNode, int> (root.right, nTemp + 1));
}
}
return nDepth;
}
}
24 changes: 24 additions & 0 deletions Week_02/id_7/LeetCode_1_7.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* @lc app=leetcode.cn id=1 lang=csharp
*
* [1] 两数之和
*/

using System.Collections.Generic;

public class Solution {
public int[] TwoSum (int[] nums, int target) {
Dictionary<int, int> dicNums = new Dictionary<int, int> ();
for (int i = 0; i < nums.Length; i++) {
if (!dicNums.ContainsKey (target - nums[i])) {
if (!dicNums.ContainsKey (nums[i])) {
dicNums.Add (nums[i], i);
}
} else {
return new int[] { dicNums[target - nums[i]], i };
}
}

return null;
}
}
28 changes: 28 additions & 0 deletions Week_02/id_7/LeetCode_242_7.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* @lc app=leetcode.cn id=242 lang=csharp
*
* [242] 有效的字母异位词
*/
using System.Collections.Generic;

public class Solution {
public bool IsAnagram (string s, string t) {
if (s.Length != t.Length) {
return false;
}

int[] arrRes = new int[26];
for (int i = 0; i < s.Length; i++) {
arrRes[s[i] - 'a']++;
arrRes[t[i] - 'a']--;
}

for (int i = 0; i < 26; i++) {
if (arrRes[i] != 0) {
return false;
}
}

return true;
}
}
24 changes: 24 additions & 0 deletions Week_02/id_7/LeetCode_3_7.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* @lc app=leetcode.cn id=3 lang=csharp
*
* [3] 无重复字符的最长子串
*/
using System;
using System.Collections.Generic;

public class Solution {
public int LengthOfLongestSubstring (string s) {
Dictionary<char, int> dic = new Dictionary<char, int> ();
int ans = 0;
for (int left = 0, right = 0; right < s.Length; right++) {
if (dic.ContainsKey (s[right])) {
left = Math.Max (left, dic[s[right]]);
dic[s[right]] = right + 1;
} else {
dic.Add (s[right], right + 1);
}
ans = Math.Max (ans, right - left + 1);
}
return ans;
}
}
71 changes: 71 additions & 0 deletions Week_02/id_7/LeetCode_726_7.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* @lc app=leetcode.cn id=726 lang=csharp
*
* [726] 原子的数量
* a-z:97-122
* A-Z:65-90
* 0-9:48-57
*/
using System;
using System.Collections;
using System.Collections.Generic;

public class Solution {
public string CountOfAtoms (string formula) {
Stack<Dictionary<string, int>> skRes = new Stack<Dictionary<string, int>> ();
skRes.Push (new Dictionary<string, int> ());
int i = 0;
while (i < formula.Length) {
if (formula[i].Equals ('(')) {
skRes.Push (new Dictionary<string, int> ());
i++;
} else if (formula[i].Equals (')')) {
int start = ++i;
while (i < formula.Length && formula[i] >= 48 && formula[i] <= 57) i++;
int multiplicity = i > start? Convert.ToInt32 (formula.Substring (start, i - start)) : 1;
if (multiplicity != 1) {
Dictionary<string, int> dicTemp = skRes.Pop ();
Dictionary<string, int> dicTemp2 = skRes.Peek ();
foreach (var name in dicTemp.Keys) {
if (dicTemp2.ContainsKey (name)) {
dicTemp2[name] += dicTemp[name] * multiplicity;
} else {
dicTemp2.Add (name, dicTemp[name] * multiplicity);
}
}
}
} else {
int start = i++;
while (i < formula.Length && formula[i] >= 97 && formula[i] <= 122) i++;
string name = formula.Substring (start, i - start);

start = i;
while (i < formula.Length && formula[i] >= 48 && formula[i] <= 57) i++;
int multiplicity = i > start? Convert.ToInt32 (formula.Substring (start, i - start)) : 1;

Dictionary<string, int> dicTemp = skRes.Peek ();
if (dicTemp.ContainsKey (name)) {
dicTemp[name] = dicTemp[name] + multiplicity;
} else {
dicTemp.Add (name, multiplicity);
}
}
}

string strRes = "";
Dictionary<string, int> dicRes = skRes.Peek ();
List<string> lst = new List<string> ();
foreach (var name in dicRes.Keys) {
lst.Add (name);
}
lst.Sort ();
for (int k = 0; k < lst.Count; k++) {
if (dicRes[lst[k]] > 1)
strRes += (lst[k] + dicRes[lst[k]]);
else
strRes += (lst[k] + "");
}

return strRes;
}
}
30 changes: 30 additions & 0 deletions Week_02/id_7/LeetCode_783_7.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* @lc app=leetcode.cn id=783 lang=csharp
*
* [783] 二叉搜索树结点最小距离
*/

using System;
/**
* 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 {
int m_nRsult = int.MaxValue;
int m_nLastv = -999999;
public int MinDiffInBST (TreeNode root) {
if (root.left != null)
MinDiffInBST (root.left);
m_nRsult = Math.Min (m_nRsult, root.val - m_nLastv);
m_nLastv = root.val;
if (root.right != null)
MinDiffInBST (root.right);

return m_nRsult;
}
}
58 changes: 58 additions & 0 deletions Week_03/id_7/703.数据流中的第k大元素.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* @lc app=leetcode.cn id=703 lang=csharp
*
* [703] 数据流中的第K大元素
*/
using System;

public class KthLargest {
int[] heap;
int count;
int capacity;

public KthLargest (int k, int[] nums) {
heap = new int[nums.Length];
capacity = k;
count = 0;
for (int i = 0; i < nums.Length; i++) {
Add (nums[i]);
}
}

public int Add (int val) {
if (count < capacity) {
heap[count++] = val;
int i = count - 1;
while (i > 0) {
if (heap[i] < heap[(i - 1) / 2]) {
int temp = heap[i];
heap[i] = heap[(i - 1) / 2];
heap[(i - 1) / 2] = temp;
}
i = (i - 1) / 2;
}
} else if (val > heap[0]) {
heap[0] = val;
int i = 0;
while (i * 2 + 1 < count) {
i = i * 2 + 1;
if (i + 1 < count && heap[i] > heap[i + 1]) {
i++;
}
if (heap[(i - 1) / 2] > heap[i]) {
int temp = heap[(i - 1) / 2];
heap[(i - 1) / 2] = heap[i];
heap[i] = temp;
}
}
}

return heap[0];
}
}

/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest obj = new KthLargest(k, nums);
* int param_1 = obj.Add(val);
*/
Loading