# 2485. Find the Pivot Integer ###### tags: `Leetcode` `Easy` `Math` Link: https://leetcode.com/problems/find-the-pivot-integer/description/ ## 思路 假设pivot integer是x 那么我们需要找到x使得```(1+x)*x/2=(n+x)*(n-x+1)/2``` 也就是```2*x*x=n*n+n``` 如果```n*n+n```不能被2整除说明找不到答案 如果```Math.sqrt((n*n+n)/2)```不是整数说明找不到答案 ## Code ```java= class Solution { public int pivotInteger(int n) { if((n*n+n)%2!=0) return -1; int sq = (int) Math.sqrt((n*n+n)/2); if(sq*sq!=(n*n+n)/2) return -1; return sq; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up