Optimizing LeetCode Plus One Problem in Java

Problem :- Plus One (LeetCode 66) Problem Statement :- You are given a large integer represented as an integer array digits, where each digits[i] is a digit of the integer. The digits are ordered from most significant to least significant. Increment the integer by one and return the resulting array of digits. Approach :- Carry Handling i - Traverse from the last digit ii - If digit < 9 → increment and return iii - If digit == 9 → make it 0 and carry forward iv - If all digits are 9 → create new array v - Time Complexity : O(n) vi - Space Complexity : O(1) class Solution {    public int[] plusOne(int[] digits) {     int n = digits.length;      for(int i = n - 1; i >= 0; i--) {        if(digits[i] < 9) {          digits[i]++;          return digits;        }        digits[i] = 0;      }      int[] result = new int[n + 1];      result[0] = 1;      return result;    } } How would you optimize this further? #Java #DSA #LeetCode #CodingJourney #LearnInPublic #SoftwareEngineering #Arrays

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories