분류 전체보기

    배열회전시키기★ rotate()함수이용, List get(), remove(), add()

    나의 풀이 import java.util.*; import java.util.stream.Collectors; import static java.util.Collections.*; class Solution { public int[] solution(int[] numbers, String direction) { List list= Arrays.stream(numbers).boxed().collect(Collectors.toList()); if (direction.equals("right")) { rotate(list, 1); } else { rotate(list, -1); } int[] answer = list.stream().mapToInt(i -> i).toArray(); return answer; ..

    String.join() 함수에 대하여

    1. String 문자열 데이터의 결합 public class Solution { String str1 ="컴퓨터"; String str2 ="본체"; String strData =String.join("", str1, str2); public static void main(String[] args) { Scanner sc = new Scanner(System.in); Solution s = new Solution(); System.out.println(s.strData); } } 출력결과 컴퓨터본체 2. ArrayList 리스트(List)요소의 결합 public class Solution { public static void main(String[] args) { ArrayList list = new ..

    인덱스바꾸기★ 문자열→문자열배열→List swap(), join()

    나의 풀이 class Solution { public String solution(String my_string, int num1, int num2) { String answer=""; String[] array_word=my_string.split(""); String tmp=array_word[num1]; array_word[num1]=array_word[num2]; array_word[num2]=tmp; for(String s: array_word){ answer+=s; } return answer; } } ▶ split()의 리턴타입은 String배열 임에 주의 한다. 다른 사람의 풀이 import java.util.Arrays; import java.util.Collections; import ..

    주사위의 개수 → 연산의 순서에 주의하자 (괄호의 중요성)

    나의 틀린 풀이 public class Solution { public int solution(int[] box, int n) { int answer = 0; answer=box[0]/n * box[1]/n * box[2]/n; return answer; } } ▶ 위와 같은 경우 몫* 몫 * 몫이 아니라 ▶ 몫*box[1]가 되는 불상사가 일어난다. 올바른 풀이 public class Solution { public int solution(int[] box, int n) { int answer = 0; answer=(box[0]/n) * (box[1]/n) * (box[2]/n); return answer; } } ▶ 괄호를 반드시 해줘서 올바른 연산을 하자