当前位置:网站首页>Leetcode daily question (2232. minimize result by addressing parents to expression)
Leetcode daily question (2232. minimize result by addressing parents to expression)
2022-07-03 09:33:00 【wangjun861205】
You are given a 0-indexed string expression of the form “+” where and represent positive integers.
Add a pair of parentheses to expression such that after the addition of parentheses, expression is a valid mathematical expression and evaluates to the smallest possible value. The left parenthesis must be added to the left of ‘+’ and the right parenthesis must be added to the right of ‘+’.
Return expression after adding a pair of parentheses such that expression evaluates to the smallest possible value. If there are multiple answers that yield the same result, return any of them.
The input has been generated such that the original value of expression, and the value of expression after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.
Example 1:
Input: expression = “247+38”
Output: “2(47+38)”
Explanation: The expression evaluates to 2 _ (47 + 38) = 2 _ 85 = 170.
Note that “2(4)7+38” is invalid because the right parenthesis must be to the right of the ‘+’.
It can be shown that 170 is the smallest possible value.
Example 2:
Input: expression = “12+34”
Output: “1(2+3)4”
Explanation: The expression evaluates to 1 _ (2 + 3) _ 4 = 1 _ 5 _ 4 = 20.
Example 3:
Input: expression = “999+999”
Output: “(999+999)”
Explanation: The expression evaluates to 999 + 999 = 1998.
Constraints:
- 3 <= expression.length <= 10
- expression consists of digits from ‘1’ to ‘9’ and ‘+’.
- expression starts and ends with digits.
- expression contains exactly one ‘+’.
- The original value of expression, and the value of expression after adding any pair of parentheses that meets the requirements fits within a signed 32-bit integer.
use / and % Operation splits numbers , Then find the smallest combination , Be careful /1 and %1 Handling of special situations
impl Solution {
pub fn minimize_result(expression: String) -> String {
let tokens: Vec<&str> = expression.split("+").collect();
let left = tokens[0].parse::<i32>().unwrap();
let right = tokens[1].parse::<i32>().unwrap();
let mut ans = left + right;
let mut ans_str = expression.clone();
let mut left_exp = 0;
let mut right_exp = 0;
while left / 10i32.pow(left_exp) > 0 {
let mut a = left / 10i32.pow(left_exp);
let mut b = left % 10i32.pow(left_exp);
if a == left {
a = 1;
b = left;
}
while right / 10i32.pow(right_exp) > 0 {
let c = right / 10i32.pow(right_exp);
let mut d = right % 10i32.pow(right_exp);
if c == right {
d = 1;
}
let v = a * (b + c) * d;
if v < ans {
let mut s = String::new();
if left_exp == 0 {
s.push_str(&format!("({}", left));
} else {
s.push_str(&format!("{}({}", a, b));
}
s.push('+');
if right_exp == 0 {
s.push_str(&format!("{})", right));
} else {
s.push_str(&format!("{}){}", c, d))
}
ans_str = s;
ans = v;
}
right_exp += 1;
}
left_exp += 1;
right_exp = 0;
}
if ans_str == expression {
ans_str.insert(0, '(');
ans_str.push(')');
}
ans_str
}
}
边栏推荐
- IDEA 中使用 Hudi
- LeetCode每日一题(2232. Minimize Result by Adding Parentheses to Expression)
- 【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
- [CSDN] C1 training problem analysis_ Part III_ JS Foundation
- [set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
- Overview of database system
- LeetCode每日一题(1162. As Far from Land as Possible)
- Install database -linux-5.7
- [set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
- [CSDN]C1训练题解析_第三部分_JS基础
猜你喜欢
![[kotlin learning] classes, objects and interfaces - define class inheritance structure](/img/66/34396e51c59504ebbc6b6eb9831209.png)
[kotlin learning] classes, objects and interfaces - define class inheritance structure

Win10 install elk

解决Editor.md上传图片获取不到图片地址问题

PolyWorks script development learning notes (4) - data import and alignment using file import
![[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature](/img/66/2e7668cfed1ef4ddad26deed44a33a.png)
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature

Flink学习笔记(九)状态编程

Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)

Principles of computer composition - cache, connection mapping, learning experience

PolyWorks script development learning notes (II) -treeview basic operations

Temper cattle ranking problem
随机推荐
Esp32 at command does not respond
解决Editor.md上传图片获取不到图片地址问题
Hudi 数据管理和存储概述
制作jetson nano最基本的根文件系统、服务器挂载NFS文件系统
Common formulas of probability theory
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
Crawler career from scratch (IV): climb the bullet curtain of station B through API
Logstash+jdbc data synchronization +head display problems
【Kotlin学习】类、对象和接口——定义类继承结构
Using Hudi in idea
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
Installation and uninstallation of pyenv
基于opencv实现桌面图标识别
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
307. Range Sum Query - Mutable
WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
[untitled] use of cmake
The server denied password root remote connection access
Jenkins learning (III) -- setting scheduled tasks
npm install安装依赖包报错解决方法