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. 【Spark】Spark-shell案例——standAlone模式下读取HDFS上存放的文件

    目录 可以先用local模式读取一下 步骤 一.先将做测试的数据上传到HDFS 二.开发scala代码 standAlone模式查看HDFS上的文件 步骤 一.退出local模式,重新进入Spark- ...

  2. PHP导出excel文件,第二步先实现自写二维数组加入模板excel文件后导出

    今天主要研究数据加入EXCEL并导出的问题,先不从数据库提取数据导出,自己先写一个二维数组,然后遍历二维数组写入excel模板中导出,首先根据模板excel的内容书写对应的二维数组 $arr=arra ...

  3. 用Stream流轻易的收集数据

    前言 在日常使用集合时,我们通常使用迭代器来处理集合中的数据,假如有一个用户列表 List,我们想要将用户按照性别分组生成 Map<String, List>.需要遍历 List,然后判断 ...

  4. EI检索

    第一步: 网址:http://www.elsevier.com/online-tools/engineering-village 第二步: 网址:http://www.engineeringvilla ...

  5. Android将库导入到build.gradle

    如图

  6. 数据库连接池Druid的介绍,配置分析对比总结

    Druid的简介 Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBos ...

  7. PHPExcel生成Excel文件---提示导出文件或者文件扩展名不一致,或导出的文件或文件扩展名无效

    $data = Db::name('shop_cart')->where('phone','15555555555')->select(); $objPHPExcel = new PHPE ...

  8. Gradle 多环境URL请求设置

    在开发过程中,多环境配置是经常遇到的,比如在Android开发过程中,在不同环境上请求服务器的URL是不同的,使用Gradle进行管理,是非常方便的. 首先查看工程目录结构: 使用AndroidStu ...

  9. 黑马程序员_毕向东_Java基础视频教程——进制(随笔)

    进制的特点 进制的由来 任何数据在计算机中都是以二进制的形式存在.二进制最早由电信号演变而来. 一个整数在内存中一样也是二进制,但是使用一大串的0 1组成的二进制数进行使用很麻烦所以就想把一大串缩短点 ...

  10. 很好的awk教程

    讲的内容配合分析web log ,疗效很好! http://www.ibm.com/developerworks/cn/linux/l-cn-awk-httplog/ 出差归来,哈哈,吐槽的不必写了, ...