1. 问题描述Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.Follow up:Could you do it without any loop/recursion in O(1) runtime?Hint:A naive implementation of the above process is trivial. Could you come up with other methods?What are all...
Given a string ‘str’ of digits and an integer ‘n’, build the lowest possible number by removing ‘n’ digits from the string and not changing the order of input digits.Examples:Input: str = "4325043", n = 3 Output: "2043"Input: str = "765028321", n = 5 Output: "0221"Input: str = "121198", n = 2 Output: "1118"解法一:目标是要得到用字符串表示的最小数字,所以要使高位的数字尽量小,可以使用贪心策略...
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.Follow up:Could you do it without any loop/recursion in O(1) runtime? num = a * 10000 + b * 1000 + c * 100 + d * 10 + e即:num = (a + b + c + d + e) + (a * 9999 + b * 999 + c * 99 + d * 9)1p...
Given a non-negative integer num, repeatedly add all its digits until the result has onlyone digit.
For example:
Given num = 38, the process is like: 3+ 8 = 11, 1 + 1 = 2. Since 2 hasonly one digit, return it.Follow up:
Could you do it without any loop/recursion in O(1) runtime?
题目描述:给出一个数组,不断地分割数字,直到这个数字是一个数字为止。题目很简单,直接上代码。
代码实现:class Solution {
...
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. Each digit must be rotated - we cannot choose to leave it alone.A number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any oth...
用hashMap查重:代码:public class Solution { public boolean isHappy(int n) { Map<Integer, Integer> map = new HashMap<>(); while(!map.containsKey(n)){ map.put(n,1); int result = 0; result += (n%10) * (n%10); while(n/10 > 0){ n /= 10; result += (n%10) * (n%10); } n = result; } ...
1295. Find Numbers with Even Number of Digits(统计位数为偶数的数字)链接https://leetcode-cn.com/problems/find-numbers-with-even-number-of-digits题目给你一个整数数组?nums,请你返回其中位数为?偶数?的数字的个数。示例 1:输入:nums = [12,345,2,6,7896]
输出:2
解释:
12 是 2 位数字(位数为偶数)?
345 是 3 位数字(位数为奇数)??
2 是 1 位数字(位数为奇数)?
6 是 1 位数字 位数为奇数)?
7896 是 4 位数字(...
/*注意注意:本题非hdu4333原题,而是简化版,原版有多组数据。但此代码在修改输入后依旧可以通过多组数据*/ 给出一个数字串,问有多少本质不同同构串比原串小,一样,大.同构串是指,对于原串S[1-N]通过旋转变成同构串S[i-N]+S[0-i-1].本质不同,指的是字符串不一样.输入格式:一行一个数字串输出格式:一行,输出本质不同的同构串比原串小,一样,大的三个数,用一个空格分隔开。样例输入:123123样例输出:0 1 2数据范围:
数字串长度...
DP+快速矩阵幂。注意base矩阵的初始化,不难。 1/* 5564 */ 2 #include <iostream>3 #include <string>4 #include <map>5 #include <queue>6 #include <set>7 #include <stack>8 #include <vector>9 #include <deque>10 #include <algorithm>11 #include <cstdio>12 #include <cmath>13 #include <ctime>14 #include <cstring>15 #include <climits>16 #include <cctype>17 #include <cassert>18 #include <functional>19 #inclu...
Revolving DigitsTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24215 Accepted Submission(s): 5268Problem DescriptionOne day Silence is interested in revolving the digits of a positive integer. In the revolving operation, he can put several last digits to the front of the integer. Of course, he can put all the digits to the front, so he wi...
/*
题意:
给出数组A,有以下几个操作:
1: AND(opn, L, R):把区间[L, R]中的元素A[i]改为A[i] & opn;;;;;;
2: OR(opn, L, R) :把区间[L, R]中的元素A[i]改为A[i] | opn;;;;;;;
3: XOR(opn, L, R):把区间[L, R]中的元素A[i]改为A[i] ^ opn;;;;;;;
4: SUM(L, R) :对区间[L, R]中的元素求和;;;;
--------------------------------------------------------------------------------
线段树区间修改的题目:
----------------...
前置知识\(dp[i][j][k]\)表示\(i\)长,\(mod7=j\),这个位置选了\(k\)的方案数。\(dp[i+1][(j*10+x)mod7][x]+=dp[i][j][y];//x+y!=K?\)\(Ma.a[i][j]=1\)表示\(i\)状态->\(j\)状态可以转移如果设\(ans=qkpow(Ma,K)\)。则\(ans.a[i][j]\)表示走\(K\)步之后状态\(i\)到状态\(j\)的方案数。如果对这个不熟悉可以看看HDU5607题解然后对于本题而言,就要构建转移的矩阵。注意到对于后两维而言,其实他们的转移都是一样的,与\(i\)没有关系...
题目:给你一个数字n,一个数字b,问n!转化成b进制后的位数和尾数的0的个数。分析:数论。 末尾的0,当10进制时。有公式 f(n)= f(n/5)+ n/5; (令k = n/5 则 n! = 5k * 5(k-1) * ... * 10 * 5 * a = 5^k * k! * a {a为不能整除5的部分}) ( 即 f(n) = k + f(k) = n/5 + f( n/5 ) { f(0) = 0 } ) 类似可推导 f( n, b ) = f( n/o ) + n/o,o为b的最大质因子p组成的最大因子...
1、给一个数字字符串s,可以把它的最后一个字符放到最前面变为另一个数字,直到又变为原来的s。求这个过程中比原来的数字小的、相等的、大的数字各有多少。例如:字符串123,变换过程:123 -> 312 -> 231 -> 123因为:312>123, 231>123, 123=123所以答案是:0 1 22、令str1=s,str2=s+s,然后str1作为子串,str2作为主串,进行扩展kmp求出str2[i...len2-1]与str1[0...len1-1]的最长公共前缀。当公共前缀==len1时,两个数相等;否则...
DescriptionNow we have a number, you can swap any two adjacent digits of it, but you can not swap more than K times. Then, what is the largest probablenumber that we can get after your swapping?InputThere is an integer T (1 <= T <= 200) in the first line, means there are T test cases in total.For each test case, there is an integer K (0 <= K < 106) in the first line, which has the same meaning as ...