Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 파이썬
- dynamic programming
- Counting
- two pointers
- 구현
- greedy
- string
- 코딩테스트
- java
- Matrix
- hash table
- 코테
- Stack
- Binary Search
- array
- database
- geometry
- Tree
- Data Structure
- sorting
- bit manipulation
- Method
- Math
- 자바
- Class
- Binary Tree
- simulation
- SQL
- implement
- Number Theory
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 69. Sqrt(x) 본문
1. Input
1) 음이 아닌 정수를 담은 int 변수 x
2. Output
1) x의 제곱근을 담은 int 변수
2) 제곱근이 소수로 나올 경우, 정수 부분만 반환
3. Constraint
1) 0 <= x <= 2^31 - 1
2) Math.pow나 x**0.5 등 built-in 함수, 연산자 사용 금지
4. Example
Input: x = 8
Output: 2
5. Code
1) 첫 코드(2022/07/15)
long i = 1;
while(true){
if((long)x<i*i)
break;
i++;
}
return (int)i-1;
- 제곱근의 정수 부분을 반환해야 하므로, i의 제곱보다 작은 경우 i-1을 반환하게끔 설계
- 중간에 계산하다보면 숫자가 커지므로 long 타입으로 계산 후, 반환 값은 int이므로 형변환을 해서 반환
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 125. Valid Palindrome (0) | 2022.10.11 |
---|---|
[LeetCode/Easy] 88. Merge Sorted Array (0) | 2022.08.24 |
[LeetCode/Easy] 58. Length of Last Word (0) | 2022.08.23 |
[LeetCode/Medium] 537. Complex Number Multiplication (0) | 2022.08.23 |
[LeetCode/Medium] 1314. Matrix Block Sum (0) | 2022.08.23 |