728x90
Jumping on the Clouds | HackerRank
Jumping on the clouds
www.hackerrank.com
- 문제
- d 만큼 왼쪽으로 Shift 한 결과를 리턴
풀이
- d 가 배열을 크기와 같다면 배열 그대로 리턴
- list 선언 후, d ~ 배열 크기 , 0 ~ d 까지를 순차적으로 더해준다.
public static List<Integer> rotLeft(List<Integer> a, int d) { if(a.size() == d) // 배열 크기와 왼쪽으로 이동할 길이가 같으므로 배열 그대로를 리턴 return a; List<Integer> list = new ArrayList<>();// 반환할 배열 선언 for(int i=d;i<a.size();++i){ list.add(a.get(i));// d ~ 배열 끝 } for(int i=0;i<d;++i){ list.add(a.get(i)); // 0 ~ d-1 } return list; }
728x90
'알고리즘 > HackerRank' 카테고리의 다른 글
| Minimum Swaps 2 (0) | 2021.11.19 |
|---|---|
| New Year Chaos (0) | 2021.11.19 |
| 2D Array - DS (0) | 2021.11.18 |
| Repeated String (0) | 2021.11.18 |
| Jumping on the Clouds (0) | 2021.11.18 |
