본문 바로가기

Java/Java 알고리즘 LeetCode

[LeetCode- Ch.1 문자열] 4. 플러스 원

반응형

https://leetcode.com/problems/plus-one/submissions/

 

Plus One - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

import java.util.stream.Stream;
class Solution {
    public int[] plusOne(int[] digits) {
        //비어 있지 않은 십진수 배열 주어지면 1증가
        //배열의 마지막 숫자에 +1
        //숫자 0제외 정수 앞에 0미포함
        int n = digits.length;
        
        if(digits[n-1]==9){
            //모든 자리가 9인 경우 -> 자리수+1 필요
            if(Arrays.stream(digits).allMatch(a -> (a==9))) {
                int[] new_digits = new int[n+1];
                new_digits[0]=1;
                return new_digits;
            }
            else{
                //마지막 자리가 9이지만 모두 9는 아닌 경우
                //그 중에서 해당 자리의 수가 9일 경우와 아닌 9가 아닌 경우
                boolean flag=false;
                for(int i=n-1;i>=0;i--){
                    if(digits[i]==9){
                        digits[i]=0;
                    }else{
                        digits[i]+=1;
                        break;
                    }
                }
            }
        }
        else{
            digits[n-1]++;
        }
        
        return digits;
    }
}

 

자바 - 람다와 스트림

allMatch() 모든 요소가 조건에 충족하면 참
anyMatch() 최소한 한 개의 요소가 충족하면 참 
noneMatch() 모든 요소가 조건에 충족하지 않으면 참
public static void main(String[] args){
        int[] intArr = {2, 4, 6};

        boolean result = Arrays.stream(intArr)
                .allMatch(a -> a%2 == 0);

        result = Arrays.stream(intArr)
                .anyMatch(a -> a%3 == 0);

        result = Arrays.stream(intArr)
                .noneMatch(a -> a%3 == 0);
}

 

 

+) 세련된 풀이

1. 뒤에서 부터 +1을 추가

2. Int형 배열이기 때문에, 해당 칸이 10이 아니면 그대로 리턴

3. 10이면 0을 넣는다.

4. 리턴되지 않았을 경우, 모두 9라는 뜻이므로 배열의 길이를 한칸 늘려서 맨 앞에 1을 넣고 리턴

class Solution {
    public int[] plusOne(int[] digits) {
        int n = digits.length;        

        for (int i = n - 1; i >= 0; i--) {
            digits[i]++;

            if (digits[i] < 10) {
                return digits;
            }

            digits[i] = 0;
        }

        int[] res = new int[n + 1];
        res[0] = 1;

        return res;
    }
}

class Solution {
    public int[] plusOne(int[] digits) {
        //마지막 배열에 +1를 한다.
        int n= digits.length;
        //케이스 분류
        //(1) 모두 9일 경우
        //(2) 마지막만 9일 경우
        //(3) 모두 9가 아닐 경우
        
        if(digits[n-1]==9){
            for(int i=n-1;i>=0;i--){
                if(i==0 && digits[i]==9){
                    int[] newdigits= new int[n+1];
                    newdigits[0]=1;
                    return newdigits;
                }
                if(digits[i]==9) {
                    digits[i]=0;
                    continue;
                }
                else{
                    digits[i]+=1;
                    break;
                }
               
            }            
        }else{
            digits[n-1]+=1;
        }
        return digits;
    }
}

 

class Solution {
    public int[] plusOne(int[] digits) {
        int n=digits.length;
        for(int i=n-1; i>=0; i--){
            digits[i]++;
            //10이 아니면, 그대로 리턴
            if(digits[i]<10){
                return digits;
            }
            //10이면, 0으로 초기화 후 for문 반복
            digits[i]=0;
        }
        
        //리턴되지 않았으므로, 모두 9인 경우
        digits = new int[n+1];
        digits[0]=1;
        
        return digits;
    }
}
반응형