Exemple #1
0
        public DataResult <NumRange> GetNumRange(string NumRangeCode)
        {
            try
            {
                using (var db = new SocietyDBEntities())
                {
                    NumRange objNumRange = db.NumRanges.Where(x => x.numRangeCode == NumRangeCode && x.isDeleted == false).FirstOrDefault();

                    if (objNumRange != null)
                    {
                        var NewNumber = objNumRange.currentNumber + 1;

                        if (NewNumber > objNumRange.startNumber && NewNumber < objNumRange.endNumber)
                        {
                            objNumRange.currentNumber   = NewNumber;
                            db.Entry(objNumRange).State = System.Data.EntityState.Modified;
                            db.SaveChanges();

                            var NewNumberString = NewNumber.ToString().PadLeft(8, '0');
                            NewNumberString = NumRangeCode + NewNumberString;

                            Result.StringResult = NewNumberString;
                            Result.Message      = "NumRange Generated Successfully.";
                            Result.MsgType      = "S";
                        }
                        else
                        {
                            Result.Message = "Numrange Exceeds.Kindly Contact Administration.";
                            Result.MsgType = "E";
                        }
                    }
                    else
                    {
                        Result.Message = "Numrange Not Found.Kindly Contact Administration.";
                        Result.MsgType = "E";
                    }
                }
            }
            catch (Exception ex)
            {
                Result.Message = ex.Message;
                Result.MsgType = "E";
            }
            return(Result);
        }
        public int[][] Merge(int[][] intervals)
        {
            /*
             * 将多个片段做个处理,即,当片段重叠,那么就直接合并成一个片段
             * 思路:
             *  1.将片段按照区间开始的大小来排序
             *  2.然后外层循环依次遍历所有的片段,内层循环查找并合并重叠片段
             *
             * 时间复杂度:排序O(nlogn)+遍历O(n),所以最后是,O(nlogn)
             * 空间复杂度:没有利用额外的空间,所以是,O(1)
             *
             * 考察点:
             *  1.排序、数组
             */

            //1.对区间片段排序
            List <NumRange> ranges = new List <NumRange>();

            for (int i = 0; i < intervals.GetLength(0); i++)
            {
                ranges.Add(new NumRange()
                {
                    Min = intervals[i][0],
                    Max = intervals[i][1]
                });
            }
            if (!ranges.Any())
            {
                return(intervals);
            }
            var orderedRanges = ranges.OrderBy(i => i.Min).ToList();

            //2.利用内外循环的方式做遍历
            var mergedRanges = new List <NumRange>();

            for (int j = 0; j < orderedRanges.Count; j++)
            {
                NumRange newRange = new NumRange();
                newRange.Min = orderedRanges[j].Min;
                newRange.Max = orderedRanges[j].Max;

                while (j + 1 < orderedRanges.Count && orderedRanges[j + 1].Min <= newRange.Max)
                {
                    if (orderedRanges[j + 1].Max > newRange.Max)
                    {
                        newRange.Max = orderedRanges[j + 1].Max;
                    }
                    j++;
                }

                mergedRanges.Add(newRange);
            }

            //3.返回需要的结果
            int[][] forReturn = new int[mergedRanges.Count][];
            for (int k = 0; k < mergedRanges.Count; k++)
            {
                forReturn[k] = new int[] { mergedRanges[k].Min, mergedRanges[k].Max }
            }
            ;

            return(forReturn);
        }
    }