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. python语法学习第六天--字典

    字典:可变容器类型,用键值对的形式采用花括号储存(键唯一) 语法:d={key1:value1,key2:value2} 访问字典中的值: 字典名[键名]#若字典中不存在则报错 更改字典: 添加值:字 ...

  2. 设计模式之GOF23迭代器模式

    迭代器模式Iterator /** * 自定义迭代器接口 * @author 小帆敲代码 * */public interface MyIterator {  void first();//游标置于第 ...

  3. 比AtomicLong更优秀的LongAdder确定不来了解一下吗?

    前言 思维导图.png 文章中所有高清无码图片在公众号号回复: 图片666 即可查阅, 可直接关注公众号:壹枝花算不算浪漫 最近阿里巴巴发布了Java开发手册(泰山版) (公众号回复: 开发手册 可收 ...

  4. ThinkPad BIOS

    ThinkPad-BIOS    如有问题指出 ~谢谢

  5. 学习docker的一点记录

    0x00 docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化 ...

  6. linux wc 的用法-linux 下统计行数、单词数、字符个数

    linux wc 的用法-linux 下统计行数.单词数.字符个数   wc : wc -l 统计有多少行 wc -w 统计有多少个单词 wc -c 统计有多少个字符

  7. Netty源码死磕一(netty线程模型及EventLoop机制)

    引言 好久没有写博客了,近期准备把Netty源码啃一遍.在这之前本想直接看源码,但是看到后面发现其实效率不高, 有些概念还是有必要回头再细啃的,特别是其线程模型以及EventLoop的概念. 当然在开 ...

  8. MySQL数据库基础操作语句

    SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,分为3种类型: 1.DDL语句 数据库定义语言: 数据库.表.视图.索引.存储过程,例如CREATE DROP ALTER 2.DM ...

  9. day01:判断与循环(20170213)

    #1测试判断用户与密码是否正确:import getpassusername = "llz"password = "123455"_username = inp ...

  10. H3C S5500V2交换机误格式化恢复

    一.格式化后,bin文件及视图全部被删除需要联系H3C客服报交换机后面的序列号,然后根据工单中给你的账号密码去H3C官网下载对应的软件包. 二.下载3CDaemon使用TFTP方式将解压出来的.ipe ...