我的面试(七)
昨天参加了公司推荐的onsite第二轮面试,考官出了一道题,没答出最优的方法。今天试了试感觉效率还是很重要的。笨方法和较优方法间的差距在10,000级有1s多到了100,000级竟然有81.296s这样大的差距。真是不敢想象。看来以后真的要多注意效率问题。
不注意效率的笨方法对于系统来说真是毁灭!
考题奉上:
两组有序数列,请计算他们不共有数据的个数,例如:
input1:1 1 2 2 3 3 5 6 6
input2:0 1 3 3 3 4
output:5(即0 2 4 5 6一共5个)
两种方法:
1
static public int GetSameUniqueNumberCount(List<int> sourceList1,List<int> sourceList2)
{
int i = 0;
int j = 0;
List<int> resultList = new List<int>();
while (i < sourceList1.Count || j < sourceList2.Count)
{
if (i >= sourceList1.Count)
{
if (!resultList.Contains(sourceList2[j])) resultList.Add(sourceList2[j]);
j++;
}
else if (j >= sourceList2.Count)
{
if (!resultList.Contains(sourceList1[i])) resultList.Add(sourceList1[i]);
i++;
}
else if (sourceList1[i] < sourceList2[j])
{
if (!resultList.Contains(sourceList1[i])) resultList.Add(sourceList1[i]);
i++;
}
else if (sourceList1[i] == sourceList2[j])
{
while (i < sourceList1.Count - 1)
{
if (sourceList1[i] != sourceList1[++i]) break;
}
while (j < sourceList2.Count - 1)
{
if (sourceList2[j] != sourceList2[++j]) break;
}
if (i == sourceList1.Count - 1) i++;
if (j == sourceList2.Count - 1) j++;
}
else if (sourceList1[i] > sourceList2[j] )
{
if (!resultList.Contains(sourceList2[j])) resultList.Add(sourceList2[j]);
j++;
}
}
return resultList.Count;
}
这个方法比较快
2.
static public int GetSameUniqueNumberCount2(List<int> sourceList1, List<int> sourceList2)
{
List<int> resultList = new List<int>();
for (int i = 0; i < sourceList1.Count; i++)
{
if (!sourceList2.Contains(sourceList1[i]) &&!resultList.Contains(sourceList1[i]))
{
resultList.Add(sourceList1[i]);
}
}
for (int j = 0; j < sourceList2.Count; j++)
{
if (!sourceList1.Contains(sourceList2[j]) && !resultList.Contains(sourceList2[j]))
{
resultList.Add(sourceList2[j]);
}
}
return resultList.Count;
}
这是最直白的方法,也是给系统带来毁灭的方法100,000级就已经有明显的用户体验下降的感觉了再高一级就肯定认为是死机啦
下边是所有的代码(包括100,000的测试用例)
不注意效率的笨方法对于系统来说真是毁灭!
考题奉上:
两组有序数列,请计算他们不共有数据的个数,例如:
input1:1 1 2 2 3 3 5 6 6
input2:0 1 3 3 3 4
output:5(即0 2 4 5 6一共5个)
两种方法:
1
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
2.
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
下边是所有的代码(包括100,000的测试用例)
![](/Images/OutliningIndicators/ContractedBlock.gif)
posted on 2007-11-02 16:16 gyhanonline 阅读(427) 评论(5) 编辑 收藏 举报