Leetcode——258.各位相加【水题】】的更多相关文章

258. 各位相加 258. Add Digits 题目描述 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. LeetCode258. Add Digits 示例: 输入: 38 输出: 2 解释: 各位相加的过程为: 3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所以返回 2. 进阶: 你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗? Java 实现 class Solution { public int addDigits(i…
258. 各位相加 给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所以返回 2. 进阶: 你可以不使用循环或者递归,且在 O(1) 时间复杂度内解决这个问题吗? 找规律.假设 num = 384 = 3 * 100 + 8 * 10 + 4 第一轮计算 sum = 15 = 3 + 8 + 4 差值 = 3 * 99 + 8 * 9 = (3 *…
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所以返回 2. AC代码: class Solution(object): def addDigits(self, num): """ :type num: int :rtype: int """ num = str(num) num_len = le…
leetcode 34 最早出现和最后出现 class Solution { public int[] searchRange(int[] nums, int target) { int []ans={-1,-1} ; for(int i=0;i<nums.length;i++){ if(nums[i]==target){ ans[0]=i; break; } } for(int j=nums.length-1;j>=0;j--){ if(nums[j]==target){ ans[1]=j;…
258. 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 withou…
问题如下: 给一个非负整数 num,反复添加所有的数字,直到结果只有一个数字. 例如: 设定 num = 38,过程就像: 3 + 8 = 11, 1 + 1 = 2. 由于 2 只有1个数字,所以返回它. 进阶: 你可以不用任何的循环或者递归算法,在 O(1) 的时间内解决这个问题么? 初始的想法: 开始只看到了进阶,要求使用O(1)的时间复杂度,因此我想了一下,既然是int型变量,那么它的范围是-32768~32767,因此最高一共有5位数,所以O(1)算法可以直接使用五个int型变量存储起…
Leetcode 44 实现一种类似正则表达式的字符串匹配功能. 复杂度要求不高, 调代码稍微费点劲.. 好像跟贪心也不太沾边, 总之 *把待匹配串分成若干个子串, 每一个子串尽量在模式串中靠前的部分匹配完成就算贪心了吧.. class Solution { public: bool match(string &s,string &p,int l2,int r2,int l1) { if(l2==r2)return true; if(l1+r2-l2-1>=s.length())re…
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所以返回 2. class Solution: def addDigits(self, num: int) -> int: def hanshu(nums): sum = ): ge = nums % sum += ge nums = ) return sum sum = hanshu(num) ):…
就是简单的模拟一下就可以了.但是我一开始是用一个二维char数组来存的,这样在最终扫全体时会扫很多空的位置,浪费了很多时间,所以就time limit error了. 所以改进一下就用string数组来存.其实可以发现,这个表我们需要的每一行的相对位置,而对于每一列的相对位置根本就无所谓,可以横向随便“拉伸”这个表. class Solution { public: string convert(string s, int numRows) { const int n=numRows; )ret…
ACM小白...非常费劲儿的学习中,我觉得目前我能做出来的都可以划分在水题的范围中...不断做,不断总结,随时更新 POJ: 1004 Financial Management 求平均值 杭电OJ: 1001  Sum Problem 相加1002  A + B Problem II 模拟人工加法-精度计算 1008 Elevator 很水,但是情况要考虑全面…
Codeforces Round #258 (Div. 2) Predict Outcome of the Game C. Predict Outcome of the Game time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There are n games in a football tournament. Three…
Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Being a programmer, you like arrays a lot. For your birthday, your friends ha…
Codeforces Round #258 (Div. 2) Game With Sticks A. Game With Sticks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output After winning gold and silver in IOI 2014, Akshat and Malvika want to hav…
POJ 3176 Cow Bowling 链接: http://poj.org/problem?id=3176 这道题可以算是dp入门吧.可以用一个二维数组从下向上来搜索从而得到最大值. 优化之后可以直接用一维数组来存.(PS 用一维的时候要好好想想具体应该怎么存,还是有技巧的) #include<iostream> #include<cstring> #include<cmath> #include<cstdio> using namespace std;…
BZOJ初级水题列表——献给那些想要进军BZOJ的OIers 代码长度解释一切! 注:以下代码描述均为C++ RunID User Problem Result Memory Time Code_Length 695765 Eolv 1000 Accepted 804 kb 0 ms 118 B 739478 Eolv 2463 Accepted 804 kb 0 ms 134 B 696662 Eolv 1968 Accepted 1272 kb 48 ms 137 B 739546 Eolv…
就是一个组合数水题 偷个图 去掉阴影部分  把整个图看成上下两个矩形 对于上面的矩形求出起点到每个绿点的方案 对于下面的矩形 求出每个绿点到终点的方案 上下两个绿点的方案相乘后相加 就是了 想想为什么 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include…
题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4, n = 6, and the list is [4, 3, 2, 2, 1, 1], then there are four different sums that equal…
在对银行账户等重要权限设置密码的时候,我们常常遇到这样的烦恼:如果为了好记用生日吧,容易被破解,不安全:如果设置不好记的密码,又担心自己也会忘记:如果写在纸上,担心纸张被别人发现或弄丢了... 这个程序的任务就是把一串拼音字母转换为6位数字(密码).我们可以使用任何好记的拼音串(比如名字,王喜明,就写:wangximing)作为输入,程序输出6位数字. 变换的过程如下: 第一步. 把字符串6个一组折叠起来,比如wangximing则变为: wangxi ming 第二步. 把所有垂直在同一个位置…
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3049    Accepted Submission(s): 2364 Problem Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of…
CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit Status Description 对于csuxushu来说,能够在CSU(California State University)上学是他一生的荣幸.CSU校园内的道路设计的十分精巧,由n+1条水平道路和n+1条竖直道路等距交错而成,充分体现了校园深厚的文化底蕴.然而不幸的是CS市每到夏季,天降大雨,…
1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 112[Submit][Status][Web Board] Description 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换. Input 一个3x3的矩阵 Output 转置后的矩阵 Sample Input 1 2 3 4 5 6 7 8 9 Sample Output 1 4 7…
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…
1195: 相信我这是水题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 821  Solved: 219 Description GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,萌萌的师弟师妹们们经常会让pigofzhou帮他们Debug,因为师弟师妹们打代码使用编程语言的种类千奇百怪,pigofzhou为此很头疼.现在假设师弟师妹们只喜欢Java或者C++或者C,因为他希望所有人都学相同的编程语言,只有这样pigofzhou教…
1303: [CQOI2009]中位数图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2340  Solved: 1464[Submit][Status][Discuss] Description 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. Input 第一行为两个正整数n和b ,第二行为1~n 的排列. Output 输出一个整数,即中位数为b的连续子序列个数.…
B - 大还是小? Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Description 输入两个实数,判断第一个数大,第二个数大还是一样大.每个数的格式为: [整数部分].[小数部分] 简单起见,整数部分和小数部分都保证非空,且整数部分不会有前导 0.不过,小数部分的最 后可以有 0,因此 0.0 和 0.000 是一样大的. Input 输入包含不超过 20 组数据.每组数据包含一行,有两个实数(格式如前所述)…
题意就是讲给出的字符串元音字母去掉,在每个辅音字母前加点,且小写输出...注意y也要去掉(以我英语挂科的水平也知道y是辅音字母)... 水题.. 直接上代码好了... #include <iostream> #include <stdio.h> #include <string.h> #define MAXN 100+10 using namespace std; char s[]={"aeiouAEIOUyY"}; int main(void) {…
1061: 从三个数中找出最大的数 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 124[Submit][Status][Web Board] Description 定义一个带参的宏(或者模板函数),从三个数中找出最大的数. Input 3个短整型数,空格隔开 3个实数,空格隔开 3个长整数,空格隔开 Output 最大的数,对于实数保留2位小数. Sample Input 1 2 3 1.5 4.7 3.2 12345…
1059: 判别该年份是否闰年 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 222  Solved: 139[Submit][Status][Web Board] Description 给年份year,定义一个宏,以判别该年份是否闰年.提示:宏名可以定义为LEAP_YEAR,形参为y,既定义宏的形式为 #define LEAP_YEAR(y) (读者设计的字符串) Input 一个年份 Output 根据是否闰年输出,是输出"L",否…
1064: 输入三个字符串,按由小到大的顺序输出 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 471  Solved: 188[Submit][Status][Web Board] Description 输入三个字符串,按由小到大的顺序输出.分别使用指针和引用方式实现两个排序函数.在主函数中输入和输出数据. Input 3行字符串 Output 按照从小到大输出成3行.由指针方式实现. 按照从小到大输出成3行.由引用方式实现.   Sample…
LSS Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 128000/64000 KB (Java/Others) SubmitStatistic Next Problem Problem Description Time flies, four years passed, colleage is over. When I am about to leave, a xuemei ask me an ACM  problem, but…