面试之leetcode分治-求众数,x幂等
1 leetcode50 计算 x 的 n 次幂函数。
实现 pow(x, n) ,即计算 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幂等的更多相关文章
- Leetcode 229.求众数II
求众数II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...
- leetcode之求众数
求众数 给定一个大小为 n 的数组,找到其中的众数. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 示例 2: 输入: [2,2,1,1,1,2 ...
- Java实现 LeetCode 229 求众数 II(二)
229. 求众数 II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2, ...
- LeetCode(169. 求众数)
问题描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] ...
- 【LeetCode】求众数
给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. class Solution(object): ...
- 力扣(LeetCode)求众数 个人题解
给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 ...
- LeetCode 229. 求众数 II(Majority Element II )
题目描述 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...
- Java实现 Leetcode 169 求众数
public static int majorityElement(int[] nums) { int num = nums[0], count = 1; for(int i=1;i<nums. ...
- 【Leetcode】【简单】【169求众数】【JavaScript】
题目 169. 求众数 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [ ...
随机推荐
- Python 通过lxml遍历html xpath
#coding:utf-8 ''' Created on 2017年10月9日 @author: li.liu ''' from selenium import webdriver from lxml ...
- GlusterFS集群文件系统研究
https://blog.csdn.net/liuaigui/article/details/6284551 1. GlusterFS概述GlusterFS是Scale-Out存储解决方案G ...
- rs485一主多从的连接方式及通信注意事项
rs485的通信方式看似比较简单,其实通信软件的处理还是有需要注意的. 下图是主机向从机发送信息的示意图,其中485的线都是手牵手相连的,因此主机向下发的时候,其实各个从机都有在接收数据的,只是,从机 ...
- MATLAB中运算符优先级
下述运算符的优先级从低到高: 1.先决或(||): 2.先决与(&&): 3.逻辑或(|): 4.逻辑与(&): 5.等于类(<,<=,>,>=,==, ...
- Java 用Jackson进行json和object之间的转换(并解决json中存在新增多余字段的问题)
1.添加jackson库 如果是maven工程,需要在pom.xml中添加jackson的依赖: <dependency> <groupId>com.fasterxm ...
- Linux 系统管理——账号管理
一.用户账号管理 1.用户账户概述 用户账户的常见分类: 超级用户:root uid=0 gid=0 权限最大 普通用户:uid>=500 做一般权限的系统管理,权限有限. 程序用户:1 ...
- spring boot中控制台打印sql日志
.properties文件 logging.level.com.example.demo.dao=debug .yml文件 # 打印sql logging: level: com.example.de ...
- leetcode 删除一张表中重复邮箱的数据,并且保留最小id 的 那条
/* create view testview as SELECT subject,MIN(Id) as id FROM test GROUP BY subject; select * FROM te ...
- ranger 使用外置的solr
一.solrcloud部署 1.下载部署 1).下载二进制包 #wget http://mirrors.tuna.tsinghua.edu.cn/apache/lucene/solr/8.3.0/so ...
- 在Windows环境下搭建Snort+BASE入侵检测系统
操作系统: Windows 7 (service pack 1) 所需软件: 虚拟机:VirtualBox 网络数据包截取驱动程序:WinPcap 4.1.3 (WinPcap_4_1_3.exe) ...