一、前面的話
本文的面試題不是很難,這里只是想記錄個人的思考過程,另一方面希望有更好的解決辦法的大牛留下寶貴的思路,大家共同學習進步。
二、題目
思路:第一步:把一維數組的值和次數存入Dictionary中;
第二步:獲取Dictionary中大於等於指定值的元素存入List;
第三步:將結果存入int數組並返回結果
1 //聲明數組中重復次數大於等於指定次數的接口 2 public interface IDuplicationFinder 3 { 4 int[] FindDuplication(int[] input, uint minTimes); 5 }
1 //檢測數組類 2 public class DuplicationFinder : IDuplicationFinder 3 { 4 int[] result=null; 5 Dictionary<int, int> map = new Dictionary<int, int>(); 6 List<int> list = new List<int>(); 7 public int[] FindDuplication(int[] input, uint minTimes) 8 { 9 int length = input.Length; 10 int times = 1; 11 int count = 0; 12 //第一步:把一維數組的值和次數存入Dictionary中 13 for (int i = 0; i < length; i++) 14 { 15 if (!map.ContainsKey(input[i])) 16 { 17 times = 1; 18 map.Add(input[i], times); 19 } 20 else 21 { 22 times = map[input[i]]; 23 times++; 24 map.Remove(input[i]); 25 map.Add(input[i], times); 26 } 27 } 28 //第二步:獲取Dictionary中大於等於指定值的元素存入List 29 foreach (KeyValuePair<int,int> item in map) 30 { 31 if (item.Value >= minTimes) 32 { 33 list.Add(item.Key); 34 } 35 } 36 count = list.Count; 37 result = new int[count]; 38 //第三步:將結果存入int數組並返回結果 39 for (int i = 0; i < count; i++) 40 { 41 result[i] = list[i]; 42 } 43 return result; 44 } 45 }
注:以下是各位園友對上面題目的補充,我把這些內容加進來~
@浩GE 順便在寫下第一題使用Linq的解法,特殊情況0就不寫出來了。1 int minTimes = 2; 2 3 var array = new int[] { 1, 1, 1, 2, 5, 5 }; 4 5 var distinctArray = array.Distinct(); 6 7 var results = distinctArray 8 9 .ToDictionary(num => num, num => array.Count(i => i == num)) 10 11 .Where(p => p.Value >= minTimes) 12 13 .Select(p => p.Key) 14 15 .ToArray();1 public static void Main() 3 { 4 var array = new int[] { 1, 1, 1, 2, 5, 5 }; 5 var q = FindDuplication(array, 2); 6 } 7 8 public static int[] FindDuplication(int[] input, uint minTimes) 10 { 11 12 return (from c in input 13 14 group c by c into g 15 16 let count = g.Count() 17 18 where count >= minTimes 19 20 select g.Key).ToArray(); 21 }@??? 第一題代碼可以優化
1 Dictionary<int, int> dict = new Dictionary<int, int>(); 2 for (int i = 0; i < inputs.Length; i++) 3 { 4 if (!dict.ContainsKey(inputs[i])) 5 dict.Add(inputs[i], 1); 6 else 7 dict[inputs[i]]++; 8 } 9 List<int> list = new List<int>(); 10 IEnumerable<KeyValuePair<int, int>> tmp = dict.Where(w => w.Value >= minTimes); 11 foreach (KeyValuePair<int, int> item in tmp) 12 list.Add(item.Key); 13 return list.ToArray();1 var result=array 2 .GroupBy(c=>c).Select(c=>new {c.Key,Num=c.Count()}) 3 .Where(c=>c.Num>=minTimes) 4 .Select(c=>c.Key) 5 .ToArray()1 public int[] FindDuplication(int[] input, uint minTimes) 2 { 3 return input.GroupBy(m => m).Where(g=>g.Count() >= minTimes).Select(g=>g.Key).ToArray(); 4 }
思路:第一步:先把單句反轉;
第二步:把反轉后的四句組合;
1 //聲明字符反轉接口 2 public interface IstringInverter 3 { 4 string PiecewiseInvert(string input); 5 }
1 //字符串反轉類 2 public class stringInverter:IstringInverter 3 { 4 //第二步:把反轉后的四句組合 5 public string PiecewiseInvert(string input) 6 { 7 StringBuilder builder = new StringBuilder(); 8 string[] poemSingle=input.Trim().Split(new char[2]{',','。'}); 9 int i = 0; 10 foreach (string item in poemSingle) 11 { 12 i++; 13 if (!string.IsNullOrEmpty(item)) 14 { 15 builder.Append(InversePoem(item)); 16 if (i % 2==0) 17 { 18 builder.Append('。'); 19 } 20 else 21 { 22 builder.Append(','); 23 } 24 } 25 } 26 return builder.ToString(); 27 } 28 //第一步:先把單句反轉 29 public string InversePoem(string singlePoem) 30 { 31 char[] ch = singlePoem.ToCharArray(); 32 int length = ch.Length; 33 char temp; 34 for (int i = 0; i < length/2; i++) 35 { 36 temp=ch[i]; 37 ch[i] = ch[length - i-1]; 38 ch[length - i-1] = temp; 39 } 40 return new String(ch); 41 } 42 }
注:以下是各位園友對上面題目的補充,我把這些內容加進來~
@ chinaonl1 const char FLAG1 = ','; 2 const char FLAG2 = '。'; 3 4 static string PicewiseInvert(string input) 5 { 6 var result = new StringBuilder(); 7 for (int i = 0, index = 0; i < input.Length; i++){ 8 if (input[i].Equals(FLAG1) || input[i].Equals(FLAG2){ 9 for (var prev = i - 1; prev > index - 1; prev--){ 10 result.Append(input[prev]); 11 } 12 index = i + 1; 13 result.Append(input[i]); 14 } 15 } 16 return result.ToString(); 17 }@ 菜刀和板磚
1 var str = ",白日依山盡,黃河入海流。欲窮千里目,,更上一層樓。"; 2 var signs = str.Where(c => c == ',' || c == '。').ToArray(); 3 var array = str.Trim().Split(new char[2] { ',', '。' }); 4 var result = string.Join("", 5 array.Select((c, i) => string.Join("", c.Reverse()) + (i == signs.Length ? "" : signs[i].ToString())));
思路:這個問題類似於形狀間的碰撞檢測,我覺得大致上可以分兩類進行討論,一類是矩形的四個頂點至少有一個在園內,這個很好理解,另一類就是矩形完全包圍圓形,看到知乎上有一個比較好的回答,在此給出鏈接,可供參考,怎樣判斷平面上一個矩形和一個圓形是否有重疊?但是我想以一個更直觀更好理解的方式來解決這個問題,目前第二種情形還沒有仔細考慮,如果哪位園友有比較好的idea可以評論,我會加進來。
1 //聲明坐標結構體 2 public struct Point 3 { 4 public double X; 5 public double Y; 6 }
1 //圓形類 2 public class Circle 3 { 4 private Point center; 5 private double radius; 6 public Point Center 7 { 8 get { return center; } 9 set { center = value; } 10 } 11 public double Radius 12 { 13 get { return radius; } 14 set { radius = value; } 15 } 16 public Circle(Point center,double radius) 17 { 18 this.Center = center; 19 this.Radius = radius; 20 } 21 //判斷矩形的四個頂點是否在圓的范圍內 22 public bool IsRectangleRepeate(Rectangle rec) 23 { 24 bool result = false; 25 List<Point> recPoints = rec.Corners; 26 foreach (Point item in recPoints) 27 { 28 if (IsContainPoint(item)) 29 { 30 result = true; 31 break; 32 } 33 } 34 return result; 35 } 36 //判斷某個點是否在圓的范圍內 37 private bool IsContainPoint(Point point) 38 { 39 bool result = false; 40 double distance = Math.Sqrt(Math.Pow((point.X - this.Center.X), 2) + Math.Pow((point.Y - this.Center.Y), 2)); 41 result = distance < this.radius ? true : false; 42 return result; 43 } 44 45 //聲明:還有一種情形尚未考慮在內,就是當矩形完全包圍圓的時候 46 }
1 //矩形類 2 public class Rectangle 3 { 4 private Point startPos; 5 private double width; 6 private double height; 7 private List<Point> corners; 8 public Point StartPos 9 { 10 get { return startPos; } 11 set { startPos = value; } 12 } 13 public double Width 14 { 15 get { return width; } 16 set { width = value; } 17 } 18 public double Height 19 { 20 get { return height; } 21 set { height = value; } 22 } 23 public List<Point> Corners 24 { 25 get { return corners; } 26 set { corners = value; } 27 } 28 public Rectangle(Point startPos,double width,double height) 29 { 30 this.StartPos = startPos; 31 this.Width = width; 32 this.Height = height; 33 GetCorners(); 34 } 35 36 public List<Point> GetCorners() 37 { 38 Corners = new List<Point>(); 39 Point leftTop = new Point() { X=StartPos.X,Y=StartPos.Y}; 40 Point rightTop = new Point() { X = StartPos.X+Width, Y = StartPos.Y }; 41 Point leftBottom = new Point() { X = StartPos.X, Y = StartPos.Y+Height }; 42 Point rightBottom = new Point() { X = StartPos.X + Width, Y = StartPos.Y+Height }; 43 Corners.Add(leftTop); 44 Corners.Add(leftBottom); 45 Corners.Add(rightTop); 46 Corners.Add(rightBottom); 47 return Corners; 48 } 49 }
注:以下是各位園友對上面題目的補充,我把這些內容加進來~
@ 愛編程的大叔
1、將圓心設定為原點(0,0),矩形坐標進行相應變換。
2、判斷矩形的中心坐標在哪個象限(1、2、3、4)上。
3、以1象限為例,矩形與圓形相交的只有三種可能
a. 左下角位於圓內部, 坐標:Left, bottom在圓形內部
b. 下邊切部份圓, 坐標:0,R在矩形內部
c. 左下角坐標位於第三象限(與a判斷部份重疊)
4、a\b\c三個判斷都是比較容易實現的。
三、總結
這些題目其實很簡單,但是一千個讀者有一千個哈姆雷特,多樣化的解決問題的方式會大大拓寬我們的思路,所以,打開你們的腦洞吧~~
還沒有買到回家的票,腫么破 %>_<%