【LeetCode】Plus One && 【九度】题目1198:a+b
2014-01-03 13:56
1031 查看
Plus One
Total Accepted: 4206 Total Submissions: 14127 My Submissions
Given a number represented as an array of digits, plus one to the number.
题目意思为给定一个数组,这个数组内的数代表某个大数,也就是可能为100位甚至多少位的一个数。
例如123456789012345678901234567890000,那么digits的每个位必然为一个个位数。
两个大数相加,可以使用数组,按位累加。
大于10就进位,同时将当前位-1,并且上一位加1。
Java AC
LeetCode Add Binary 也是大数相加,只不过这次是二进制,知道进制相加的原则即可。
Java AC
九度题目1198:a+b 2010年华中科技大学计算机研究生机试真题是一类题。
当时用了三种解法去解决。
一并给代码。
方法1 Java AC
方法2 Java AC
方法3 Java AC
Total Accepted: 4206 Total Submissions: 14127 My Submissions
Given a number represented as an array of digits, plus one to the number.
题目意思为给定一个数组,这个数组内的数代表某个大数,也就是可能为100位甚至多少位的一个数。
例如123456789012345678901234567890000,那么digits的每个位必然为一个个位数。
两个大数相加,可以使用数组,按位累加。
大于10就进位,同时将当前位-1,并且上一位加1。
Java AC
public class Solution { public int[] plusOne(int[] digits) { int len = digits.length; int addNum[] = new int[len + 1]; addNum[len] = 1; for (int i = len - 1; i >= 0; i--) { addNum[i + 1] += digits[i]; if (addNum[i + 1] >= 10) { addNum[i + 1] -= 10; addNum[i] += 1; } } if (addNum[0] == 0) { System.arraycopy(addNum, 1, digits, 0, len); return digits; }else { return addNum; } } }
LeetCode Add Binary 也是大数相加,只不过这次是二进制,知道进制相加的原则即可。
Java AC
public class Solution { public String addBinary(String a, String b) { if(a == null || b == null){ return ""; } if("".equals(a) || "".equals(b)){ return ""; } if("".equals(a.replaceAll("0", "")) && "".equals(b.replaceAll("0", ""))){ return "0"; } int len1 = a.length(); int len2 = b.length(); char arrayA[] = a.toCharArray(); char arrayB[] = b.toCharArray(); int maxLen = Math.max(len1,len2); int minLen = Math.min(len1,len2); int array[] = new int[maxLen+1]; int k = maxLen; if(maxLen == len1){ for(int i = minLen-1; i >= 0; i--){ array[k] += arrayA[k-1] - '0' + arrayB[i] - '0'; if(array[k] >= 2){ array[k] -= 2; array[k-1] += 1; } k--; } while(k > 0){ array[k] += arrayA[k-1] - '0'; if(array[k] >= 2){ array[k] -= 2; array[k-1] += 1; } k--; } }else{ for(int i = minLen-1; i >= 0; i--){ array[k] += arrayA[i] - '0' + arrayB[k-1] - '0'; if(array[k] >= 2){ array[k] -= 2; array[k-1] += 1; } k--; } while(k > 0){ array[k] += arrayB[k-1] - '0'; if(array[k] >= 2){ array[k] -= 2; array[k-1] += 1; } k--; } } StringBuffer sb = new StringBuffer(); int i = 0; if(array[0] == 0){ i = 1; } for(; i < maxLen+1; i++){ sb.append(array[i]); } return sb.toString(); } }
九度题目1198:a+b 2010年华中科技大学计算机研究生机试真题是一类题。
当时用了三种解法去解决。
一并给代码。
方法1 Java AC
import java.math.BigDecimal; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ String a = scanner.next(); String b = scanner.next(); BigDecimal ad = new BigDecimal(a); BigDecimal bd = new BigDecimal(b); BigDecimal c = ad.add(bd); System.out.println(c); } } } /************************************************************** Problem: 1198 User: wzqwsrf Language: Java Result: Accepted Time:980 ms Memory:84752 kb ****************************************************************/
方法2 Java AC
import java.util.Scanner; public class Main { /* * 1198 */ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ String a = scanner.next(); String b = scanner.next(); int alength = a.length(); int blength = b.length(); int minlength = Math.min(alength, blength); int maxlength = Math.max(alength, blength); int in = 0 ; String result = ""; String maxStr = alength>blength?a:b; String minStr = alength<blength?a:b; int c = maxlength-minlength; for (int i = minlength -1; i >= 0; i--) { int sa = minStr.charAt(i)-'0'; int sb = maxStr.charAt(c + i)-'0'; int count = sa+sb+in; if(count >= 10){ in = 1; result = count-10+result; }else{ in = 0 ; result = count + result; } } for (int i = c-1 ; i >= 0 ; i--) { int max = maxStr.charAt(i)-'0'; int count = max+in; if(count>=10){ in = 1; result = count-10+result; }else{ in = 0 ; result = count + result; } } if(in==1){ result = in +result ; } System.out.println(result); } } } /************************************************************** Problem: 1198 User: wzqwsrf Language: Java Result: Accepted Time:1120 ms Memory:210296 kb ****************************************************************/
方法3 Java AC
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ String a = scanner.next(); String b = scanner.next(); int aLength = a.length(); int bLength = b.length(); int max = Math.max(aLength, bLength); int[] arrayA = new int[max+1]; int[] arrayB = new int[max+1]; int k = 0; for (int i = max+1-aLength ; i< max+1; i++ ) { arrayA[i] = a.charAt(k)-'0'; k++; } k = 0; for (int i = max+1-bLength ; i< max+1; i++ ) { arrayB[i] = b.charAt(k)-'0'; k++; } int[] arrayC = new int[max+1]; for (int i = max ; i> 0 ;i--) { arrayC[i] += arrayA[i] + arrayB[i]; if (arrayC[i]>=10) { arrayC[i] -= 10; arrayC[i-1] += 1; } } if (arrayC[0] == 0) { for (int i = 1; i < arrayC.length; i++) { System.out.print(arrayC[i]); } System.out.println(); }else { for (int i = 0; i < arrayC.length; i++) { System.out.print(arrayC[i]); } System.out.println(); } } } } /************************************************************** Problem: 1198 User: wzqwsrf Language: Java Result: Accepted Time:2290 ms Memory:95360 kb ****************************************************************/
相关文章推荐
- [LeetCode]Length of Last Word&Plus One
- 【九度】题目1548:平面上的点 && 【LeetCode】Max Points on a Line
- 矩阵旋转系列之【LeetCode】Rotate Image && 【九度】题目1164:旋转矩阵
- 【九度】题目1388:跳台阶 && 【LeetCode】Climbing Stairs
- 【LeetCode】Two Sum && 【九度】题目1352:和为S的两个数字
- 【九度】题目1090:路径打印 && 【LeetCode】Simplify Path
- 【LeetCode】Remove Nth Node From End of List && 【九度】题目1517:链表中倒数第k个结点
- 【LeetCode】Search a 2D Matrix && 【九度】题目1384:二维数组中的查找
- 【LeetCode】N-Queens && 【九度】题目1140:八皇后
- 【LeetCode】N-Queens II && 【九度】题目1254:N皇后问题
- 【LeetCode】66 & 67- Plus One & Add Binary
- 【LeetCode】Min Stack && 【九度】题目1522:包含min函数的栈
- 【LeetCode】Longest Palindromic Substring && 【九度】题目1528:最长回文子串(腾讯2013年实习生招聘二面面试题)
- 【LeetCode】Longest Substring Without Repeating Characters && 【九度】题目1530:最长不重复子串
- 【九度OJ】题目1198:a+b 解题报告
- leetcode Plus One 加一
- LeetCode "Plus One Linked List"
- LeetCode All in One 题目讲解汇总(持续更新中...)
- Leetcode Plus One
- leetcode之plus One