给你一个整数数组 nums,请你返回其中位数为 偶数 的数字的个数. 示例 1: 输入:nums = [12,345,2,6,7896] 输出:2 解释: 12 是 2 位数字(位数为偶数)  345 是 3 位数字(位数为奇数)   2 是 1 位数字(位数为奇数)  6 是 1 位数字 位数为奇数)  7896 是 4 位数字(位数为偶数)   因此只有 12 和 7896 是位数为偶数的数字 示例 2: 输入:nums = [555,901,482,1771] 输出:1 解释: 只有 17…
题目描述: 方法一:O(N) class Solution: def findNumbers(self, nums: List[int]) -> int: ans=0 for num in nums: if len(str(num))%2==0: ans+=1 return ans 方法二:数学 O(N) class Solution: def findNumbers(self, nums: List[int]) -> int: return sum(1 for num in nums if…
/**  * 找出四位数的全部吸血鬼数字  * 吸血鬼数字是指位数为偶数的数字,能够由一对数字相乘而得到,而这对数字各包括乘积的一半位数的数字,当中从最初的数字中选取的数字能够随意排序.  * 以两个0结尾的数字是不同意的.  *   比例如以下列数字都是吸血鬼数字 1260=21*60 1827=21*87  2187=27*81  ...  * 比較笨的低效率的做法: 遍历全部四位数, 每生成一个四位数的时候,  *         在双重循环遍历两位数,在两位数的内层循环中推断是否与最外层…
/**  * 找出四位数的所有吸血鬼数字  * 吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到,而这对数字各包含乘积的一半位数的数字,其中从最初的数字中选取的数字可以任意排序.  * 以两个0结尾的数字是不允许的.  *   例如下列数字都是吸血鬼数字 1260=21*60 1827=21*87  2187=27*81  ...  * 比较笨的低效率的做法: 遍历所有四位数, 每生成一个四位数的时候,  *         在双重循环遍历两位数,在两位数的内层循环中判断是否与最外层循环…
偶数个数字3 总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 131072kB [描述] 在所有的N位数中,有多少个数中有偶数个数字3? [输入] 一行给出数字N,N<=1000 [输出] 如题 [样例输入] 2 [样例输出] 73 [Solution] 用dp[i][1]表示i位数有奇数个三的数量,dp[i][0]表示有偶数个三的数量.dp[i][1]可以从dp[i-1][1]首位添加0 1 2 4 5 6 7 8 9和dp[i-1][0]首位添加3转移,dp[i…
题意:问所有的N位数中,有多少个有偶数个数字3的数. 解法:f[i][j]表示i位数中含数字3的个数模2为j的个数.于是分第i位填3还是不填3讨论. 小tip:要模12345:for循环新定义了一个变量会慢一点点~ 1 #include<cstdio> 2 #include<cstdlib> 3 #define N 1010 4 #define mod 12345 5 6 int f[2][2]; 7 int main() 8 { 9 int n; 10 scanf("%…
题目:打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个 "水仙花 数 ",因为153=1的三次方+5的三次方+3的三次方. public class Algorithm_Game_03 { public static void main(String[] args) { for(int i = 100 ; i < 999 ; i++){ if(i==(i/100)*(i/10…
从键盘上输入字符,(1)分别统计一下其中字母,数字,其他字符的个数, (2)将统计的字母,数字,其他字符的个数以柱状图的形式打印.例如 5 ***** *****     3 *****   *****     2 *****   *****   ***** *****   *****   ***** alp     num     oth 思路解析: 在统计了字母.数字和其他字符的个数之后,分别记作alpN, numN, othN.取三者最大值hMax.分别取alpN, numN, othN与…
Leetcode 137. 只出现一次的数字 II - 题解 137. Single Number II 在线提交: https://leetcode.com/problems/single-number-ii/ 题目描述 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: 输入: [2,2,3,2] 输出: 3 示例 2: 输入: [0,1,0,1,0,1…
package com.zuoye.test;//打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数,//其各位数字立方和等于该数本身.//例如:153是一个 "水仙花数 ",//因为153=1的三次方+5的三次方+3的三次方.public class Shuixian { public static void main(String[] args) { int a; int b; int c; int sum1=0; int su…
//统计整型变量m中各数字出现的次数,并存放在数组a中,其中,a[0]存放0出现的次数,a[1]存放1出现的次数. #include <stdio.h> ]) { int i; ; i<; i++) /**********found**********/ a[i] = ; ) { /**********found**********/ i = m%; a[i]++; /**********found**********/ m = m/; } } void main() { ],i; pr…
day8 --------------------------------------------------------------- 实例013:所有水仙花数 题目 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方. 分析了下,问题不大,偷个懒导入math包: 1 import math 2 3 floor = [] 4 fo…
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, it reads -121. From right to…
https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA) 面试官,你再问我 Bit Operation 试试? 描述 Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusi…
Long Time No See !   题目链接https://leetcode.com/problems/palindrome-number/?tab=Description   首先确定该数字的位数.按照已知数字x对10进行多次求余运算,可以得到数字位数. 具体思路: 1.每次取出该数字的最高位和最低位进行比较. 2.如果不相等则直接返回FALSE, 3.如果相等修改x的值(去掉最高位也同时去掉最低位)其中去掉最高位可以通过求模运算,去掉最低位可以采用除以10 4.进行循环直到x的值不大于…
地址 https://www.acwing.com/solution/leetcode/content/5801/ 题目描述给你一个整数数组 nums 和一个整数 k. 如果某个子数组中恰好有 k 个奇数数字,我们就认为这个子数组是「优美子数组」. 请返回这个数组中「优美子数组」的数目. 示例 : 输入:nums = [,,,,], k = 输出: 解释:包含 个奇数的子数组是 [,,,] 和 [,,,] . 示例 : 输入:nums = [,,], k = 输出: 解释:数列中不包含任何奇数,…
Given a non-negative integer N, find the largest number that is less than or equal to N with monotone increasing digits. (Recall that an integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.) Examp…
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?…
You are given two linked lists representing two non-negative numbers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not con…
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. Note: The length of num is less than 10002 and will be ≥ k. The given num does not contain any leading zero. Ex…
思路:如果我们在数字前面补0的话,就会发现n位所有10进制数其实就是n个从0到9的全排列.也就是说,我们把数字的每一位都从0到9排列一遍,就得到了所有的10进制数. /** *ch 存放数字 *n n位数 *index 计数值 **/ private function num(ch:Array,n:int,index:int):void { if(index==n) { trace(ch); return; } for(var i:int=0;i<10;i++) { ch[index]=i; n…
给定一个N位数,比如12345,从里面去掉k个数字.得到一个N-k位的数.比如去掉2,4,得到135,去掉1,5.得到234.设计算法.求出全部得到的N-k位数里面最小的那一个. 写的代码例如以下,思路是通过堆排序得到N位数里边最大的前K个数,然后依照原数字的顺序去除得到的最大的K个数. 感觉写的非常乱,可能还有些小问题,鲁棒性应该非常差,努力锻炼..努力提高. typedef unsigned int uint; //Heap adjust function void HeapAdjust(u…
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you implement it usi…
主要用正则表达式在完成对汉字,数字,英文数量的验证. import java.util.Scanner; /* * 统计汉字,数字,英文,other * */ public class Test { public static int count(String text) { // 正则,这两个unicode值正好是Unicode表中的汉字的头和尾."[]"代表里边的值出现一个就可以,后边的"+"代表至少出现1次,合起来即至少匹配一个汉字. String Reg =…
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…
给定正整数 N,返回小于等于 N 且具有至少 1 位重复数字的正整数. 示例 1: 输入:20 输出:1 解释:具有至少 1 位重复数字的正数(<= 20)只有 11 . 示例 2: 输入:100 输出:10 解释:具有至少 1 位重复数字的正数(<= 100)有 11,22,33,44,55,66,77,88,99 和 100 . 示例 3: 输入:1000 输出:262 数位dp可以解决,不知道怎么写.考虑一种通解的方法:原答案即N-没有重复的数字.关键是求没有重复的数字对于位数比N小的情…
1. 题目 2. 解答 字典序排数可以看做是第一层节点分别为 1-9 的十叉树,然后我们在树上找到第 K 小的数字即可.因此,我们需要分别统计以 1-9 为根节点的每个树的节点个数.如果 K 小于当前树的节点个数,那么第 K 小的数字即在当前树中,我们进入子树继续查找:如果 K 大于当前树的节点个数,那么我们需要查找后面树中第 (K - 当前树节点) 小的数字. 其中,比较关键的步骤就是统计树中的节点个数,我们按照逐层统计的方法来进行,详见下图. 首先我们初始化 cur = 1 然后我们让 le…
只出现一次的数字III 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次. 找出只出现一次的那两个元素. 示例 : 输入: [1,2,1,3,2,5] 输出: [3,5] 注意: 结果输出的顺序并不重要,对于上面的例子, [5, 3] 也是正确答案. 你的算法应该具有线性时间复杂度.你能否仅使用常数空间复杂度来实现? 分析 1. 数组中所有数字做异或运算,因为有两个元素只出现一次,所以最后的异或运算结果sum 为两个单独元素x,y的异或结果.因为相同数字异或运算结…
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to determine if a number is strobogrammatic. The number is represented as a string. For example, the numbers "69", "…
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, 让我们判断它是不是回文数字. 首先,负数就不是回文数字,因为有个负号. 剩下的都是正数,只要把数字 reverse 一下,和原来的比较,一样就是回文. 当有overflow 的时候,返回一个负数就可以了(因为负数肯定不会和正数相等). Java Solution: Runtime beats 61.…