- 浏览: 139242 次
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
[分析]
此题若不考虑极大值极小值相关的corner case是简单的,如base version,当前leetcode的test case没有包含这些边缘case,因此是可以通过的。下面给出两种实现,base version和做了防溢出处理的robust version,并提供了相应的test case。
遇到整数数组问题,需要特别留意溢出问题,面试时可先写出base version然后再考虑增加放溢出处理,不然后者可能严重干扰主干思路,对我来说,添加上防溢出处理的时间远多于写出base version的时间。
此题若不考虑极大值极小值相关的corner case是简单的,如base version,当前leetcode的test case没有包含这些边缘case,因此是可以通过的。下面给出两种实现,base version和做了防溢出处理的robust version,并提供了相应的test case。
遇到整数数组问题,需要特别留意溢出问题,面试时可先写出base version然后再考虑增加放溢出处理,不然后者可能严重干扰主干思路,对我来说,添加上防溢出处理的时间远多于写出base version的时间。
public class MissingRanges { @Test public void testMissingRanges() { MissingRanges obj = new MissingRanges(); List<String> expect = new ArrayList<String>(); // case 1 expect.add("-1->2147483646"); int[] nums = {Integer.MAX_VALUE}; List<String> actual1 = obj.findMissingRanges1(nums, -1, Integer.MAX_VALUE); List<String> actual2= obj.findMissingRanges2(nums, -1, Integer.MAX_VALUE); Assert.assertEquals(false, checker(expect, actual1)); Assert.assertEquals(true, checker(expect, actual2)); // case2 expect.clear(); int[] nums2 = {Integer.MAX_VALUE}; List<String> actual3 = obj.findMissingRanges1(nums2, Integer.MAX_VALUE, Integer.MAX_VALUE); List<String> actual4= obj.findMissingRanges2(nums2, Integer.MAX_VALUE, Integer.MAX_VALUE); Assert.assertEquals(false, checker(expect, actual3)); Assert.assertEquals(true, checker(expect, actual4)); } public boolean checker(List<String> expect, List<String> actual) { if (expect.size() != actual.size()) return false; for (int i = 0; i < expect.size(); i++) { if (!expect.get(i).equals(actual.get(i))) return false; } return true; } // more robust version, take care of overflow edge case public List<String> findMissingRanges2(int[] nums, int lower, int upper) { List<String> ret = new ArrayList<String>(); if (nums == null) return ret; for (int i = 0; i < nums.length; i++) { if (lower < nums[i]) { if (lower < nums[i] - 1) {// avoid overflow ret.add("" + lower + "->" + (nums[i] - 1)); } else { ret.add(String.valueOf(lower)); } } if (nums[i] < Integer.MAX_VALUE) // avoid overflow lower = nums[i] + 1; else lower = Integer.MAX_VALUE; } // process lower==upper for case like: [-1], -1, 0 // 添加nums[nums.length -1] != Integer.MAX_VALUE 这个判断,考虑case: // [Integer.MAX_VALUE], Integer.MAX_VALUE, Integer.MAX_VALUE if (lower < upper) { ret.add("" + lower + "->" + upper); } else if (lower == upper && !(nums.length > 0 && nums[nums.length -1] == Integer.MAX_VALUE)) { ret.add(String.valueOf(lower)); } return ret; } // base version public List<String> findMissingRanges1(int[] nums, int lower, int upper) { List<String> ret = new ArrayList<String>(); if (nums == null) return ret; for (int i = 0; i < nums.length; i++) { if (lower < nums[i]) { if (nums[i] - lower > 1) { ret.add("" + lower + "->" + (nums[i] - 1)); } else { ret.add(String.valueOf(lower)); } } lower = nums[i] + 1; } if (lower < upper) { ret.add("" + lower + "->" + upper); } else if (lower == upper) { ret.add(String.valueOf(lower)); } return ret; } }
发表评论
-
Leetcode - H-Index
2015-09-06 09:08 1134Given an array of citations (ea ... -
Leetcode - Integer to English Words
2015-09-04 20:53 1110[分析] 这题通过率之所以非常低是因为有很多corner ca ... -
Leetcode - Strobogrammatic Number III
2015-09-03 16:45 2202A strobogrammatic number is a n ... -
Leetcode - Basic Calculator II
2015-08-27 09:16 915mplement a basic calculator to ... -
Leetcode - Factorial Trailing Zeroes
2015-08-25 09:00 438[思路] 数乘积结果的后缀0,其实就是数结果中有多少个因子10 ... -
Leetcode - Ugly Number II
2015-08-24 22:54 1171[分析] 暴力的办法就是从1开始检查每个数是否是丑数,发现丑数 ... -
Leetcode - Excel Sheet Column Title
2015-08-24 10:24 646[分析] 十进制转26进制,需要注意的是26进制是以1为最小数 ... -
Leetcode - Max Points on a Line
2015-08-23 15:30 734[分析] 两条直线若包含一个公共点且斜率相同,则为同一条直线。 ... -
Leetcode - Fraction to Recurring Decimal
2015-08-23 10:05 477[分析] 处理int型整数运算时,为避免溢出,省事的做法就是内 ... -
Leetcode - Count Primes
2015-08-22 13:42 519[ref] https://en.wikipedia.org/ ... -
Leetcode - Strobogrammatic Number
2015-08-22 10:48 1101A strobogrammatic number is a n ... -
Leetcode - Add Binary
2015-08-21 09:28 479[分析] 从低位往高位逐位相加,就是这么一个简单的题却花了我一 ... -
Leetcode - Longest Consecutive Sequence
2015-08-20 21:20 496[分析] base version说几句: 数组题一定要考虑重 ... -
Leetcode - First Missing Positive
2015-08-20 07:45 663[分析] 将各个正数放入相应下标处,使之满足nums[nums ... -
Leetcode - Set Matrix Zeros
2015-08-19 20:55 565Given a m x n matrix, if an ele ... -
Leetcode - Rotate Image
2015-08-19 19:51 506[分析] 自己的思路:从外到内一圈圈顺时针旋转90度,坐标映射 ... -
Leetcode - 3Sum Smaller
2015-08-18 22:12 1498Given an array of n integers nu ... -
Leetcode - Spiral Matrix II
2015-08-18 19:49 507Given an integer n, generate a ... -
Leetcode - Spiral Matrix
2015-08-18 09:50 437Given a matrix of m x n element ... -
Leetcode - Contains Duplicate II
2015-08-18 07:57 570Given an array of integers and ...
相关推荐
javascript js_leetcode题解之163-missing-ranges.js
python python_leetcode题解之163_Missing_Ranges.py
12. Missing Ranges:给定一个无序的整数数组,找出所有缺失的区间。 13. Longest Palindromic Substring:找出最长的回文子串,可以使用中心扩散法或者动态规划。 14. One Edit Distance:判断两个字符串是否相差一...
12. Missing Ranges:给定一个有序整数列表和两个端点值,找出缺失的范围。 13. Longest Palindromic Substring:找出字符串中的最长回文子串。 14. One Edit Distance:判断两个字符串是否只差一个字符操作。 15. ...
268| [Missing Number](https://leetcode.com/problems/missing-number/) | [C++](./C++/missing-number.cpp) [Python](./Python/missing-number.py) | _O(n)_ | _O(1)_ | Medium | LintCode || 318| [Maximum ...
- **Using DATA Step Approaches to Identify and Count Missing Values**: Discusses various DATA step techniques for identifying and counting missing values. - **Searching for a Specific Numeric Value**:...
开源项目-studiofrenetic-period.zip,Period provides a set of missing Time Range to Go, it cover all basic operations regardings time ranges.
- **Bus ranges syntax errors** 总线范围的语法错误:如果在定义总线范围时使用了不正确的语法结构,则会产生这类错误。 - **Illegal bus range values** 非法的总线范围值:如果总线范围值不符合软件内部定义的...
If you encounter any issues or feel that something essential is missing from the documentation, consider contributing to the Kotlin project. The Kotlin community welcomes contributions, whether it’s ...
to TStringFields results in access violation.5.00 of 5 Closed8785 Filter or Ranges on Nested Dataset's should restrict master as well.0.00 of 5 Closed8417 TCustomSQLDataSet.GetFieldData ...
- **RHSOBJCH – Fixes SPD Control Tables Missing in TXSWU3**:解决SPD控制表在TXSWU3中缺失的问题。 - **RV80HGEN**:生成RV80H表的示例程序。 - **Scheduling of System Maintenance Jobs**:系统维护任务的...
- The `satf` function, which saturates values to finite ranges, has been updated in the Warp matrix functions. This is useful for ensuring numerical stability in matrix computations. 17. **...
- **3.3.3 初始化日期范围在选择选项 (Initializing Date Ranges on Selection Options)** - **概述**:设置日期范围的选择选项,方便用户过滤数据。 - **用途**:提高报表的灵活性和可用性。 - **3.3.4 报表标题...
7.4.1.1 Define Number Ranges for Actual Postings 501 7.4.1.2 Maintain Characteristic Groups 502 7.4.1.3 Assign Cha. Grp. for Assignment Screen 502 7.4.1.4 Assing Char. Grp. For Line Item Screen 503 ...
There is apparently some malware going around that blocks execution of Cheat Engine (Saying file missing, check filename, etc...) If you have been a victim of this then try this windows repair tool to...
ct. 18, 1995 v. 1.00 =========================================================================== + First release of CurveExpert 1.0. Oct. 26, 1995, v....==============================================...
2.2.1 RHSOBJCH – FIXES PD CONTROL TABLES MISSING IN TX SWU3.............................................11 3.8.12 CHECKING FOR BACKGROUND PROCESSING32 2.2.2 RV80HGEN.....................................
DLL: Infineon TLE98xx: Some J-Link LITEs could not connect establish a successful target connection due to missing firmware functionality. Fixed. DLL: JTAG: When only having 1 TAP in the JTAG chain ...