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. Excel+Python:分组求和

    Excel选中区域,排除序号.姓名等列,复制Ctrl+C. Python,import pandas as pd,读取剪切板并赋值给变量df,df.groupby('部门').sum().若要避免部门 ...

  2. python 基础应用1

    1.使用while循环输入1 2 3 4 5 6 8 9 10 n = 0 while n < 11: n = n + 1 if n == 7: continue print(n) n = 0 ...

  3. video 全屏,播放,隐藏控件。

    requestFullscreen全屏具体实现 1.进入全屏 function full(ele) { if (ele.requestFullscreen) { ele.requestFullscre ...

  4. 手把手numpy教程【二】——数组与切片

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Numpy专题的第二篇,我们来进入正题,来看看Numpy的运算. 上一篇文章当中曾经提到过,同样大小的数据,使用Numpy的运算速度会 ...

  5. Ubuntu下解决中文显示为方块最佳方法

    一.问题分析 由于Linux系统中并没有包含中文相关的字体库,而不是系统不支持中文或者中文乱码,因此显示给我们的是方块儿 二.解决方法 1.从window系统中的字体库复制需要的中文库到Linux系统 ...

  6. MVC4.0接口学习

    /// <summary> /// 正则验证身份证号是否合法 /// </summary> /// <param name="sIdCard"> ...

  7. dede列表页限制标题长度

    {dede:list pagesize ='10' titlelen="45"} <li><a href="[field:arcurl/]"& ...

  8. SimpleAuthenticationInfo的参数

    SimpleAuthenticationInfo的参数 仅供个人参考,以及学习记录.SimpleAuthenticationInfo authenticationInfo = new SimpleAu ...

  9. C#不定长参数

    Test(5,6,7,8,9,10); void Test(int sd, params int[] arr)//不定长参数前要加params { for(int i = 0 ; i < arr ...

  10. [翔哥高手无敌之路]0-002.如何提取apk中的信息?

    面对一款apk软件,我们如何去获取它的信息,如何获取它的版本号,包名,或者ID,用户权限,这些信息都隐藏在apk包中的AndroidManifest.xml文件中,解开它我们就能获取任何想要的信息.但 ...