华为OD:求字符串中所有整数的最小和

输入字符串s,输出s中包含所有整数的最小和。

说明:

字符串s,只包含a-z A-Z+-;

合法的整数包括:

  1. 正整数一个或者多个0-9组成,如 0 2 3 002 202
  2. 负整数负号-开头,数字部分由一个或者多个0-9组成,如-0 -012 -23 -00023

输入描述:

包含数字的字符串

输出描述:

所有整数的最小和

用例:

|----|----------|
| 输入 | bb1234aa |
| 输出 | 10 |
| 说明 | 无 |

Java算法源码:

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<s.length();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();
}
}
相关推荐
小羊没烦恼!1 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
倒头就睡的小比特1 天前
算法竞赛C++常用的STL
c++·算法
俊昭喜喜里1 天前
java中的继承和多态的区别
java
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕1 天前
JSONObject与JSONArray封装数据格式区别
java·json
胡写代码1 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖1 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
此时不提桶,更待何时1 天前
01-06-A-JVM排查实战详解
java·jvm
猎头南楼1 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
vipxieliang1 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot