1 leetcode50 计算 x 的 n 次幂函数。

实现 pow(xn) ,即计算 x 的 n 次幂函数。

(1)调用库函数

(2)暴力o(N)

(3)分治

xxxxxx.......x   采用两端夹,如果是偶数 y=x的二分之n次方  result=y*y。如果是奇数,x的二分之n次方,result=y*y*x

x(n)->x(n/2)->x(n/4).....x(0)  每次减半,logn

 class Solution(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float
"""
if not n:
return
if n<:
return / self.myPow(x,-n)
if n%:
return x*self.myPow(x,n-)
return self.myPow(x*x,n/)

非递归

 class Solution(object):
def myPow(self, x, n):
"""
:type x: float
:type n: int
:rtype: float if not n:
return
if n<:
return / self.myPow(x,-n)
if n%:
return x*self.myPow(x,n-)
return self.myPow(x*x,n/)
"""
if n<:
x=/x
n=-n
pow=
while n:
if n&:
pow*=x
x*=x
n>>=
return pow

2 leetcode169 求众数

(1)暴力 两个循环,针对每一个x进行计数,时间复杂度n平方

(2)map,key为元素,value为count

c++版本 时间复杂度0(n) 循环一次 每一次对mapO(1)

 class Solution {
public:
int majorityElement(vector<int>& nums) {
unordered_map<int,int>hash;
int res=;
int len=nums.size();
for(int i=;i<len;i++)
{
hash[nums[i]]++;
if(hash[nums[i]]>len/)
{
res=nums[i];
}
}
return res;
}
};

(3) sort nlogn

(4)分治

先分两部分,left和right,如果right==left,那么总体也是ok,结果就是right/left。如果不相等,比较谁count大

 def majorityElement(self, nums):
if not nums:
return None
if len(nums) == :
return nums[]
a = self.majorityElement(nums[:len(nums)//2])
b = self.majorityElement(nums[len(nums)//2:])
if a == b:
return a
return [b, a][nums.count(a) > len(nums)//2]

面试之leetcode分治-求众数,x幂等的更多相关文章

  1. Leetcode 229.求众数II

    求众数II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...

  2. leetcode之求众数

    求众数 给定一个大小为 n 的数组,找到其中的众数. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 示例 2: 输入: [2,2,1,1,1,2 ...

  3. Java实现 LeetCode 229 求众数 II(二)

    229. 求众数 II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2, ...

  4. LeetCode(169. 求众数)

    问题描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...

  5. 【LeetCode】求众数

    给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. class Solution(object): ...

  6. 力扣(LeetCode)求众数 个人题解

    给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 ...

  7. LeetCode 229. 求众数 II(Majority Element II )

    题目描述 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...

  8. Java实现 Leetcode 169 求众数

    public static int majorityElement(int[] nums) { int num = nums[0], count = 1; for(int i=1;i<nums. ...

  9. 【Leetcode】【简单】【169求众数】【JavaScript】

    题目 169. 求众数 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [ ...

随机推荐

  1. python开发笔记-DataFrame的使用

    今天详细做下关于DataFrame的使用,以便以后自己可以翻阅查看 DataFrame的基本特征: 1.是一个表格型数据结构 2.含有一组有序的列 3.大致可看成共享同一个index的Series集合 ...

  2. apscheduler 执行报错No handlers could be found for logger "apscheduler.executors.default

    执行报错如下: No handlers could be found for logger "apscheduler.executors.default 解决: 加入日志,查看具体报错,载根 ...

  3. php 正则表达示中的原子

    原子 原子是正则表达示里面的最小单位,原子说白了就是需要匹配的内容.一个成立的正则表达示当中必须最少要有一个原子.大理石平台精度等级 所有可见不可见的字符就是原子 说明:我们见到的空格.回车.换行.0 ...

  4. (尚019)Vue基于脚手架编写项目

    vue_demo目录结构截图: (1)图一 (2).图二 (3).图三 (四).图四 (5).图五 (6).图六 (7).图七 不能随便改入口文件的名字,因为已经配置好了 (8).图八 (9).图九 ...

  5. Minidumps 和 modules匹配

    简介 调试应用程序时,调试器必须加载可执行模块的符号,以便能够显示有意义的调用堆栈.当前源代码行.变量值等.如果您曾经调试过在另一个系统上创建的小型转储,那么您已经知道除了符号之外,调试器还需要访问创 ...

  6. 开源项目 03 DocX

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. ImageView.ScaleType

    前言 对ImageView.ScaleType,学习安卓需掌握.以官方链接:http://android.xsoftlab.net/reference/android/widget/ImageView ...

  8. Hadoop 在启动或者停止的时候需要输入yes确认问题

    启动或者停止hadoop的时候,信息如下: Stopping namenodes on [hadoop1 hadoop2] The authenticity of host 'hadoop2 (172 ...

  9. Oracle in不超过1000,List<String>参数拆分,代码举例

    public Map<String,Map<String, Object>> getConsInfo(List<String> consIdList) { Map& ...

  10. intellij ide调用一个对象所有的set方法

    1.下载地址:https://github.com/yoke233/genSets/releases/download/1.1/genSets.jar 2.plugin 从本地磁盘安装找到jar,并重 ...