publicListNodeMiddleNode(ListNode head){ListNode slow = head, fast = head;//快慢指针法while(fast !=null&& fast.next !=null){
slow = slow.next;
fast = fast.next.next;}return slow;}
找不同
publiccharFindTheDifference(string s,string t){var cs = s.ToCharArray();
Array.Sort(cs);var ct = t.ToCharArray();
Array.Sort(ct);int i =0;for(; i < cs.Length; i++){if(cs[i]!= ct[i]){return ct[i];}}return ct[i];}
队列实现栈
publicclassMyStack{
Queue<int> queue;/** Initialize your data structure here. */publicMyStack(){
queue =newQueue<int>();}/** Push element x onto stack. */publicvoidPush(int x){var temp =newQueue<int>();
temp.Enqueue(x);var items = queue.ToList();foreach(var item in items){
temp.Enqueue(item);}
queue = temp;}/** Removes the element on top of the stack and returns that element. */publicintPop(){if(!Empty()){return queue.Dequeue();}else{return-1;}}/** Get the top element. */publicintTop(){if(!Empty()){var bill = queue.ToList();return bill[0];}else{return-1;}}/** Returns whether the stack is empty. */publicboolEmpty(){return queue.Count ==0;}}
privateboolisDividing(int num){int temp = num;while(temp !=0){if(temp %10==0)returnfalse;if(num %(temp %10)!=0)returnfalse;else
temp /=10;}returntrue;}public IList<int>SelfDividingNumbers(int left,int right){
List<int> list =newList<int>();for(int i = left; i <= right; i++){if(isDividing(i)==true) list.Add(i);}return list;}
N叉树的前序遍历
var list =newList<int>();if(root ==null)return list;var stack =newStack<Node>();
stack.Push(root);//确定序列中的任何元素是否存在或满足条件。while(stack.Any()){var pop = stack.Pop();
list.Add(pop.val);if(pop.children !=null){for(var i = pop.children.Count()-1; i >=0; i--)
stack.Push(pop.children[i]);}}return list;
N叉树的后序遍历
public IList<int>Postorder(Node root){if(root ==null)returnnewList<int>();var list =newList<int>();if(root.children !=null){foreach(var child in root.children){
list.AddRange(Postorder(child));}}
list.Add(root.val);return list;}
单值二叉树
publicboolIsUnivalTree(TreeNode root){
List<int> list =Bianli(root);int k =0;if(list.Count >0){
k = list[0];}return list.All(n => n == k);}public List<int>Bianli(TreeNode root){
List<int> list =newList<int>();if(root !=null){
list.Add(root.val);}else{return list;}
list.AddRange(Bianli(root.left));
list.AddRange(Bianli(root.right));return list;}
N叉树的最大深度
publicintMaxDepth(Node root){if(root ==null)return0;var res =0;if(root.children !=null&& root.children.Count !=0){foreach(var child in root.children){var depth =MaxDepth(child);
res = Math.Max(res, depth);}}return res +1;}
publicintTitleToNumber(string s){/*方法一
int n = s.Length;
int res = 0;
int temp = 1;
for(int i = n; i >= 1; i--)
{
res += (s[i - 1] - 'A' + 1) * temp;
temp *= 26;
}
return res;
*/int i = s.Length -1;return s.ToCharArray().Select(x => x -'A'+1).Select(x =>(int)(x * Math.Pow(26, i--))).Sum();}
最小差值 I
publicintSmallestRangeI(int[] A,int K){
Array.Sort(A);int sum =(A[A.Length-1]- A[0]-(2* K));if(sum <=0)return0;elsereturn sum;}
转置矩阵
publicint[][]Transpose(int[][] A){var m = A.Length;var n = A[0].Length;var result =newint[n][];for(int i =0; i < n; i++){
result[i]=newint[m];}for(var i =0; i < n; i++){for(var j =0; j < m; j++){
result[i][j]= A[j][i];}}return result;}
子域名访问计数
public IList<string>SubdomainVisits(string[] cpdomains){
Dictionary<string,int> dict =newDictionary<string,int>();foreach(var item in cpdomains){string[] sArray = item.Split(' ');string ss = sArray[1];int count = Convert.ToInt32(sArray[0]);if(!dict.ContainsKey(ss)){
dict.Add(ss, count);}else{
dict[ss]+= count;}while(ss.IndexOf('.')!=-1){string temp = ss.Substring(ss.IndexOf('.')+1);if(!dict.ContainsKey(temp)){
dict.Add(temp, count);}else{
dict[temp]+= count;}
ss = temp;}}return dict.Select(n => n.Value.ToString()+" "+ n.Key).ToList();}
交替位二进制数
publicboolHasAlternatingBits(int n){//&全一才一,| 有一就一,箭头方向指向移动方向,右边值移动多少位int temp = n ^(n >>1);return(temp &(temp +1))==0;}