leetcode367. Valid Perfect Square

2024-04-10 23:18

本文主要是介绍leetcode367. Valid Perfect Square,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt.
Example 1:
Input: 16
Returns: True
Example 2:
Input: 14
Returns: False

思路:二分法

class Solution(object):def isPerfectSquare(self, num):""":type num: int:rtype: bool"""start=0end=(num+1)/2+1while(end-start>1):mid=start+(end-start)/2tmp=mid*midif tmp<num:start=midelif tmp>num:end=midelse:return Truereturn False

这篇关于leetcode367. Valid Perfect Square的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/892432

相关文章

LeetCode --- Valid Anagram解题分析

题目描述:给定两个字符串,判断是否是字谜游戏。比如:   s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.   解题思路一:只要字符串中所有字母出现次数相同即可判定是字谜游戏,所以统计两字符串各字符出现的次数,如果都相同则返回true,否则返回false: bool isAnagr

asp.net 错误解决方案--对象的当前状态使该操作无效的解决办法(peration is not valid due to the current state of the object)

错误提示: 异常详细信息: System.InvalidOperationException: 对象的当前状态使该操作无效(Operation is not valid due to the current state of the object) 原因: 出现这个异常的原因正是因为2012年12月29号那次微软发布的最后一次非正常更新程序引起的.在这次安全更新中对于asp.net单次的提交量

LeetCode第20题之Valid Parentheses

方法一:用栈实现 C++代码: #include <stack>#include <iostream>using namespace std;//Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.clas

A. Theatre Square

A. Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes input standard input output standard output Theatre Square in the capital city of Berland has

PAT甲级 1085 Perfect Sequence 二分和双指针(Two Pointers)

二分写法 #include <bits/stdc++.h>using namespace std;int find_upper_bound(const vector<long long>& nums, long long x){int beg = 0, end = nums.size(), mid = beg + (end - beg) / 2;while (beg < end) {mid

【vue】form表单提交validate验证不进valid原因

目录 1. 原因 1. 原因 1.<el-form>是否写了ref=“form”。2.是否有其它标签写了ref=“form”。3.<el-form>中要写成:model,不能使用v-model。4.自定义的validate要各个路径均能返回callback()。 const validatePass= (rule, value, callback) => {if (th

Valid Anagram问题及解法

问题描述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false. Note: You may a

Valid Palindrome II问题及解法

问题描述: Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. 示例: Input: "aba"Output: True Input: "abca"Output: TrueExplanation: You c

Easy 5 Valid Parentheses(20)

Description Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. The brackets must close in the correct order, “()” and “()[]{}” are

【LeetCode最详尽解答】125-验证回文串 Valid-Palindrome

欢迎收藏Star我的Machine Learning Blog:https://github.com/purepisces/Wenqing-Machine_Learning_Blog。如果收藏star, 有问题可以随时与我交流, 谢谢大家! 链接: 125-验证回文串 直觉 这个问题需要使用一些内置函数,比如 s[l].isalnum() 和 s[l].lower()。基本要求简单明了。