485. 最大连续1的个数

  1. class Solution {
  2. public int findMaxConsecutiveOnes(int[] nums) {
  3. int ans = 0;
  4. if (nums == null || nums.length == 0)
  5. return ans;
  6. int length = nums.length;
  7. int left = 0, right = 0;
  8. while (left < length && right < length) {
  9. if (nums[left] == 1) {
  10. right = left;
  11. while (right < length && nums[right] == 1) {
  12. ++right;
  13. }
  14. int curMaxOne = right - left;
  15. ans = Math.max(ans, curMaxOne);
  16. left = right;
  17. } else {
  18. ++left;
  19. }
  20. }
  21. return ans;
  22. }
  23. }