LeetCode_Study_Plan/Algorithm
977. Squares of a Sorted Array java
개발하는루루
2022. 12. 20. 22:16
https://leetcode.com/problems/squares-of-a-sorted-array/
Squares of a Sorted Array - 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
class Solution {
public int[] sortedSquares(int[] nums) {
int[] ans = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
ans[i] = (int) Math.pow(nums[i],2);
}
Arrays.sort(ans);
return ans;
}
}
** Math.pow를 사용함에 있어 (int) 없이 쓰려고 하니 에러가 떴다.
possible lossy conversion from double to int
그래서 관련되어 에러를 찾아보니 pow를 통해 정수 결과를 얻고 싶으면 타입에 대한 캐스팅이 필요한 것 같다.
해당 내용만 채워주니 바로 문제를 통과할 수 있었다.