/** * Your CQueue object will be instantiated and called as such: * CQueue obj = new CQueue(); * obj.AppendTail(value); * int param_2 = obj.DeleteHead(); */ publicclassCQueue { private Stack<int> _a, _b;
publicCQueue() { _a = new Stack<int>(); _b = new Stack<int>(); }
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */ publicclassSolution { privateStack<int> _stack = new Stack<int>();
publicint[] ReversePrint(ListNode head) { while (head != null) { _stack.Push(head.val); head = head.next; }
int[] res = newint[_stack.Count]; for (int i = 0; i < res.Length; i++) res[i] = _stack.Pop(); return res; } }
/* // 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; } } */
publicclassSolution { publicstringReverseLeftWords(string s, int n) { StringBuilder sb = new StringBuilder(); for (int i = n; i < n + s.Length; i++) { sb.Append(s[i % s.Length]); }
publicclassSolution { publicintMissingNumber(int[] nums) { int left = 0, right = nums.Length - 1; while (left <= right) { int mid = right - (right - left) / 2; if (nums[mid] == mid) left = mid + 1; else right = mid - 1; }
Queue<TreeNode> treeNodes = new Queue<TreeNode>(); List<int> res = new List<int>(); treeNodes.Enqueue(root); while (treeNodes.Count > 0) { TreeNode now = treeNodes.Dequeue(); res.Add(now.val); if (now.left != null) treeNodes.Enqueue(now.left); if (now.right != null) treeNodes.Enqueue(now.right); }
publicclassSolution { public IList<IList<int>> LevelOrder(TreeNode root) { var ans = new List<IList<int>>(); if (root == null) return ans; var q = new Queue<TreeNode>(); q.Enqueue(root); while (q.Count > 0) { var level = new List<int>(); for (int cnt = q.Count; cnt > 0; cnt--) { var cur = q.Dequeue(); level.Add(cur.val); if (cur.left != null) q.Enqueue(cur.left); if (cur.right != null) q.Enqueue(cur.right); }
publicclassSolution { public IList<IList<int>> LevelOrder(TreeNode root) { List<IList<int>> list = new List<IList<int>>(); Queue<TreeNode> queue = new Queue<TreeNode>(); if (root != null) { queue.Enqueue(root); }
while (queue.Count > 0) { List<int> tempList = new List<int>(); int count = queue.Count; for (int i = 0; i < count; i++) { TreeNode tree = queue.Dequeue(); if (list.Count % 2 == 0) { tempList.Add(tree.val); } else { tempList.Insert(0, tree.val); }
if (tree.left != null) { queue.Enqueue(tree.left); }
if (tree.right != null) { queue.Enqueue(tree.right); } }
publicclassSolution { publicboolIsSubStructure(TreeNode A, TreeNode B) { if (A == null || B == null) returnfalse; return Helper(A, B) || IsSubStructure(A.left, B) || IsSubStructure(A.right, B); }
publicboolHelper(TreeNode a, TreeNode b) { if (b == null) returntrue; if (a == null || a.val != b.val) returnfalse; return Helper(a.left, b.left) && Helper(a.right, b.right); } }
privateboolHelper(TreeNode a, TreeNode b) { if (a == null && b == null) returntrue; if (a == null || b == null) returnfalse; if (a.val != b.val) returnfalse; return Helper(a.left, b.right) && Helper(a.right, b.left); } }
publicclassSolution { publicintTranslateNum(int num) { string numStr = num.ToString(); int a = 1, b = 1; int c = 0; for (int i = 1; i < numStr.Length; i++) { int number = int.Parse(numStr.Substring(i - 1, 2)); if (number is >= 10and <= 25) c = a + b; else c = b;
publicclassSolution { publicintLengthOfLongestSubstring(string s) { varset = new HashSet<char>(); int ans = 0; for (int l = 0, r = 0; r < s.Length; r++) { while (set.Contains(s[r])) { set.Remove(s[l++]); }
publicclassSolution { public ListNode GetKthFromEnd(ListNode head, int k) { ListNode fast = head; ListNode now = head; for (int i = 0; i < k; fast = fast.next, i++) { if (fast == null) returnnull; }
while (fast != null) { now = now.next; fast = fast.next; }
publicclassSolution { publicint[] Exchange(int[] nums) { int n = nums.Length; int l = 0, r = n - 1; while (l < r) { while (l < r && Check(nums[l])) l++; while (l < r && !Check(nums[r])) r--; (nums[l], nums[r]) = (nums[r], nums[l]); }
publicclassSolution { publicboolExist(char[][] board, string word) { for (int x = 0; x < board.Length; x++) for (int y = 0; y < board[0].Length; y++) if (DFS(x, y, 0, board, word)) returntrue; returnfalse; }
publicboolDFS(int x, int y, int index, char[][] board, string word) { if (x < 0 || x >= board.Length || y < 0 || y >= board[0].Length || board[x][y] != word[index]) returnfalse; if (index == word.Length - 1) returntrue; index++; board[x][y] = '\0';
if (DFS(x + 1, y, index, board, word) || DFS(x - 1, y, index, board, word) || DFS(x, y + 1, index, board, word) || DFS(x, y - 1, index, board, word)) returntrue; board[x][y] = word[index - 1]; returnfalse; } }
publicclassSolution { publicbool[][] visited; publicint m, n, k;
publicintMovingCount(int m, int n, int k) { this.m = m; this.n = n; this.k = k; visited = newbool[m][]; for (int i = 0; i < m; i++) visited[i] = newbool[n]; return DFS(0, 0, 0, 0); }
publicintDFS(int i, int j, int si, int sj) { if (i >= m || j >= n || k < si + sj || visited[i][j]) return0; visited[i][j] = true; return1 + DFS(i + 1, j, GetNumber(i + 1), GetNumber(j)) + DFS(i, j + 1, GetNumber(i), GetNumber(j + 1)); }
publicintGetNumber(int x) { int res = 0; while (x != 0) { res += x % 10; x /= 10; }
/** initialize your data structure here. */ publicMedianFinder() { _minHeap = new PriorityQueue<int, int>(Comparer<int>.Create((x, y) => y - x)); _maxHeap = new PriorityQueue<int, int>(); }
publicvoidAddNum(int num) { if (_minHeap.Count == _maxHeap.Count) { _maxHeap.Enqueue(num, num); int number = _maxHeap.Dequeue(); _minHeap.Enqueue(number, number); } else { _minHeap.Enqueue(num, num); int number = _minHeap.Dequeue(); _maxHeap.Enqueue(number, number); } }