本文主要是介绍【LeetCode】Plus One 【九度】题目1198:a+b,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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
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: 1198User: wzqwsrfLanguage: JavaResult: AcceptedTime:980 msMemory: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: 1198User: wzqwsrfLanguage: JavaResult: AcceptedTime:1120 msMemory: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: 1198User: wzqwsrfLanguage: JavaResult: AcceptedTime:2290 msMemory:95360 kb
****************************************************************/
这篇关于【LeetCode】Plus One 【九度】题目1198:a+b的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!