[코테 풀이] Keep Multiplying Found Values by Two

시내·2024년 7월 29일
0

Q_2154) Keep Multiplying Found Values by Two

출처 : https://leetcode.com/problems/keep-multiplying-found-values-by-two/

You are given an array of integers nums. You are also given an integer original which is the first number that needs to be searched for in nums.

You then do the following steps:

  1. If original is found in nums, multiply it by two (i.e., set original = 2 * original).
  2. Otherwise, stop the process.
  3. Repeat this process with the new number as long as you keep finding the number.

Return the final value of original.

class Solution {
    public int findFinalValue(int[] nums, int original) {
        List<Integer> numbers = new ArrayList<>();
        for (int n = 0; n < nums.length; n++) numbers.add(nums[n]);
        if (!numbers.contains(original)) return original;
        while (numbers.contains(original)) {
            original *= 2;
        }
        return original;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글