publicintBinaryGap(int N){int[] A =newint[32];int t =0;for(int i =0; i <32;++i)if(((N >> i)&1)!=0)
A[t++]= i;int ans =0;for(int i =0; i < t -1;++i)
ans = Math.Max(ans, A[i +1]- A[i]);return ans;}
最大三角形面积
publicdoubleLargestTriangleArea(int[][] points){double area =0d;for(int i =0; i < points.Length; i++){for(int j = i; j < points.Length -1; j++){for(int x = j; x < points.Length; x++){double x1 = points[i][0];double y1 = points[i][1];double x2 = points[j][0];double y2 = points[j][1];double x3 = points[x][0];double y3 = points[x][1];
area = Math.Max(area, Math.Abs((x1 - x3)*(y2 - y3)-(x2 - x3)*(y1 - y3)));}}}return area /2;}
设计哈希映射
publicclassMyHashMap{int[] map =newint[1000000];/** Initialize your data structure here. */publicMyHashMap(){for(int i =0; i < map.Length; i++){
map[i]=-1;}}/** value will always be non-negative. */publicvoidPut(int key,intvalue){
map[key]=value;}/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */publicintGet(int key){return map[key];}/** Removes the mapping of the specified value key if this map contains a mapping for the key */publicvoidRemove(int key){
map[key]=-1;}}
重新排列日志文件
//判断是否是一个纯数字publicboolIsNumeric(string str)//接收一个string类型的参数,保存到str里{if(str ==null|| str.Length ==0)//验证这个参数是否为空returnfalse;//是,就返回FalseASCIIEncoding ascii =newASCIIEncoding();//new ASCIIEncoding 的实例byte[] bytestr = ascii.GetBytes(str);//把string类型的参数保存到数组里foreach(byte c in bytestr)//遍历这个数组里的内容{if(c <48|| c >57)//判断是否为数字{returnfalse;//不是,就返回False}}returntrue;//是,就返回True}publicstring[]ReorderLogFiles(string[] logs){
List<string> shuziList =newList<string>();
List<string> zimuList =newList<string>();var c =from str in logs
whereIsNumeric(str.Split(' ')[1])==trueselect str;
shuziList.AddRange(c);var c2 =from str in logs
let jiujiu = str.Split(' ')let cw = jiujiu[1]whereIsNumeric(cw)==falseselect str;
zimuList.AddRange(c2);
zimuList.Sort((a, b)=> a.Substring(a.IndexOf(" ")+1).CompareTo(b.Substring(b.IndexOf(" ")+1)));
zimuList.AddRange(shuziList);return zimuList.ToArray();}
publicintNumberOfBoomerangs(int[,] points){var res =0;for(var i =0; i < points.GetLength(0);++i){var dic =newDictionary<double,int>();for(var j =0; j < points.GetLength(0);++j){var a = points[i,0]- points[j,0];var b = points[i,1]- points[j,1];var c = a * a + b * b;if(dic.ContainsKey(c)){
dic[c]++;}else{
dic[c]=1;}}foreach(var item in dic){
res += item.Value *(item.Value -1);}}return res;}
验证外星语词典
publicint[] ordArray =newint[26];publicboolIsAlienSorted(string[] words,string order){for(int i =0; i <26; i++){char alpha = order.ElementAt(i);
ordArray[alpha -97]= i;}for(int i =1; i < words.Length; i++){if(!IsOrder(words[i -1], words[i]))returnfalse;}returntrue;// dic = order.ToDictionary<char, int>(Sk);//string[] words2 = (string[])words.Clone();//int minLeng = words.Min(str => str.Length);//Sort(words2);//var temp = from str in words2// orderby str.Sum(chaa => dic.Where(q => q.Value == chaa).Select(q => q.Key).ToArray()[0]) descending// select str;//string[] words3 = temp.ToArray();}privateboolIsOrder(string word1,string word2){for(int i =0; i < word1.Length && i < word2.Length; i++){char ch1 = word1.ElementAt(i);char ch2 = word2.ElementAt(i);if(ordArray[ch1 -97]> ordArray[ch2 -97])returnfalse;elseif(ordArray[ch1 -97]< ordArray[ch2 -97])returntrue;}if(word1.Length > word2.Length)returnfalse;returntrue;}
递增顺序查找树
public List<int>Go(TreeNode root){
List<int> list =newList<int>();if(root ==null)return list;
list.AddRange(Go(root?.left));
list.Add(root.val);
list.AddRange(Go(root?.right));return list;}publicTreeNodeIncreasingBST(TreeNode root){if(root ==null)returnnull;TreeNode sk;
List<int> k =Go(root);
sk =newTreeNode(k[0]);TreeNode bill = sk;
k.RemoveAt(0);while(k.Count >0){int val = k[0];
sk.right =newTreeNode(val);
sk = sk.right;
k.RemoveAt(0);}return bill;}
旋转数字
publicintRotatedDigits(int N){int c =0;String s;for(int i =2; i <= N; i++){
s = i.ToString();if(s.IndexOf("3")!=-1|| s.IndexOf("4")!=-1|| s.IndexOf("7")!=-1){continue;}if(s.IndexOf("2")!=-1|| s.IndexOf("5")!=-1|| s.IndexOf("6")!=-1|| s.IndexOf("9")!=-1){
c++;}}return c;}
最短完整词
publicstringShortestCompletingWord(string licensePlate,string[] words){string lp = licensePlate.ToLower();StringBuilder sb =newStringBuilder();for(int i =0; i < lp.Length; i++){if(lp[i]>='a'&& lp[i]<='z'){
sb.Append(lp[i]);}}string lp2 = sb.ToString();var c =from p in words
whereIsStr(lp2, p)orderby p.Lengthselect p;var bb = c.ToArray();if(bb.Length !=0){return c.ToArray()[0];}else{returnnull;}}//m是过滤了的publicboolIsStr(string lp2,string o){StringBuilder sb =newStringBuilder();for(int i =0; i < o.Length; i++){if(lp2.Contains(o[i])){
sb.Append(o[i]);}}string lp3 = sb.ToString();char[] a = lp2.ToArray();char[] b = lp3.ToArray();if(!a.Except(b).Any()&&IsEqulCount(a,b)){returntrue;}else{returnfalse;}}publicboolIsEqulCount(char[] a,char[] b){
Dictionary<char,int> dic =newDictionary<char,int>();for(int i =0; i < a.Length; i++){if(!dic.ContainsKey(a[i])){
dic.Add(a[i],1);}else{
dic[a[i]]+=1;}}
Dictionary<char,int> dic2 =newDictionary<char,int>();for(int i =0; i < b.Length; i++){if(!dic2.ContainsKey(b[i])){
dic2.Add(b[i],1);}else{
dic2[b[i]]+=1;}}
Dictionary<char,int> dic3 =newDictionary<char,int>();foreach(KeyValuePair<char,int> item in dic2){if(dic.ContainsKey(item.Key)){
dic3.Add(item.Key, item.Value);}}return dic3.All(keyandvalue => dic3[keyandvalue.Key]>= dic[keyandvalue.Key]);}
快乐数
publicboolIsHappy(int n){//参考IsHappy2while(n !=1&& n !=4){var sum =0;while(n >0){
sum +=(n %10)*(n %10);
n /=10;}
n = sum;}return n ==1;}
设计哈希集合
publicclassMyHashSet{private List<int> data;/** Initialize your data structure here. */publicMyHashSet(){
data =newList<int>();}publicvoidAdd(int key){if(!data.Contains(key)){
data.Add(key);}}publicvoidRemove(int key){if(data.Contains(key)){
data.Remove(key);}}/** Returns true if this set contains the specified element */publicboolContains(int key){return data.Contains(key);}}
区域和检索 - 数组不可变
publicclassNumArray{publicint[] ks;publicNumArray(int[] nums){
ks = nums;}publicintSumRange(int i,int j){int res =0;for(int k = i; k <= j; k++){
res += ks[k];}return res;}}
检测大写字母
publicboolDetectCapitalUse(string word){if(word.ToUpper().Equals(word))returntrue;if(word.ToLower().Equals(word))returntrue;if(word.ElementAt(0)>='a'&& word.ElementAt(0)<='z')returnfalse;for(int i =1; i < word.Length; i++){if(word.ElementAt(i)>='A'&& word.ElementAt(i)<='Z')returnfalse;}returntrue;}