【总结】.Net面试题集锦 (二)

一、前面的话

本文的面试题不是很难,这里只是想记录个人的思考过程,另一方面希望有更好的解决办法的大牛留下宝贵的思路,大家共同学习进步。

二、题目

思路:第一步:把一维数组的值和次数存入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()

lambert_li

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     }

注:以下是各位园友对上面题目的补充,我把这些内容加进来~

 

@chinaonl
 1 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三个判断都是比较容易实现的。

 三、总结

这些题目其实很简单,但是一千个读者有一千个哈姆雷特,多样化的解决问题的方式会大大拓宽我们的思路,所以,打开你们的脑洞吧~~

posted @ 2015-01-09 15:14  悠扬的牧笛  阅读(2615)  评论(22编辑  收藏  举报