• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ying_vincent
博客园    首页    新随笔    联系   管理    订阅  订阅

LeetCode: Max Consecutive Ones

这题最关键的是处理最开始连续1和最后连续1的方式,想到list一般在最前面加个node的处理方式,在最前面和最后面加0即可以很好地处理了

 1 public class Solution {
 2     public int findMaxConsecutiveOnes(int[] nums) {
 3         int[] newNums = new int[nums.length+2];
 4         newNums[0] = 0;
 5         newNums[newNums.length-1] = 0;
 6         for (int i = 1; i < newNums.length-1; i++) {
 7             newNums[i] = nums[i-1];
 8         }
 9         int ans = 0;
10         int lastPos = 0;
11         for (int i = 0; i < newNums.length; i++) {
12             if (newNums[i] == 0) {
13                 ans = Math.max(ans, i - lastPos);
14                 lastPos = i;
15             }
16         }
17         return ans - 1;
18     }
19 }

 

posted @ 2017-02-24 13:33  ying_vincent  阅读(167)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3