【leetcode】485.解题报告
2017-05-08 11:29
399 查看
自己的答案
public class Solution { public List<Integer> findDuplicates(int[] nums) { List<Integer> res = new ArrayList<>(); for(int i=0;i<nums.length;++i){ int index = Math.abs(nums[i])-1; if(nums[index]<0) res.add(Math.abs(index+1)); nums[index]=-nums[index]; } return res; } }
别人的漂亮代码
public int findMaxConsecutiveOnes(int[] nums) { int maxHere = 0, max = 0; for (int n : nums) max = Math.max(max, maxHere = n == 0 ? 0 : maxHere + 1); return max; }
反思:
两个数比较大小,应该用Math.max(A,B);多考虑 for each 循环
仅仅有一个 if else 的话,尽量用这种形式。简洁
哎,很简单一道题,但也可以看到和别人的差距。加油吧!
相关文章推荐
- Leetcode 485. Max Consecutive Ones 最大连续1 解题报告
- [Leetcode] 485. Max Consecutive Ones 解题报告
- 【LeetCode】485.Max Consecutive Ones解题报告
- [Leetcode] 188. Best Time to Buy and Sell Stock IV 解题报告
- [Leetcode] 198. House Robber 解题报告
- Letter Combinations of a Phone Number --leetcode解题报告
- 【LeetCode】41. First Missing Positive(Hard)解题报告
- Leetcode 101. Symmetric Tree 验证树的对称性 解题报告
- leetcode 第十六周解题报告
- LeetCode解题报告 452. Minimum Number of Arrows to Burst Balloons [medium]
- [Leetcode] 744. Find Smallest Letter Greater Than Target 解题报告
- leetcode 40. Combination Sum II 解题报告
- LeetCode: Regular Expression Matching 解题报告
- [leetcode] 302. Smallest Rectangle Enclosing Black Pixels 解题报告
- [Leetcode] 202. Happy Number 解题报告
- [LeetCode 解题报告]058. Length of Last Word
- LeetCode解题报告—— Jump Game & Merge Intervals & Permutation Sequence
- [Leetcode] 759. Employee Free Time 解题报告
- LeetCode 240. Search a 2D Matrix II 解题报告
- [Leetcode] 475. Heaters 解题报告