Polycarp knows that if the sum of the digits of a number is divisible by 3, then the number itself is divisible by 3. He assumes that the numbers, the sum of the digits of which is divisible by 4, are also somewhat interesting. Thus, he considers a p…
Polycarp knows that if the sum of the digits of a number is divisible by 33, then the number itself is divisible by 33. He assumes that the numbers, the sum of the digits of which is divisible by 44, are also somewhat interesting. Thus, he considers…
题目链接:http://codeforces.com/problemset/problem/1183/A 题意:求不小于 a 且每位数和能被 4 整除的 最小 n 思路:暴力模拟就好. AC代码: #include<bits/stdc++.h> using namespace std; int main() { int a; cin >> a; bool flag = true; ; while(flag) { int t = a; ans = ; while(t) { ans +…
题目 主元素 III 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的1/k. 样例 ,返回 3 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O(k) 解题 上一题刚介绍过所用的方法,但是这个确实很复杂的 先利用HashMap实现 public class Solution { /** * @param nums: A list of integers * @param k: As described * @return: The major…
Biggest Number http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30851#problem/F 解题思路:DFS(检索)+BFS(探路)=强剪枝 一看就知道是搜索题,只是,要是DFS裸搜,真的会超时.时间,非常紧! 做该题的时候,要无时无刻不忘剪枝,抓住一切剪枝的机会,剪吧! (1)用flag记录当前检索的值与当前已经检索到的最大值的关系,一但有机会,就要剪掉那些检索到的值会比当前最大值要小的“残枝败叶”. (2)每次DF…
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0,   F(1) = 1 F(N) = F(N - 1) + F(N - 2), for N > 1. Given…
题目: Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until…
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_{i-1}+F_{i-2}\) for $ i \geq 2$. Problem Description Task.Given an integer \(n\),find the \(n\)th Fibonacci number \(F_n\). Input Format.The input con…
题目 主元素II 给定一个整型数组,找到主元素,它在数组中的出现次数严格大于数组元素个数的三分之一. 样例 给出数组[1,2,1,2,1,3,3] 返回 1 注意 数组中只有唯一的主元素 挑战 要求时间复杂度为O(n),空间复杂度为O(1). 解题 利用HashMap,key值是元素值,value是出现次数,但是时间复杂度和空间复杂度都是O(N) public class Solution { /** * @param nums: A list of integers * @return: Th…
这是悦乐书的第310次更新,第331篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第179题(顺位题号是754).你站在无限数字线的0号位置.在目的地有个target.在每次移动中,您可以向左或向右移动.在第n次移动(从1开始)期间,你可以走n步.返回到达目的地所需的最少步骤数.例如: 输入:target = 3 输出:2 说明:在第一步中,我们从0步进到1.在第二步,我们从1步骤到3. 输入:target = 2 输出:3 说明:在第一步中,我们从0步进到1.在第…
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始几行代码内容,有一些不规范的地方,比如函数名大小写问题等等:更合理的代码实现参考我的github repo 1.读题 Determine whether an integer is a palindrome. Do this without extra space. 这一题描述很简单,判断一个数字是…
这是悦乐书的第222次更新,第235篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第89题(顺位题号是414).给定非空的整数数组,返回此数组中的第三个最大数字.如果不存在,则返回最大数量.时间复杂度必须在O(n)中.例如: 输入:[3,2,1] 输出:1 说明:第三个最大值为1. 输入:[1,2] 输出:2 说明:第三个最大值不存在,因此返回最大值2. 输入:[2,2,3,1] 输出:1 说明:请注意,此处的第三个最大值表示第三个最大不同的数字.值为2的两个数字都…
这是悦乐书的第219次更新,第231篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第86题(顺位题号是405).给定一个整数,写一个算法将其转换为十六进制.对于负整数,使用二进制补码方法.例如: 输入:26 输出:"1a" 输入:-1 输出:"ffffffff" 注意: 十六进制(a-f)中的所有字母必须为小写. 十六进制字符串不得包含额外的前导0.如果数字为零,则由单个零字符"0"表示;否则,十六进制字符串中的第一…
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1:          Input: 121            Output: true Example 2:          Input: -121          Output: false                Explanat…
Description In mathematics, a square number is an integer that is the square of an integer. In other words, it is the product of some integer with itself. For example, 9 is a square number, since it can be written as 3 * 3. Given an array of distinct…
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu…
现有n个Zack,每个Zack有一个一个能力值,从这n个Zack中选出k个,使他们的最大公约数的值最大 [输入格式] 第一行有一个整数n,代表有n个Zack 第二行n个整数ai,代表每个Zack的能力值 [输出格式] 一共n行,第n行为k=i情况下的能力值的最大公因数 [样例输入] 4 1 2 3 4 [样例输出] 4 2 1 1 所有有关约数的题我们可以通过它们的共同因子来进行运算: 这道题我们可以统计对于每个因子x,有多少个数拥有因子x: 这样我们循环模拟来对答案回答: #include <…
1.题目 9. Palindrome Number   Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right,…
B. Long Number You are given a long decimal number aa consisting of nn digits from 11 to 99. You also have a function ff that maps every digit from 11 to 99 to some (possibly the same) digit from 11 to 99. You can perform the following operation no m…
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_{i-1}+F_{i-2}\) for $ i \geq 2$. Problem Description Task.Given two integers \(n\) and \(m\), output \(F_n \ mod \ m\)(that is, the remainder of \(F_n…
题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度,O(1)的额外空间复杂度 解题 根据落单的数I,可以想到,所有的数进行异或运行的结果就是所求两个数的异或结果. 这个异或的结果,二进制数是1的位置说明这两个数对应的二进制位不相同.然后再怎么还原???参考,理解的不是很透,找到第k位后,再判断数组中所以数的第k位是0 还是1,,出现两次的数对求解无影…
题目 落单的数 II 给出3*n + 1 个的数字,除其中一个数字之外其他每个数字均出现三次,找到这个数字. 样例 给出 [1,1,2,3,3,3,2,2,4,1] ,返回 4 挑战 一次遍历,常数级的额外空间复杂度 解题 可以利用HashMap直接解决,时间复杂度和空间复杂度都是O(N) 1.map中存在该元素则:map.put(num,map.get(num) + 1) 2.map中不存在该元素则:map.put(num , 1) 3.map中这个元素出现次数等于三次,则删除该元素 空间复杂…
题目: 给定一个整数序列··有两个操作: add x,表示序列中加入x mid 表示询问这个序列的中位数 原始序列数量n<=100000,操作数m<=10000 题解: 这道题可以直接用权值线段树做···先离散化一下就行·· 然而最快的方法是用优先队列··将先开始序列排序后分成左右两部分···左边用大根堆褚岑··右边用小根堆储存··在操作的时候维护即可·· 代码: #include<iostream> #include<cstdio> #include<cstdl…
DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ... shows the first 20 humble numbers. Write a program to find and print…
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treated as an…
Paint Fence 要点: 这题是求number of ways,如果是相邻不相同颜色,那么就trivial了:k*(k-1)^(n-1).所以这里no more than two adjacent fence posts就是不能连续出现3个相同颜色的. 注意这题的解序列都是连续的,所以dp表示的就是以当前fence结束的情况. 这题要用两个dp variables:same:表示最近两个posts是相同颜色,diff:表示最近两个posts是不同color.why?首先这两种情况是excl…
Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 思路: 这个题是Single Number的进阶版,如果其他的数都出现两次而…
转自:http://blog.csdn.net/shahdza/article/details/7779324 [HDU]2255 奔小康赚大钱 模板题★1533 Going Home 模板题★2426 Interesting Housing Problem KM★3395 Special Fish KM★2282 Chocolate KM★2813 One fihgt one KM★1853 Cyclic Tour 最小费用圈覆盖★★3488 Tour 最小费用圈覆盖★★3435 A new…
原题: 434. Number of Segments in a String 解题: 刚看到题目时,觉得可以通过统计空格个数,但想想有可能会有多个空格的情况 思路: 一:遍历字符,if条件碰到非空格时,计数加1,然后while循环跳过非空格字符,一直到最后 二:设置flag初始为0,当碰到非空格时,计数加1,且flag置1,当flag为1且碰到空格时,flag再重置为0 思路一是自己想的,思路二更巧妙,是论坛里摘抄的 AC代码: 思路一: class Solution { public: in…
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive…