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. Owin Katana 的底层源码分析

    最近看了一下开源项目asp.net katana,感觉公开的接口非常的简洁优雅,channel 9 说是受到node.js的启发设计的,Katana是一个比较老的项目,现在已经整合到asp.net c ...

  2. Qt 操作sql server数据库

    添加qtsql的库 连接数据库 QSqlDatabase_db = QSqlDatabase::addDatabase("QODBC"); _db.setHostName(); _ ...

  3. SEO策略之关键词选择的原则

    策略就是指为了实现某一个目标而预先制定的能够实施的方案.在制定SEO策略的时候,我们需要了解网站所有的基本情况,同时又要对网站所处的行业的竞争对手有一个准确的数据分析.SEO策略有几个比较突出的属性: ...

  4. window 10电脑永不熄屏的方法

    你的电脑是不是人还没有离开一会儿,经常锁屏,输入密码??反复反复,特别的折磨人,别急,下面我教你,告别反复,从此我的电脑我做主. 第一步,打开设置,进入个性化界面,点击锁屏界面,往下滑 第二步,找到屏 ...

  5. css3及css技巧

    左右对齐:  

  6. Java内存区域与内存溢出异常——深入理解Java虚拟机 笔记一

    Java内存区域 对比与C和C++,Java程序员不需要时时刻刻在意对象的创建和删除过程造成的内存溢出.内存泄露等问题,Java虚拟机很好地帮助我们解决了内存管理的问题,但深入理解Java内存区域,有 ...

  7. npm加载包报错 :syscall access

    $ npm install --save-dev babel-plugin-syntax-dynamic-import npm WARN checkPermissions Missing write ...

  8. NPOI导入excel为datatable (xls xlsx xlsm)

    使用NPOI导入导出Excel(xls/xlsx)数据到DataTable中 http://www.cnblogs.com/songrun/p/3547738.html NPOI 2.0教程 – 自动 ...

  9. 王艳 201771010127《面向对象程序设计(java)》第三周学习总结

    一:理论知识总结: 第一章:主要概述了java相比其他程序设计语言(如C语言.c++)之间的不同性能.为我们揭示了java这种语言的设计初衷一节截至目前java语言达到的效果.另外,还简要介绍了jav ...

  10. 决策树purity/基尼系数/信息增益 Decision Trees

    目录 决策树简单描述 衡量purity的三种方法 Gini Coefficient Entropy熵 决策树简单描述 决策树的样子大概是这个样子的: 选择一个特征作为根节点,把这个特征划分成两个孩子节 ...