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 最优除法(思路问题)的更多相关文章

  1. Leetcode 553.最优除法

    最优除法 给定一组正整数,相邻的整数之间将会进行浮点除法操作.例如, [2,3,4] -> 2 / 3 / 4 . 但是,你可以在任意位置添加任意数目的括号,来改变算数的优先级.你需要找出怎么添 ...

  2. 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 ...

  3. 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. ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 【Kafka】Producer API

    Producer API Kafka官网文档给了基本格式 地址:http://kafka.apachecn.org/10/javadoc/index.html?org/apache/kafka/cli ...

  2. 【Hadoop离线基础总结】MapReduce增强(下)

    MapReduce增强(下) MapTask运行机制详解以及MapTask的并行度 MapTask运行流程 第一步:读取数据组件InputFormat(默认TextInputFormat)会通过get ...

  3. vue-cli3.0读取外部化配置文件来修改公共路径

    之前我写过一篇通过nginx配置代理转发的博客,正常来说也是正确的,但不足之处在了甲方还用了F5负载均衡和gateway来代理转发.所以之前我认为的请求->nginx转发代理->后端服务, ...

  4. Web(4)servlet

    一.servlet.GenericServlet.HttpServlet 1.servlet具有四个生命周期方法 特性:单例模式,线程不安全,效率高 2.servletConfig接口对应根元素对应的 ...

  5. flink进阶篇

    Flink 面试--进阶篇 1.Flink是如何支持批流一体的? 2.Flink是如何做到高效的数据交换的? 3.Flink是如何做容错的? 4.Flink 分布式快照的原理是什么? 5.Flink ...

  6. 码农高效率工作必备工具之 StrokesPlus

    鲁迅先生曾经说过:不想偷懒的码农不是好码农. 我今天偏偏就勤奋一下,把压箱底的偷懒神器分享给大家. StrokesPlus(简称S+)是一款非常好用的鼠标手势软件,通过按下鼠标键画手势,或者按快捷键, ...

  7. linux --开机自动挂载硬盘【转】

    转:http://c.biancheng.net/view/900.html 了解了 mount 命令之后,读者可能会问,系统如何在开机时自动挂载硬盘,它又是怎么知道哪些分区是需要挂载的呢? 很简单, ...

  8. JS中各种变量类型在条件判断为false的情况

    var a = undefined; ->false var a = 0; ->false var a = 0.0; ->false var a = NaN; ->false ...

  9. Office 2016 英文版(VOL版)下载

    Office 2016 英文版(大客户版)下载磁力链接: 1.专业版(含project.visio) ProPlus, Project Pro, Visio Pro (x86-x64) magnet: ...

  10. php的 mysqlnd驱动

    这篇文章写的好 http://blog.linuxeye.com/395.html 传统编译php的时候需要指定mysql 的参数 --with-mysql=/usr/local/mysql \ -- ...