[LintCode]各位相加】的更多相关文章

描述: 给出一个非负整数 num,反复的将所有位上的数字相加,直到得到一个一位的整数. 给出 num = 38. 相加的过程如下:3 + 8 = 11,1 + 1 = 2.因为 2 只剩下一个数字,所以返回 2. 分析: 这道题并不难,只能说用好递归吧. 方法一: public int addDigits(int num) { // write your code here if(num / 10 == 0){ return num; }else{ return addDigits(sum(nu…
题目描述: 我的代码 public class Solution { /* * @param num: a non-negative integer * @return: one digit */ public int addDigits(int num) { // write your code here int[] n = new int[10]; int f = -1,sum=0; //当num是个位数的时候退出循环 while(num/10 != 0) { //循环取得num的各位数 w…
You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the sum as…
Given two binary strings, return their sum (also a binary string). Have you met this question in a real interview? Yes Example a = 11 b = 1 Return 100 LeetCode上的原题,请参见我之前的博客Add Binary. class Solution { public: /** * @param a a number * @param b a num…
题目1 落单的数 给出2*n + 1 个的数字,除其中一个数字之外其他每个数字均出现两次,找到这个数字. 链接:http://www.lintcode.com/zh-cn/problem/single-number/ 样例 给出 [1,2,2,1,3,4,3],返回 4 挑战 一次遍历,常数级的额外空间复杂度 解决方案 方法1思路:将所有的数转换成二进制,因为是int类型,共32位.申请常数级(32位)的额外空间,然后每个数对应的位相加,最后对应位上的和模2.最后的结果就是单个数对应的二进制数.…
题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个指针分别从左从右开始扫描,每次判断这两个数相加是不是符合题意,如果小了,那就把左边的指针向右移,同理右指针.然后在备份的数组里找到位置. class Solution: """ @param numbers : An array of Integer @param target…
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow: F(k…
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度. 如: 给出[100, 4, 200, 1, 3, 2], 最长的连续元素序列是[1, 2, 3, 4].返回它的长度:4. 你的算法必须有O(n)的时间复杂度 . 解法: 初始思路 要找连续的元素,第一反应一般是先把数组排序.但悲剧的是题目中明确要求了O(n)的时间复杂度,要做一次排序,是不能达…
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (2016-02-10) For more problems and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. See cnblogs t…
本文出处:http://www.cnblogs.com/wy123/p/6217772.html 字符串自身相加, 虽然赋值给了varchar(max)类型的变量,在某些特殊情况下仍然会被“截断”,这到底是varchar(max)长度的问题还是操作的问题? 1,两个不超过8000长度的字符串自身相加,其结果长度超过8000之后会被截断: 不多说,直接上例子:定义一个字符串,赋值给 varchar(max)类型的变了,字符创长度为4040没有,任何问题. 把4040长度的字符串复制一份出来,也就是…