#Leet Code# Sqrt

描述:log(n)

代码:

 1 class Solution:
 2     # @param x, an integer
 3     # @return an integer
 4     def getVal(self, begin, end, x):
 5         if end == begin :
 6             return begin
 7         if end == begin + 1:
 8             return begin
 9
10         while True:
11             mid = (begin + end) / 2
12             tmp = mid * mid
13
14             if tmp == x:
15                 return mid
16             elif tmp < x:
17                 return self.getVal(mid, end, x)
18             elif tmp > x:
19                 return self.getVal(begin, mid, x)
20
21     def sqrt(self, x):
22         if x == 0: return 0
23
24         val = 1
25         while True:
26             tmp  = val * val
27
28             if tmp == x:
29                 return val
30             elif tmp > x:
31                 return self.getVal(val/2, val, x)
32             else:
33                 val *= 2

#Leet Code# Sqrt,布布扣,bubuko.com

时间: 2024-10-12 16:45:46

#Leet Code# Sqrt的相关文章

#Leet Code# Evaluate Reverse Polish Notation

描述:计算逆波兰表达法的结果 Sample: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 使用stack实现: 1 def is_op

#Leet Code# Unique Tree

语言:Python 描述:使用递归实现 1 class Solution: 2 # @return an integer 3 def numTrees(self, n): 4 if n == 0: 5 return 0 6 elif n == 1: 7 return 1 8 else: 9 part_1 = self.numTrees(n-1) * 2 10 part_2 = 0 11 12 for i in range(1,n-1): 13 part_left = self.numTrees(

leet code Sort List

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leet code Sort List 对链表使用快慢指针归并排序 Sort List Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-lin

【Leet Code】Palindrome Number

Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an integer is a palindrome. Do this without extra space. 判断一个数整数是不是回文?例如121,1221就是回文,好吧,直接利用前面写过的[Leet Code]Reverse Integer--"%"你真的懂吗? 不过这里要考虑翻转后,数值

Leet Code OJ 119. Pascal&#39;s Triangle II [Difficulty: Easy]

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 翻译: 给定一个下标k,返回第k行的杨辉三角. 例如给定k=3,返回[1,3,3,1]. 提示:你可以优化你的算法,让它只使用O(k)的额

#Leet Code# Gray Code

描述: 要求相邻数2进制差一位 先获得n-1的列表表示小于 2^(n-1) 的符合要求的列表,加上最高位的加成 2^(n-1) 就是大于等于 2^(n-1) 的符合要求的列表,后者翻转一下就能够与前者连接上了 代码: 1 class Solution: 2 # @return a list of integers 3 def grayCode(self, n): 4 if n == 0: return [0] 5 6 s1 = self.grayCode(n - 1) 7 s2 = [item

Leet Code OJ 118. Pascal&#39;s Triangle [Difficulty: Easy]

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 翻译: 给定一个数numRows,产生前numRows行的杨辉三角(即贾宪三角形.帕斯卡三角形). 分析: 除了每行首尾是1以外,其他元素均可由上行推出,本方案采用lastLine保存上行数

#Leet Code# Same Tree

语言:Python 描述:使用递归实现 1 # Definition for a binary tree node 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 # @param p, a tree node 10 # @param q, a tree node 11 # @return

#Leet Code# LRU Cache

语言:C++ 描述:使用单链表实现,HeadNode是key=-1,value=-1,next=NULL的结点.距离HeadNode近的结点是使用频度最小的Node. 1 struct Node { 2 int key; 3 int value; 4 Node* next; 5 }; 6 7 class LRUCache { 8 public: 9 LRUCache(int capacity) { 10 this->capacity = capacity; 11 this->curLength