给定一个字符串str,求其中全部整数数字之和

本文介绍了一个使用Java实现的简单计算器,它能从输入的字符串中解析并计算包含数字和负号的表达式。通过使用栈来跟踪负号的出现次数,确保了正确处理负数。此外,该程序能识别连续的数字字符并将其作为一个整体进行处理。

参考 https://blog.youkuaiyun.com/duoduo18up/article/details/82193122

 public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String line = scan.nextLine();
        Stack stack=new Stack();
        int result=0;
        for(int i=0;i<line.length();i++){
            String temp=line.charAt(i)+"";
            if(temp.matches("-")){
                stack.push('-');
            }
            else if(temp.matches("\\d")){ //如果是数字
                int count=0;
                while(!stack.empty()){ //先弹出所有的符号
                    stack.pop();
                    count++;
                }

                for(int j=i+1;j<line.length();j++){  //查看后面是否还有数字
                    String other=line.charAt(j)+"";
                    if(other.matches("\\d")) {
                        temp=temp+other;
                        i=j;
                    }
                    else break;

                }
                int result_temp=Integer.valueOf(temp);

                if(count%2!=0) result_temp=-result_temp;
                result=result+result_temp;
            }

        }
        System.out.println(result);
    }

 

以下是一些给定字符串的常见操作及处理方法: - **查找并替换特定词汇**:利用 Python 提供的字符串处理方法,从字符串中查找特定的词汇,并将其替换为另外一个更合适的词。例如,将字符串 "Where there are a will, there are a way" 中的 "are" 替换为 "is",可转换为 "Where there is a will, there is a way" [^1]。 ```python string = "Where there are a will, there are a way" new_string = string.replace("are", "is") print(new_string) ``` - **根据统计字符串索引查找对应字符**:给定一个字符串的统计字符串 `str` 一个整数 `index`,返回 `str` 所代表的原始字符串上的第 `index` 个字符。例如,“a_1b_100” 所代表的原始字符串上第 0 个字符是 ‘a’,第 50 个字符是 ‘b’ [^2]。 ```python def get_char(stat_str, index): current_index = 0 parts = stat_str.split("_") i = 0 while i < len(parts): char = parts[i] count = int(parts[i + 1]) if current_index + count > index: return char current_index += count i += 2 return None stat_str = "a_1b_100" index = 50 print(get_char(stat_str, index)) ``` - **删除操作使字典序最小**:给定一个字符串,对该字符串进行删除操作,保留 `k` 个字符且相对位置不变,使字典序最小。可以使用动态规划解决,`dp[i][j]` 表示从前 `i` 个字符中选择 `j` 个字符的字典序最小字符串 [^3]。 ```python def removeToKeepKMinDP(s: str, k: int) -> str: n = len(s) dp = [["" for _ in range(k + 1)] for _ in range(n + 1)] for i in range(n + 1): dp[i][0] = "" for j in range(1, k + 1): dp[0][j] = "~" for i in range(1, n + 1): for j in range(1, k + 1): option1 = dp[i - 1][j] option2 = dp[i - 1][j - 1] + s[i - 1] if j <= i else "~" dp[i][j] = min(option1, option2) return dp[n][k] s = "bcabc" k = 3 print(removeToKeepKMinDP(s, k)) ``` - **判断字符串是否为回文**:对于大多数实际应用场景,双指针法是判断字符串是否为回文的最优解决方案,它在时间空间复杂度方面都表现得非常高效 [^4]。 ```python def is_palindrome(s): left, right = 0, len(s) - 1 while left < right: if s[left] != s[right]: return False left += 1 right -= 1 return True s = "racecar" print(is_palindrome(s)) ``` - **字符串数字翻转**:对于字符串数字进行翻转输出,对于数字要注意负数的情况,可通过取余 `%` 操作完成。例如,将 -123 翻转得到 -321 [^5]。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int x = sc.nextInt(); int res = 0; while (x != 0) { int b = x % 10; x /= 10; res = res * 10 + b; } System.out.println(res); } } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值