标签:ali tps abs hash ace string tor array http
博客园markdown太烂, 题解详情https://github.com/TangliziGit/leetcode/blob/master/solution/11-20.md
marks:
@: hard to get a direct solution
%: need optimization
%%% 11. Container With Most Water[Medium]
%%%%% 15. 3Sum[Medium]
%%% 16. 3Sum Closest [Medium]
% 18. 4Sum [Medium]
Arrays.sort(arr);
Arrays.stream(arr).boxed().collect(Collectors.toList());
11. Container With Most Water[Medium]
无
class Solution {
public int maxArea(int[] height) {
int l=0, r=height.length-1, ans=0;
while (l<r){
int area=Math.min(height[l], height[r])*(r-l);
ans=Math.max(ans, area);
if (height[l]<height[r]) l++;
else r--;
}return ans;
}
}
13. Roman to Integer [Easy]
水题, 注意题意
无
class Solution {
private static Map<Character, Integer> map=new HashMap();
static{
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
};
public int romanToInt(String s) {
int ans=0, len=s.length();
for (int i=0; i<len; i++)
if (i+1<len && map.get(s.charAt(i))<map.get(s.charAt(i+1)))
ans-=map.get(s.charAt(i));
else
ans+=map.get(s.charAt(i));
return ans;
}
}
14. Longest Common Prefix [Easy]
水题
刚好用来写Stream
// Stream version
// 47ms, 38MB
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs==null || strs.length==0) return "";
int minlen=Stream.of(strs)
.mapToInt(x -> x.length())
.reduce(Integer::min)
.getAsInt();
int idx=Stream.iterate(0, x -> x+1).limit(0+minlen)
.filter(x -> check(strs, x))
.findFirst()
.orElse(minlen);
return strs[0].substring(0, idx);
}
public boolean check(String[] strs, int idx){
return Stream.of(strs)
.anyMatch(x -> x.charAt(idx)!=strs[0].charAt(idx));
}
}
// Original
// 4ms, 39MB
class OriginalSolution {
public String longestCommonPrefix(String[] strs) {
if (strs==null || strs.length==0) return "";
int idx=0, minlen=strs[0].length();
for (String str: strs)
minlen=Math.min(minlen, str.length());
for (;idx<minlen; idx++)
if (check(strs, idx)) break;
return strs[0].substring(0, idx);
}
public boolean check(String[] strs, int idx){
for (String str: strs)
if (str.charAt(idx)!=strs[0].charAt(idx))
return true;
return false;
}
}
16. 3Sum Closest [Medium]
class Solution {
public int threeSumClosest(int[] nums, int target) {
int ans=nums[0]+nums[1]+nums[2];
Arrays.sort(nums);
for (int i=0; i<nums.length; i++){
int l=i+1, r=nums.length-1;
while (l<r){
int sum=nums[i]+nums[l]+nums[r];
if (Math.abs(sum-target)<Math.abs(ans-target))
ans=sum;
if (sum<target) l++;
else r--;
}
}return ans;
}
}
17. Letter Combinations of a Phone Number [Medium]
水题, 递归
class Solution {
private String template="abcdefghijklmnopqrstuvwxyz";
public List<String> letterCombinations(String digits) {
if (digits.equals("")) return new ArrayList<String>();
return solve(digits, 0);
}
private List<String> solve(String digits, int ptr){
if (ptr==digits.length()) return Arrays.asList("");
List<String> tmp=solve(digits, ptr+1), ans=new LinkedList();
int num=digits.charAt(ptr)-'2', n=(num+2==9||num+2==7)?4:3;
int offset=(num+2==9||num+2==8)?1:0;
for (String str: tmp){
for (int i=offset; i<n+offset; i++)
ans.add(template.charAt(i+num*3)+str);
}return ans;
}
}
19. Remove Nth Node From End of List [Medium]
水题
优化:
可以在第一个指针走了n个元素后, 在起一个指针, 等第一个结束了之后, 删除后一个指针的元素.
然而对复杂度没有提升, 而且有人说这是个很好的优化, 我说简直扯淡好吧
无
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
int len=0;
ListNode tmp=head;
while (tmp!=null){
len++;
tmp=tmp.next;
}
if (n==len) return head.next;
tmp=head;
for (int i=0; i<len-n-1; i++)
tmp=tmp.next;
tmp.next=tmp.next.next;
return head;
}
}
20. Valid Parentheses [Easy]
水题, 栈
class Solution {
private static Map<Character, Character> map=new HashMap();
static{
map.put('(', ')');
map.put('{', '}');
map.put('[', ']');
}
public boolean isValid(String s) {
Stack<Character> sta=new Stack();
int len=s.length();
for (int i=0; i<len; i++){
if (map.containsKey(s.charAt(i))) sta.push(s.charAt(i));
else{
if (!sta.isEmpty() && s.charAt(i)==map.get(sta.peek())) sta.pop();
else return false;
}
}
if (sta.isEmpty())
return true;
return false;
}
}
标签:ali tps abs hash ace string tor array http
原文地址:https://www.cnblogs.com/tanglizi/p/11502672.html