当前位置:网站首页>[binary search simple question] leetcode 35. search insertion position, 69. Square root of X, 367. Effective complete square, 441. Arrange coins
[binary search simple question] leetcode 35. search insertion position, 69. Square root of X, 367. Effective complete square, 441. Arrange coins
2022-07-27 03:04:00 【Wow, Kaka, negative, positive】
LeetCode 35. Search insert location
https://leetcode.cn/problems/search-insert-position/
Given a sort array and a target value , Find the target value in the array , And return its index . If the target value does not exist in the array , Return to where it will be inserted in sequence .
nums by No repeating elements Of Ascending Arrange arrays
Please use a time complexity of O(log n) The algorithm of .
Example 1:
Input : nums = [1,3,5,6], target = 5
Output : 2
Example 2:
Input : nums = [1,3,5,6], target = 2
Output : 1
Example 3:
Input : nums = [1,3,5,6], target = 7
Output : 4
Solution
int searchInsert(int* a, int n, int target){
int l = 0, r = n - 1;
int ans = n;
int mid;
while (l <= r) {
mid = (l + r) / 2;
if (a[mid] >= target) {
ans = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
LeetCode 69. x The square root of
https://leetcode.cn/problems/sqrtx
Give you a nonnegative integer x , Calculate and return x Of Arithmetical square root .
Because the return type is an integer , The result is only the integral part , The decimal part will be removed .
Request can't be used sqrt(x) and pow(x, 0.5).
Example 1:
Input :x = 4
Output :2
Example 2:
Input :x = 8
Output :2
explain :8 The arithmetic square root of is 2.82842..., Because the return type is an integer , The decimal part will be removed .
Solution
int mySqrt(int x){
long long l = 0, r = x, mid, ans;
while (l <= r) {
mid = (l + r) / 2;
if (mid * mid <= x) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
LeetCode 367. Effective complete square number
https://leetcode.cn/problems/valid-perfect-square
Given a Positive integer num , Write a function , If num It's a complete square , Then return to true , Otherwise return to false .
Advanced : Don't Use any built-in library functions , Such as sqrt .
Example 1:
Input :num = 16
Output :true
Example 2:
Input :num = 14
Output :false
Solution
bool isPerfectSquare(int num){
long long l = 0, r = num, mid, temp;
while (l <= r) {
mid = (l + r) / 2;
temp = mid * mid;
// Add one more judgment condition
if (temp == num) {
return true;
} else if (temp < num) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return false;
}
Another more convenient way : Directly in 69 Based on the question, you can directly make a judgment on the return value
bool isPerfectSquare(int num){
long long l = 0, r = num, mid, ans;
while (l <= r) {
mid = (l + r) / 2;
if (mid * mid <= num) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans * ans == num;
}
LeetCode 441. Arrange coins
https://leetcode.cn/problems/arranging-coins
You have a total of n Coin , And plan to arrange them in a ladder . For a by k A ladder of rows , Its first i OK, there must be exactly i Coin . The last line of the ladder Probably It's incomplete .
Here's a number n , Calculate and return to form Full ladder row The total number of rows of .
Input :n = 5
Output :2
explain : Because the third line is incomplete , So back 2 .
Input :n = 8
Output :3
explain : Because the fourth line is incomplete , So back 3 .
Solution
Violence law :
int arrangeCoins(int n){
int i;
for (i = 1; n >= i; i++) {
n -= i;
}
return i - 1;
}
Dichotomy :
In fact, it's two points i The process of
whole i Layer or whole i Layer plus extra incomplete next layer , The results are ans = i
The meet i * (i + 1) / 2 <= n < i * (i + 1) / 2 + (i + 1), Get ans = i
int arrangeCoins(int n){
long long ans = 0, l = 1, r = n, mid;
while (l <= r) {
mid = (l + r) / 2;
if (mid * (mid + 1) / 2 <= n) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return ans;
}
Mathematical method :
To assume that n It can be arranged completely x A staircase , namely x ( x + 1 ) 2 = n \frac{x(x+1)}{2} = n 2x(x+1)=n
Into a quadratic equation of one variable : x 2 + x − 2 n = 0 x^2 + x - 2n = 0 x2+x−2n=0
solve equations : x 1 = − 1 + 8 n + 1 2 , x 2 = − 1 − 8 n + 1 2 x_1 = \frac{-1+\sqrt{8n+1}}{2}, x_2 = \frac{-1-\sqrt{8n+1}}{2} x1=2−1+8n+1,x2=2−1−8n+1
because x 2 < 0 x_2 \lt 0 x2<0 disqualification , So take ⌊ x 1 ⌋ \lfloor x_1 \rfloor ⌊x1⌋ For the solution .
int arrangeCoins(int n){
return (int) ((sqrt(8 * (long long)n + 1) - 1) / 2);
}
边栏推荐
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- 小程序怎样助力智能家居生态新模式
- B-树的应用以及添加和删除操作
- Kubernetes dashboard deployment application and access
- Plato Farm全新玩法,套利ePLATO稳获超高收益
- Blog competition dare to try BAC for beginners
- JS utils fragmented
- Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
- 小玩一个并行多线程MCU—MC3172
- Plato Farm通过LaaS协议Elephant Swap,为社区用户带来全新体验
猜你喜欢

Non global function of lua function

杀毒软件 clamav 的安装和使用

JMeter interface test, quickly complete a single interface request

百度云人脸识别

小玩一个并行多线程MCU—MC3172

Lua函数之非全局函数

Leetcode skimming -- no.238 -- product of arrays other than itself

论构造函数的原型是谁

毕业2年转行软件测试获得12K+,不考研月薪过万的梦想实现了

"Software testing" packaging resume directly improves the pass rate from these points
随机推荐
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
[栈和队列简单题] LeetCode 232. 用栈实现队列,225. 用队列实现栈
Blog competition dare to try BAC for beginners
云开发寝适闹钟微信小程序源码
iNFTnews | GGAC联合中国航天ASES 独家出品《中国2065典藏版》
Heisei thousand words (Heisei, September 10, 2012) (Shidu Mingzuo) the universe is far away, the Milky way is permanent, the sun and moon are running disorderly, the earth is open, the seasons shift,
如何白嫖最新版BurpSuite Pro
智能指针shared_ptr、unique_ptr、weak_ptr
Plato Farm全新玩法,套利ePLATO稳获超高收益
Applet utils
阿里云解决方案架构师张平:云原生数字化安全生产的体系建设
论构造函数的原型是谁
Rust web (I) -- self built TCP server
数据库读写分离和分库分表
Interrupt, signal, system call
小玩一个并行多线程MCU—MC3172
调用JShaman的Web API接口,实现JS代码加密。
Non global function of lua function
Okaleido tiger logged into binance NFT on July 27, and has achieved good results in the first round
毕业2年转行软件测试获得12K+,不考研月薪过万的梦想实现了