华为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();
}
}
相关推荐
Omics Pro4 分钟前
端到端单细胞空间组学数据分析
大数据·数据库·人工智能·算法·数据挖掘·数据分析·aigc
迈巴赫车主4 分钟前
错位排序算法
开发语言·数据结构·算法·排序算法
zzb15804 分钟前
Agent记忆与检索
java·人工智能·python·学习·ai
炽烈小老头6 分钟前
【每日天学习一点算法 2026/03/31】不同路径
学习·算法
Darkwanderor6 分钟前
搜索优化——迭代加深dfs
c++·算法·深度优先·迭代加深
计算机安禾18 分钟前
【数据结构与算法】第17篇:串(String)的高级模式匹配:KMP算法
c语言·数据结构·学习·算法·visual studio code·visual studio·myeclipse
大萌神Nagato20 分钟前
力扣HOT100 Q146LRU缓存
算法·leetcode·缓存
源码之家26 分钟前
大数据毕业设计汽车推荐系统 Django框架 可视化 协同过滤算法 数据分析 大数据 机器学习(建议收藏)✅
大数据·python·算法·django·汽车·课程设计·美食
nianniannnn28 分钟前
力扣 3.无重复字符的最长子串
c++·算法·leetcode
种时光的人32 分钟前
Java+AI 无缝衔接:Spring AI 聊天模型入门到精通
java·人工智能·spring·ai·spring ai