매일 Algorithm

신재원·2023년 5월 6일
1

Algorithm

목록 보기
114/243

프로그래머스 (정수 부분)

class Solution {
    public int solution(double flo) {
    // double 형은 int 형보다 메모리 크기가 크므로 int형으로 형변환이 가능합니다.
        int answer = (int) flo;
        return answer;
    }
}

프로그래머스 (문자열의 뒤의 n글자)

class Solution {
        public String solution(String my_string, int n) {
            // my_string의 길이의 - n 부터 문자열을 자르기시작합니다.
            String answer = my_string.substring(my_string.length() - n);
            return answer;
        }
    }

프로그래머스 (n 번째 원소부터)

import java.util.ArrayList;
import java.util.List;
class Solution {
        public int[] solution(int[] num_list, int n) {
            List<Integer> tempList = new ArrayList<>();

            // List에 값을 담아준다.
            for (int i = n - 1; i < num_list.length; i++) {
                tempList.add(num_list[i]);
            }

            // tempList 길이 만큼 배열 크기 할당
            int[] answer = new int[tempList.size()];

            for (int i = 0; i < answer.length; i++) {
                answer[i] = tempList.get(i);
            }
            return answer;
        }
    }

프로그래머스 (홀짝에 따라 다른 값 반환하기)

public class problem376 {
    class Solution {
        public int solution(int n) {
            int answer = 0;
            
            // n이 짝수일경우
            if (n % 2 == 0) {
                // 조건문의 증감식을 잘 조정한다.
                for (int i = n; i >= 1; i -= 2) {
                    answer += i * i;
                }
            } else {
                for (int i = 1; i <= n; i += 2) {
                    answer += i;
                }
            }

            return answer;
        }
    }
}

0개의 댓글