Leetcode Tags(6)Math
一、204. Count Primes
Count the number of prime numbers less than a non-negative number, n.
Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
1.数学原理:两个质数的乘积一定是合数。一个质数乘以任何数的积都一定不是质数。(除了1)
2.代码:需要注意的点:for (int j = 2; j * i < n; j++) notPremes[i * j] = true;
public int countPrimes(int n) {
boolean[] notPremes = new boolean[n];
int count = 0;
for (int i = 2; i < n; i++) {
if (!notPremes[i]) {
count ++;
for (int j = 2; j * i < n; j++) notPremes[i * j] = true;
}
}
return count;
}
二、441. Arranging Coins
n = 5
The coins can form the following rows:
¤
¤ ¤
¤ ¤
Because the 3rd row is incomplete, we return 2. n = 8
The coins can form the following rows:
¤
¤ ¤
¤ ¤ ¤
¤ ¤
Because the 4th row is incomplete, we return 3.
1.数学原理:即求x满足:1+2+3+...+x <= n,即(1+x)*x<=n,解得x
2.代码:注意:如果使用8*n,可能会造成overflow,但是如果改成8.0*n,那么会自动转换成double类型的,就不会造成值大于Integer.Max_Value的情况。
public int arrangeCoins(int n) {
return (int) ((Math.sqrt(1 + 8.0 * n) - 1) / 2);
}
三、258. Add Digits(不使用循环或者递归,并且在O(1)时间复杂度内解决这个问题。)
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。
输入: 38
输出: 2
解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数,所以返回 2。
1.思路:多写几个找出规律来:1 + (num - 1)%9【-1%9=-1】
输入:0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30...
输出:0,1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9, 1,2,3,4,5,6,7,8,9, 1,2,3...
四、836. Rectangle Overlap
矩形以列表 [x1, y1, x2, y2] 的形式表示,其中 (x1, y1) 为左下角的坐标,(x2, y2) 是右上角的坐标。
如果相交的面积为正,则称两矩形重叠。需要明确的是,只在角或边接触的两个矩形不构成重叠。
给出两个矩形,判断它们是否重叠并返回结果。
输入:rec1 = [0,0,2,2], rec2 = [1,1,3,3]
输出:true
输入:rec1 = [0,0,1,1], rec2 = [1,0,2,1]
输出:false
1.思路:两种方法:(1)按照点的位置考虑(2)按照重合的矩形的区域考虑
(1)按照点的位置考虑
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
return !(rec1[2] <= rec2[0] || // left
rec1[3] <= rec2[1] || // bottom
rec1[0] >= rec2[2] || // right
rec1[1] >= rec2[3]); // top
}
(2)按照重合的矩形的区域考虑
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
return (Math.min(rec1[2], rec2[2]) > Math.max(rec1[0], rec2[0]) && // width > 0
Math.min(rec1[3], rec2[3]) > Math.max(rec1[1], rec2[1])); // height > 0
}
五、326. Power of Three(TODO)
不使用循环或者递归来判断给定的整数是否是 3 的幂次方。
六、263. Ugly Number
丑数就是只包含质因数 2, 3, 5 的正整数。
1.遍历2,3,4,5,这里的4由于是和2重复,因此没有任何影响。注意表达:for (int i=2; i<6 && num>0; i++)
for (int i=2; i<6 && num>0; i++)
while (num % i == 0)
num /= i;
return num == 1;
2.直接方法,如果例如22 * 32 * 52 * 7这个数,
在第一个while之后,变为:1 * 32 * 52 * 7
在第二个while之后,变为:1 * 1 * 52 * 7
在第三个while之后,变为;1 * 1 * 1 * 7
public boolean isUgly(int num) {
if(num==1) return true;
if(num==0) return false;
while(num%2==0) num=num>>1;
while(num%3==0) num=num/3;
while(num%5==0) num=num/5;
return num==1;
}
七、172. Factorial Trailing Zeroes(TODO)
给定一个整数 n,返回 n! 结果尾数中零的数量。
输入: 3
输出: 0
解释: 3! = 6, 尾数中没有零。
输入: 5
输出: 1
解释: 5! = 120, 尾数中有 1 个零.
说明: 你算法的时间复杂度应为 O(log n) 。
思路:
八、633. Sum of Square Numbers(TODO)
Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
Input: 3
Output: False
思路:
九、400. Nth Digit(TODO)
在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字。
n 是正数且在32为整形范围内 ( n < 231)。
输入:3
输出:3
输入:11
输出:0,第11个数字在序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 里是0,它是10的一部分。
Leetcode Tags(6)Math的更多相关文章
- Leetcode Tags(13)Tree
1.前序.中序.后序递归方式遍历二叉树 public void preOrderRecur(Node T) { if (T != null) { System.out.print(T.val + &q ...
- Leetcode Tags(8)Binary Search
一.475. Heaters 输入: [1,2,3],[2] 输出: 1 解释: 仅在位置2上有一个供暖器.如果我们将加热半径设为1,那么所有房屋就都能得到供暖. 输入: [1,2,3,4],[1,4 ...
- Leetcode Tags(3)String(TODO)
一.Easy 696 Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 su ...
- Leetcode Tags(2)Array
一.448. Find All Numbers Disappeared in an Array 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了 ...
- Leetcode Tags(13)Bit Manipulation
一.477.汉明距离总和 输入: , , 输出: 解释: 在二进制表示中,4表示为0100,14表示为1110,2表示为0010.(这样表示是为了体现后四位之间关系) HammingDistance( ...
- Leetcode Tags(5)Hash Table
一.500. Keyboard Row 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 输入: ["Hello", "Alaska", &q ...
- Leetcode Tags(1)Linked List
1.知识点回顾 https://www.cnblogs.com/BigJunOba/p/9174206.html https://www.cnblogs.com/BigJunOba/p/9174217 ...
- Leetcode Tags(4)Stack & Queue
一.232. Implement Queue using Stacks private Stack<Integer> stack; /** Initialize your data str ...
- [LeetCode]题解(python):030-Substring with Concatenation of All Words
题目来源 https://leetcode.com/problems/substring-with-concatenation-of-all-words/ You are given a string ...
随机推荐
- electron教程(一): electron的安装和项目的创建
我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(二): http服务器, ws服务器, 进程管理 electron教程(三): 使 ...
- Spring boot - 梳理 - 根本上说,Spring Boot项目只不过是一个普通的Spring项目,只是使用了Spring Boot的起步依赖和自动配置
根本上说,Spring Boot项目只不过是一个普通的Spring项目,只是使用了Spring Boot的起步依赖和自动配置
- 冒泡排序--JavaScript描述
相信凡是编程入门的都接触过冒泡排序算法,排序算法在编程中经常用到. 1. code /** * 冒泡排序 * 1.比较的轮数等于总数 - 1 * 2.比较次数等于要比较的个数 - 1 * --比较从第 ...
- GitHub项目徽标
前言 GitHub徽标,GitHub Badge,你也可以叫它徽章.就是在项目README中经常看到的那些表明构建状态或者版本等信息的小图标.就像这样: 这些好看的小图标不仅简洁美观,而且包含了清晰易 ...
- netty源码解解析(4.0)-23 ByteBuf内存管理:分配和释放
ByteBuf内存分配和释放由具体实现负责,抽象类型只定义的内存分配和释放的时机. 内存分配分两个阶段: 第一阶段,初始化时分配内存.第二阶段: 内存不够用时分配新的内存.ByteBuf抽象层没有定义 ...
- SpringCloud系列-利用Feign实现声明式服务调用
上一篇文章<手把手带你利用Ribbon实现客户端的负载均衡>介绍了消费者通过Ribbon调用服务实现负载均衡的过程,里面所需要的参数需要在请求的URL中进行拼接,但是参数太多会导致拼接字符 ...
- 04-12 scikit-learn库之随机森林
目录 scikit-learn库之随机森林 一.RandomForestClassifier 1.1 使用场景 1.2 代码 1.3 参数 1.4 属性 1.5 方法 二.RandomForestRe ...
- 基于Spring AOP实现的权限控制
1.AOP简介 AOP,面向切面编程,往往被定义为促使软件系统实现关注点的分离的技术.系统是由许多不同的组件所组成的,每一个组件负责一块特定的功能.除了实现自身核心功能之外,这些组件还经常承担着额外的 ...
- HDU 1428漫步校园
漫步校园 Problem Description LL最近沉迷于AC不能自拔,每天寝室.机房两点一线.由于长时间坐在电脑边,缺乏运动.他决定充分利用每次从寝室到机房的时间,在校园里散散步.整个HDU校 ...
- CSP2019 考前复习
动态规划 [NOIP2016]愤怒的小鸟(状压+思维) 多组数据题 共有i只猪,给出每只猪的坐标,鸟的飞行轨迹为经过原点的抛物线,求最少要多少只鸟能消灭所有的猪 \[ 猪数量n<=18 \] 看 ...