반응형
250x250
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
- KMP알고리즘
- 스택
- 프로그래머스
- 별 찍기
- ai리터리시
- 브루트포스 알고리즘
- 해시를 사용한 집합과 맵
- 문자열
- 구현
- 실패함수
- Queue
- 문자열제곱
- 정렬
- 임의 정밀도 / 큰 수 연산
- 큰 수 연산
- 수학
- 자료 구조
- 연결리스트 중복제거
- 다이나믹 프로그래밍
- Physical AI
- 이분 탐색
- 유클리드 호제법
- 시뮬레이션
- SeSAC
- 재귀
- 정수론
- 피지컬 에이아이
- 큐
- 조합론
- 사칙연산
Archives
- Today
- Total
hahn
[LeetCode - C] 69. Sqrt(x) 본문
728x90
반응형
Sqrt(x) - 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
Given a non-negative integer x, compute and return the square root of x.
Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.
Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5.
Example 1:
Input: x = 4
Output: 2
Example 2:
Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
Constraints:
- 0 <= x <= 231 - 1
Solution 1
x의 제곱근을 소수점을 버린 뒤 리턴하는 문제
int의 최댓값의 제곱근은 46,340.95000105199.. 이므로
num * num에서 46340을 넘어가면
오버플로가 일어나기 때문에 46341이 되면 46340을 리턴
int mySqrt(int x)
{
int num;
num = 0;
while (num * num <= x)
{
num++;
if (num > 46340)
return (46340);
}
return (num - 1);
}728x90
반응형
'코딩테스트 연습 > LeetCode(C - Easy)' 카테고리의 다른 글
| [LeetCode - C] 83. Remove Duplicates from Sorted List (0) | 2022.04.18 |
|---|---|
| [LeetCode - C] 70. Climbing Stairs (0) | 2022.04.18 |
| [LeetCode - C] 67. Add Binary (0) | 2022.04.18 |
| [LeetCode - C] 66. Plus One (0) | 2022.04.16 |
| [LeetCode - C] 58. Length of Last Word (0) | 2022.04.12 |
