各种条件真复杂,不仅是边界条件,而且还要分很多种情况讨论 而且 检查数组越界真心是个伤不起的活。。。
/** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Interval(int s, int e) { start = s; end = e; } * } */ public class Solution { public ArrayList<Interval> insert(ArrayList<Interval> intervals, Interval newInterval) { // Start typing your Java solution below // DO NOT write main() function int i = 0; for (; i < intervals.size(); ++i) if (intervals.get(i).end >= newInterval.start) break; int start = i; for (; i < intervals.size(); ++i) if (intervals.get(i).end >= newInterval.end) break; int end = i; if (intervals.size() == 0 || start >= intervals.size()){ intervals.add(newInterval); return intervals; } if (end >= intervals.size()) end--; if (start == end){ if (intervals.get(start).start > newInterval.end) intervals.add(start, newInterval); else{ intervals.get(start).start = Math.min(newInterval.start, intervals.get(start).start); intervals.get(start).end = Math.max(newInterval.end, intervals.get(start).end); } } else{ int tempstart = intervals.get(start).start; int tempend = intervals.get(end).end; if (intervals.get(end).start > newInterval.end){ for (int j = 0; j < end - start-1; ++j) intervals.remove(start); intervals.get(start).start = Math.min(tempstart, newInterval.start); intervals.get(start).end = newInterval.end; } else { for (int j = 0; j < end - start; ++j) intervals.remove(start); intervals.get(start).start = Math.min(tempstart, newInterval.start); intervals.get(start).end = Math.max(tempend, newInterval.end); } } return intervals; } }
发表评论
-
Implement strStr()
2012-11-07 15:44 581唉 终于到了要记算法的时候了 KMP。。。还没写完 回去再写。 ... -
Flatten Binary Tree to Linked List && Generate Parentheses && Gray Code
2012-11-07 00:08 1092Flatten太简单了 递归 一遍过 oh yeah = = ... -
First Missing Positive
2012-11-06 22:50 588唉 想了很久都没想出来 后来还是看了网上的答案 >_&l ... -
Edit Distance
2012-11-06 00:27 669动规 就是递推。。。比较难想 然后数组长度设置比字符串长度多一 ... -
Divide Two Integers
2012-11-05 00:12 712自己实现除法 太太太恶心了。。。。 就是用位移代替了乘法,然后 ... -
Distinct Subsequences
2012-11-04 21:44 657动规,从前到后用T的每一个字符i,扫描S的每一个字符j。维护一 ... -
Count and Say
2012-11-04 18:46 692public class Solution { ... -
Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Sear
2012-11-04 17:36 826/** * Definition for binary ... -
Container With Most Water
2012-11-04 00:25 697本来以为是个简单的题目,直接二重循环,结果小测试过了,大测试超 ... -
Construct Binary Tree from Inorder and Postorder Traversal
2012-11-03 23:40 746不知道为什么错了。。。eclipse上明明是正确的啊 leet ... -
Combinations
2012-11-03 22:19 604全排列 按理说很简单,可是用递归写,边界条件就还是难想清楚,s ... -
Combination Sum I && II
2012-11-03 21:41 751还是递归 但是边界条件以及边界上的处理不容易搞清楚(一开始我就 ... -
climbing stairs
2012-11-03 17:18 609一开始觉得是简单的组合数学题,但是写完之后发现,首先组合数不是 ... -
Binary Tree Inorder Traversal
2012-11-02 23:51 615I简单 直接递归就好 addAll函数很好用 /** ... -
Best Time to Buy and Sell Stock I & II
2012-11-02 22:05 1044啊 第一次直接过small和big测试 好爽!虽然主要是以前知 ... -
Balanced Binary Tree
2012-11-01 23:38 609/** * Definition for binary ... -
Anagrams
2012-10-31 00:33 586这题实在是没懂它的意思。。。囧啊 import java. ... -
Add Two Numbers
2012-10-30 23:03 614这题不难 直接上递归就行 /** * Definiti ... -
Add Binary
2012-10-29 00:07 599public String addBinary(Strin ... -
4Sum
2012-10-27 22:49 734本来以为只要在3Sum外面再包一层循环就好了,可是。。。在Ju ...
相关推荐
12 Insert Interval 45 13 Two Sum 47 14 Two Sum II Input array is sorted 49 15 Two Sum III Data structure design 51 16 3Sum 53 17 4Sum 55 18 3Sum Closest 57 19 String to Integer (atoi) 59 20 Merge ...
Insert Interval Merge Intervals Minimum Window Substring Multiply Strings Substring with Concatenation of All Words Pascal's Triangle Pascal's Triangle II Spiral Matrix Spiral Matrix II ZigZag ...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
颜色分类leetcode leetcode.etc My solutions of the problems in Online judge website, leetcode, lintcode, etc. leetcode: 13 ...Insert Interval Easy Two Strings Are Anagrams(比较字符串) E
Interval 解决方法:遍历 LeetCode: 229. Majority Element II 解决方法:Majority Voting算法的变种。但是最终的算法实现形式,很难理解。 2018-08-19 19:16 LeetCode: 79. Word Search 解决方法:DFS LeetCode: 31...
LifeInterval57Insert Interval56Merge Intervals252Meeting Rooms253Meeting客房II352Data流从数据Stream53Maximum Subarray325Maximum大小子阵总和脱节IntervalsTreeMapCounter239Sliding窗口Maximum295Find中位数...
INSERT INTO warranty VALUES 123 INTERVAL "8" MONTH ; INSERT INTO warranty VALUES 155 INTERVAL "200" YEAR 3 ; INSERT INTO warranty VALUES 678 "200 11" ; SELECT FROM ...
3 insert into TEST values(sysdate); 4 end; 5 / 过程已创建。 创建JOB SQL> variable job1 number; SQL> SQL> begin 2 dbms_job.submit(:job1,'MYPROC;',sysdate,'sysdate+1/...
ep_insert 时间戳功能当用户triggerSequence中定义的字符串时,插入当前的 UNIX 时间戳。 实现了一个非常简单的时间偏移校正,每updateInterval毫秒触发一次。 如果 Starttime 定义为Starttime: YYYY-MM-DD HH:MM:SS...
2 learn to file save and open, the interval of print preview, paragraphs, the first character position change, etc., can insert images, forms, art words in the Word, special symbols, etc. (3) ...
python-mysql-parrot-analytics ETL管道从API和INSERT INTO Mysql DB调用Parrot Analytics每月需求数据使用configuration.py来配置设置START_DATE ='2020-01-01'->数据查询的开始日期END_DATE ='2020-12-31'->数据...
'sysdate + 15/1440'–interval,设置定时器执行的频率,这样写每隔15分钟执行一次 ); commit; end; 这里第一个参数是任务编号,系统自动赋值。也可以采用isubmit来手动指定 第二个参数是需要执行...
this.timer3.Interval = 100; this.timer3.Start(); } else { SqlCommand sqlcmd; String sql = "insert into COM_JS_DATA values (" + comID.ToString() + ",getdate(),'" + STR + "')"; sqlcmd = new ...
-Interval [ms] 定时欺骗的时间间隔,默认是3秒 -spoofmode [1|2|3] 将数据骗发到本机,欺骗对象:1为网关,2为目标机,3为两者 -speed [kb] 限制指定的IP或IP段的网络总带宽,单位:KB example: 嗅探指定的IP段中端口...
5)蛇身移动的实现:蛇身的移动主要是用ArrList类来实现的,该类的主要功能是使用大小可以根据需要动态增加数组,即建立动态数组来存储蛇身,本实验主要使用ArrList类的Insert方法和RemoveAt方法实现蛇模块的增加、...
一、单项选择题 (只有一个正确答案) 【1】 执行语句"SELECT '2008-01-20'+ INTERVAL 2 DAY; "结果为 A: 2008-01-22 B: 2010-01-20 C: 2008-02-11 D: 2008-03-20 答案: A 【2】 下列哪个是不正确的MySQL的变量命名...
― Number(p, s) Oracle 主要数据类型 4-3 Date 数据类型 ―Date ―Timestamp ―Interval day to second ―Interval year to month ―Timestamp with time zone ―Timestamp with local time zone Oracle 主要数据...
― Number(p, s) Oracle 主要数据类型 4-3 Date 数据类型 ―Date ―Timestamp ―Interval day to second ―Interval year to month ―Timestamp with time zone ―Timestamp with local time zone Oracle 主要数据...
insert into temp values(null, ‘jack’, 22), (null, ‘jackson’ 23); 2、 update 修改语句 update主要完成对数据的修改操作,可以修改一条或多条数据。修改多条或指定条件的数据,需要用where条件来完成。 ...
SqlCommand sqlcom = new SqlCommand("insert into oa_smsend( Mobile,Content) values('" + phone + "','" + verify + "') ", this.my_sql_con); sqlcom.ExecuteNonQuery(); last_id = id; my_sql_con.Close();...