博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Merge 2 given sorted interval sequences
阅读量:5814 次
发布时间:2019-06-18

本文共 3907 字,大约阅读时间需要 13 分钟。

  hot3.png

亚马逊机试题:Merge 2 given sorted interval sequences

Given 2 sorted interval sequences. Implement a function to get the merged sorted interval sequence from the input. If there's overlapped intervals, please merge them as new interval.

The Given 2 sorted interval sequences as string arrays. Eatch element of the array is a string that indicats an interval. The boundaries of each interval are separated by comma (no space allowed).

Sample 1:
Input:
["1,2", "6,7"]
["3,5"]
Output:
["1,2", "3,5", "6,7"]
Sample 2:
Input:
["1,2", "3,5"]

["4,6"]

Output:

["1,2", "3,6"]

//以下为c#代码,方法比较愚钝,以后多加练习。

static string[] mergeSortIntervals(string[] intervalSequence1, string[] intervalSequence2)        {            int iLength1 = intervalSequence1.Length;            int iLength2 = intervalSequence2.Length;            string[] retVal = new string[iLength1 + iLength2];            int Flag = 0;            int i = 0;            int j = 0;            int One_1 = Int32.Parse(intervalSequence1[0].Substring(0, 1));            int One_2 = Int32.Parse(intervalSequence1[0].Substring(2, 1));            int Two_1 = Int32.Parse(intervalSequence2[0].Substring(0, 1));            int Two_2 = Int32.Parse(intervalSequence2[0].Substring(2, 1));            for (i = 0; i < iLength1; )            {                if (One_1 < Two_1 && One_2 < Two_1)                {                    retVal[Flag] = intervalSequence1[i];                    Flag++;                    i++;                }                else if (One_1 < Two_1 && One_2 >= Two_1 && One_2 <= Two_2)                {                    retVal[Flag] = intervalSequence1[i].Substring(0, 1) + "," + intervalSequence2[j].Substring(2, 1);                    i++;                    j++;                    Flag++;                }                else if (One_1 < Two_1 && One_2 > Two_2)                {                    retVal[Flag] = intervalSequence1[i];                    i++;                    j++;                    Flag++;                }                else if (One_1 >= Two_1 && One_1 <= Two_2 && One_2 <= Two_2)                {                    retVal[Flag] = intervalSequence2[j];                    i++;                    j++;                    Flag++;                }                else if (One_1 >= Two_1 && One_1 <= Two_2 && One_2 > Two_2)                {                    retVal[Flag] = intervalSequence2[j].Substring(0, 1) + "," + intervalSequence1[i].Substring(2, 1);                    i++;                    j++;                    Flag++;                }                else if (One_1 > Two_2)                {                    retVal[Flag] = intervalSequence2[j];                    j++;                    Flag++;                }                if (i == iLength1 || j == iLength2)                {                    break;                }                One_1 = Int32.Parse(intervalSequence1[i].Substring(0, 1));                One_2 = Int32.Parse(intervalSequence1[i].Substring(2, 1));                Two_1 = Int32.Parse(intervalSequence2[j].Substring(0, 1));                Two_2 = Int32.Parse(intervalSequence2[j].Substring(2, 1));            }            if (i != iLength1)            {                for (int k = i; k < iLength1; k++)                {                    retVal[Flag] = intervalSequence1[k];                    Flag++;                }            }            if (j != iLength2)            {                for (int k = j; k < iLength2; k++)                {                    retVal[Flag] = intervalSequence2[k];                    Flag++;                }            }            //去除数组中空的字符            List
strs = new List
(); foreach (string s in retVal) { if (!string.IsNullOrEmpty(s)) strs.Add(s); } string[] result = strs.ToArray(); return result; }

转载于:https://my.oschina.net/fudum/blog/163527

你可能感兴趣的文章
jenkins 没有maven选项,怎么办
查看>>
mysql join优化原理
查看>>
Css 描点
查看>>
别人恃才自傲,你却虚怀若谷。——保持谦虚
查看>>
计算机网络起源 网络发展简介(一)
查看>>
AT3908 Two Integers
查看>>
渐变色文字
查看>>
JS找出一串字符中出现最多的字符
查看>>
C++ 0X 新特性实例(比较常用的) (转)
查看>>
node生成自定义命令(yargs/commander)
查看>>
各种非算法模板
查看>>
mahout learning 代码示例
查看>>
node-express项目的搭建并通过mongoose操作MongoDB实现增删改查分页排序(四)
查看>>
PIE.NET-SDK插件式二次开发文档
查看>>
如何创建Servlet
查看>>
.NET 设计规范--.NET约定、惯用法与模式-2.框架设计基础
查看>>
Linux单用户CS模型TCP通讯完全注释手册
查看>>
win7 64位+Oracle 11g 64位下使用 PL/SQL Developer 的解决办法
查看>>
to Generate Armstrong Numbers
查看>>
配置管理
查看>>