输入字符串s,输出s中包含所有整数的最小和。
说明:
字符串s,只包含a-z A-Z+-;
合法的整数包括:
输入描述:
包含数字的字符串
输出描述:
所有整数的最小和
用例:
| 输入 | bb1234aa |
| 输出 | 10 |
| 说明 | 无 |
Java算法源码:
- import java.math.BigInteger;
- import java.util.Scanner;
- public class Main{
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- System.out.println(getResult(sc.nextLine()));
- }
- public static String getsult(String s){
- boolean isNegative=false;
- StringBuilder negative =new StringBuilder();
- BinInteger ans = new BigInteger("0");
- for(int i=0;i
- char c = s.charAt(i);
- if(isNagative){
- negative.append(c);
- }else{
- ans = ans.add(new BigInteger(c+""));
- }
- }else{
- if(isNegative){
- ans = ans.subtract(new BigInteger(negative.toString()));
- negative = new StringBuilder();
- }
- isNegative = c =='-';
- }
- }
- if(negative.length()>0){
- ans=ans.subtract(new BigInteger(negative.toString()));
- }
- return ans.toString();
- }
- }