LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number
1. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
思路:可以遍历线条所有两两组合的情况来解,但是复杂度回达到O(n2),超时。采用另一种方法,从数组两端的点a1和an开始考虑,先计算a1和an所构成的Area,也就是a1和an中最短的那根线乘以a1到an x轴上的距离。然后开始往中间考虑,途中记录最大的Area。具体做法是,以a1 和 an为例,同时往左往右考虑,抛弃其中较短的线,因为对于a1和an中的较短者来说,x轴上的距离现在是最大了,也就是说不管剩下的那些线条比这个较短者(a1或者an)长还是短(因为装多少水取决于短板),所构成的Area都不会比原来a1和an构成的Area要大,所以在这个求最大Area的过程中可以直接将这个较短者剔除,移动至a2或者a(n-1)。再重复此步骤,每一步得到的Area都要和最大Area比较以实时更新。
详细见:https://leetcode.com/problems/container-with-most-water/solution/
import java.util.*;
public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String input=sc.nextLine();
int[] nums=new int[input.length()];
for(int i=0;i<input.length();i++){
nums[i]=input.charAt(i)-'0';
}
}
public static int maxArea(int[] height) {
int maxarea = 0, l = 0, r = height.length - 1;
while (l < r) {
maxarea = Math.max(maxarea, Math.min(height[l], height[r]) * (r - l));
if (height[l] < height[r])
l++;
else
r--;
}
return maxarea;
}
}
2. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
解析:模仿3Sum那题的思路即可。
import java.util.*;
public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String input=sc.nextLine();
int n=sc.nextInt();
String[] str=input.split(",| ");
int[] nums=new int[str.length];
for(int i=0;i<str.length;i++){
// Integer.ParseInt() 方法将String转为int
nums[i]=Integer.parseInt(str[i]);
}
System.out.println(closet(nums,n));
}
public static int closet(int[] nums,int target) {
Arrays.sort(nums);
// Integer.MAX_VALUE
int closet=Integer.MAX_VALUE;
int closet_sum=0;
for(int i=0;i<nums.length-2;i++){
int l=i+1,r=nums.length-1;
while(l<r){
int sum=nums[i]+nums[l]+nums[r];
// 利用 Math.abs()方法求绝对值
if(Math.abs(sum-target)<closet){
closet=Math.abs(sum-target);
closet_sum=sum;
}
if(sum-target>0)
r--;
else
l++;
}
}
return closet_sum;
}
}
3. Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below.

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
思路:利用先进先出队列构造所有可能的组合,一开始进队列的应该是一个字母,进完后出队,每个与另一组中的字母组合成2个,进队,进完后再出队,与另一组中的字母组合构成3个,进队再出队,以此类推。
import java.util.*;
public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String digits=sc.nextLine();
List<String> letters=letterCombinations(digits);
for(String s:letters)
System.out.println(s);
}
public static List<String> letterCombinations(String digits) {
// LinkedList 实现了 Queue 接口和 List 接口,能当作队列来用
LinkedList<String> letters=new LinkedList<String>();
// 数组下标对应于输入的数字,值是数字对应的键盘上的字母
String[] mapping=new String[]{"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
// 这里先往队列添个东西,因为空队列 remove 方法会抛异常
letters.add("");
for(int i=0;i<digits.length();i++){
int x=digits.charAt(i)-'0';
// 这里是个较难理解的点,i(digits的char索引,从0开始)和队列中目前的字母组合的长度(也就是1个字母还是2个字母或者0个字母空队列的情况)对应
while(letters.peek().length()==i){
String s=letters.remove();
for(char c:mapping[x].toCharArray()){
letters.add(s+c);
}
}
}
return letters;
}
}
LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number的更多相关文章
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- leetcode面试准备:Container With Most Water
leetcode面试准备:Container With Most Water 1 题目 Given n non-negative integers a1, a2, ..., an, where eac ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
随机推荐
- bzoj3810: [Coci2015]Stanovi(记忆化搜索)
实际上切出来的矩阵在原矩阵上的位置是不重要的...重要的只有矩阵的大小和上下左右是否在边界上. 于是我们可以设f[x][y][l][r][u][d]表示x*y的矩阵上下左右是不是边界的最小代价. 记忆 ...
- bzoj1057: [ZJOI2007]棋盘制作(悬线法)
题目要求纵横坐标和奇偶性不同的点取值不同,于是我们把纵横坐标和奇偶性为1的点和0的点分别取反,就变成经典的最大全1子矩阵问题了,用悬线法解决. #include<iostream> #in ...
- 【BZOJ 1998】[Hnoi2010]Fsk物品调度 置换群+并查集
置换群的部分水得一比,据说是经典的置换群理论(然而我并不知道这理论是啥).重点就在于怎么求pos!!!容易发现这个东西是这样的:每次寻找pos,先在本环里找,找不到再往下一个环里找,直到找到为止……一 ...
- stout代码分析之八:cache类
stout中实现了LRU cache.cache的成员如下: public: typedef std::list<Key> list; typedef std::tr1::unordere ...
- bzoj 3252 攻略 长链剖分思想+贪心
攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 889 Solved: 423[Submit][Status][Discuss] Descrip ...
- Mybatis中传入List条件
传入一个map的参数,map里有一个tenantIds的List,在xml里先判断这个List的size是否大于o,然后通过foreach 构造一个in后面括号里的元素,具体的xml如下: <i ...
- 解决Sublime Install Package的There are no packages available for install问题(channel_v3.json)
Sublime版本 Sublime Text 3.1.1 Build 3176 自己也尝试了很多次,所以这一解决办法仅是可能解决你的问题 一.解决简要描述 造成的原因大致是 无法通过request去获 ...
- 2015/9/17 Python基础(13):函数
函数是对程序逻辑进行结构化或过程化的一种编程方法. Python的函数返回值当什么也不返回时,返回了None和大多数语言一样,Python返回一个值或对象.只是在返回容器对象时,看起来像返回多个对象. ...
- 自己实现KNN算法
import numpy as np from math import sqrt from collections import Counter class KNNClassifier(object) ...
- [BZOJ1101&BZOJ2301][POI2007]Zap [HAOI2011]Problem b|莫比乌斯反演
对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d. 我们可以令F[n]=使得n|(x,y)的数对(x,y)个数 这个很容易得到,只需要让x, ...