LeetCode -- Pow(x, n)
2015-10-17 00:42
316 查看
题目描述:
Implement pow(x, n).
思路:
如果n为偶数:
MyPow(x,n) = MyPow(x,n/2) 的平方
如果n为奇数:
MyPow(x,n) = x * MyPow(x, (n-1)/2)的平方
实现代码:
Implement pow(x, n).
思路:
如果n为偶数:
MyPow(x,n) = MyPow(x,n/2) 的平方
如果n为奇数:
MyPow(x,n) = x * MyPow(x, (n-1)/2)的平方
实现代码:
public class Solution { public double MyPow(double x, int n) { if(n <= 2){ return Math.Pow(x, n); } if(n % 2 == 0){ var y = MyPow(x, n/2) ; return y * y; } else{ var y = MyPow(x, (n-1)/2); return x * y * y; } } }
相关文章推荐
- 设计模式-单例模式
- UVA - 12166 Equilibrium Mobile
- UVa12100 Printer Queue (打印队列)
- Modern Operating System --- Thin Clients
- Leetcode Nim Game
- LeetCode -- Palindrome Partitioning
- Html5下炫酷文字雨滴效果_简单实现
- android 20 Intnet类重要的成员变量
- LeetCode -- Jump Game
- php 模拟登陆正方教务系统
- 磁盘测速工具汇总
- LeetCode -- Evaluate Reverse Polish Notation
- LeetCode -- Convert Sorted Array to Binary Search Tree
- json格式化
- LeetCode -- Climbing Stairs
- LeetCode -- Binary Tree Preorder Traversal
- 浅谈R语言
- Scala学习笔记26【Set、Map、TreeSet、TreeMap实战】
- seo
- 有关页面编码的控制问题,乱码如何解决