Java实现 LeetCode 553 最优除法(思路问题)
553. 最优除法
给定一组正整数,相邻的整数之间将会进行浮点除法操作。例如, [2,3,4] -> 2 / 3 / 4 。
但是,你可以在任意位置添加任意数目的括号,来改变算数的优先级。你需要找出怎么添加括号,才能得到最大的结果,并且返回相应的字符串格式的表达式。你的表达式不应该含有冗余的括号。
示例:
输入: [1000,100,10,2]
输出: “1000/(100/10/2)”
解释:
1000/(100/10/2) = 1000/((100/10)/2) = 200
但是,以下加粗的括号 “1000/((100/10)/2)” 是冗余的,
因为他们并不影响操作的优先级,所以你需要返回 “1000/(100/10/2)”。
其他用例:
1000/(100/10)/2 = 50
1000/(100/(10/2)) = 50
1000/100/10/2 = 0.5
1000/100/(10/2) = 2
说明:
输入数组的长度在 [1, 10] 之间。
数组中每个元素的大小都在 [2, 1000] 之间。
每个测试用例只有一个最优除法解。
PS:
捋一下思路,大于两个数,就是让除数变小就好,所以就是第一个数/(剩下的数相除)
class Solution {
public String optimalDivision(int[] nums) {
StringBuilder sb = new StringBuilder();
int len = nums.length;
//元素个数小于 2,直接 a / b 或 a 即可
if(len < 3){
for(int i = 0; i < nums.length; i++){
sb.append(nums[i]);
if(i != nums.length - 1){
sb.append("/");
}
}
}else{
for(int i = 0; i < nums.length; i++){
sb.append(nums[i]);
if(i == 0){
sb.append("/(");
}else if(i != nums.length - 1){
sb.append("/");
}
}
sb.append(")");
}
return sb.toString();
}
}
Java实现 LeetCode 553 最优除法(思路问题)的更多相关文章
- Leetcode 553.最优除法
最优除法 给定一组正整数,相邻的整数之间将会进行浮点除法操作.例如, [2,3,4] -> 2 / 3 / 4 . 但是,你可以在任意位置添加任意数目的括号,来改变算数的优先级.你需要找出怎么添 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- Linux 通过终端命令行切换系统语言
通过命令的形式修改系统的语言,比较详细的讲解了来龙去脉: 文章目录 0 前言 1 locale 文件 2 查找相关文件 3 解决方案 4 相关信息 4.1 locale属性的含义 4.2 LANGUA ...
- Centos ps命令
输出格式(ps -aux) USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND VSZ:占用的虚拟内存大小() RSS: COMMAND: 执 ...
- python --字符串学习
一 转义字符 借用一个特殊的方法表示一系列不方便写出的内容,比如回车键,换行符,退格键 借助反斜杠字符,一旦出现反斜杠,则反斜杠后面一个或者几个字符表示已经不是原来的意思了 在字符串中,一旦出现了斜杠 ...
- ql的python学习之路-day13
前言:本节主要学习模块 一.模块的定义 模块:本质是.py结尾的python文件(文件名:test.py,对应的模块是:test),用来从逻辑上组织python代码(变量.函数.类.逻辑,本质是实现一 ...
- xml rpc SimpleXMLRPCServer [python]
SimpleXMLRPCServe 其实里面xml的概念不是很强,主要是rpc !不用关心什么xml . rpc 是就是远程调用,把函数什么的放到远程服务器上,本地调用就行了.用 SimpleXMLR ...
- 「雕爷学编程」Arduino动手做(28)——RGB全彩LED模块
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...
- PART(Persistent Adaptive Radix Tree)的Java实现源码剖析
论文地址 Adaptive Radix Tree: https://db.in.tum.de/~leis/papers/ART.pdf Persistent Adaptive Radix Tree: ...
- uefi win10 Ubuntu 18的安装
uefi win10 Ubuntu 18的安装 (Ubuntu折腾的第一天) 安装时的踩坑记录
- 基于 abp vNext 和 .NET Core 开发博客项目 - 集成Hangfire实现定时任务处理
上一篇文章(https://www.cnblogs.com/meowv/p/12956696.html)成功使用了Redis缓存数据,大大提高博客的响应性能. 接下来,将完成一个任务调度中心,关于定时 ...
- 【半译】扩展shutdown超时设置以保证IHostedService正常关闭
我最近发现一个问题,当应用程序关闭时,我们的应用程序没有正确执行在IHostedService中的StopAsync方法.经过反复验证发现,这是由于某些服务对关闭信号做出响应所需的时间太长导致的.在这 ...