Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution set must not contain duplicate quadruplets. For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0. A solution set is:
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"]. Solution: public class Solution { public List<String> letterCombinations(String digits) { List<String> ans = new ArrayList<>(); if(digits.length()==0) return ans; ans.add(""); for(int i=0; i<digits.length(); i++){ List<String> newAns = new ArrayList<>();
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).
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] Pay attention: Don’t forget to move on "l++" and "r--" after adding a feasible triplet.
Write a function to find the longest common prefix string amongst an array of strings. Solution: public class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length==0 || strs[0].length()==0) return ""; int len = strs[0].length(); for(int i=1; i<strs.length; i++){ int j=0; while(j<len && j<strs[i].length() && strs[i-1].charAt(j)==strs[i].charAt(j)) j++; len = Math.min(len,j); if(len==0) break; } return strs[0].substring(0,len); } }
Given $n$ non-negative integers $a_1, a_2, …, a_n$, where each represents a point at coordinate $(i, a_i)$. $n$ vertical lines are drawn such that the two endpoints of line $i$ is at $(i, a_i)$ 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. Solution: public class Solution { public int maxArea(int[]
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 public class Solution { public int reverse(int x) { long ans = 0; while(x!=0){ ans = 10 * ans + (x%10); x /= 10; } return (ans<Integer.MIN_VALUE || ans>Integer.MAX_VALUE) ? 0 : (int)ans; } }
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, If nums = [1,2,2], a solution is: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] Solution: public class Solution { public List<List<Integer>> subsetsWithDup(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); ans.add(new ArrayList<Integer>()); Arrays.sort(nums); int size = ans.size(); for(int i=0; i<nums.length; i++){ List<List<Integer>> newAns = new
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: “PAHNAPLSIIGYIR” Write the code that will take a string and make this conversion given a number of rows: string convert(string text, int nRows); convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example, If nums = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] Solution 1: recursive solution, beats 13% of java submissions public class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); /* i<=nums.length */ for(int i=0; i<=nums.length; i++){ ans.addAll(subsets(nums,i,nums.length));