Implement pow(x, n). Solution: public class Solution { public double myPow(double x, int n) { if(n==0) return 1; double res = myPow(x, n/2); return n%2==0 ? res*res : n<0 ? res*res*(1/x) : res*res*x; } }
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? public class Solution { public void rotate(int[][] matrix) { int n = matrix.length; for(int i=0; i<n; i++){ for(int j=i+1; j<n; j++){ int tmp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = tmp; } } for(int i=0; i<n; i++){ int l=0, r=n-1; while(l<r){ int tmp =
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Example 1: Input: k = 3, n = 7 Output: [[1,2,4]] Example 2: Input: k = 3, n = 9 Output: [[1,2,6], [1,3,5], [2,3,4]] Solution: public class Solution { public List<List<Integer>> combinationSum3(int k, int n)
Given a collection of candidate numbers ($C$) and a target number ($T$), find all unique combinations in $C$ where the candidate numbers sums to $T$. Each number in $C$ may only be used once in the combination. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8, A solution set is: [ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6] ] Note: combinationSum2(ans, candidates, target-candidates[i], i+1, list); use i+1 to control duplicate selection.
Given a set of candidate numbers ($C$) and a target number ($T$), find all unique combinations in $C$ where the candidate numbers sums to $T$. The same repeated number may be chosen from $C$ unlimited number of times. Note: All numbers (including target) will be positive integers. The solution set must not contain duplicate combinations. For example, given candidate set [2, 3, 6, 7] and target 7, A solution set
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memory. Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column. 1,2,3 → 1,3,2 3,2,1 → 1,2,3 1,1,5 → 1,5,1 Idea: Reverse find first number which breaks descending order.
Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [ [1,1,2], [1,2,1], [2,1,1] ] Solution: based on Permutations, use Set to avoid duplication. public class Solution { public List<List<Integer>> permuteUnique(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); ans.add(new ArrayList<Integer>()); for(int i=0; i<nums.length; i++){ List<List<Integer>> newAns = new ArrayList<>(); Set<String> set = new HashSet<>(); for(List<Integer> list : ans){
Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] Solution: public class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> ans = new ArrayList<>(); ans.add(new ArrayList<Integer>()); for(int i=0; i<nums.length; i++){ List<List<Integer>> newAns = new ArrayList<>(); for(List<Integer> list : ans){ /* for every postion */ for(int pos=0; pos<=i; pos++){ /* copy list */ List<Integer> newList
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. Solution: public class Solution { public void solveSudoku(char[][] board) { solve(board); } public boolean solve(char[][] board) { for(int i=0; i<9; i++){ for(int j=0; j<9; j++){ if(board[i][j]=='.'){ for(char c='1'; c<='9'; c++){ if(isValid(board,i,j,c)){ board[i][j] = c; if(solve(board)) return true; board[i][j]
Determine if a Sudoku is valid, according to Sudoku Puzzles Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. Solution: public class Solution { public boolean isValidSudoku(char[][] board) { for(int i=0; i<board.length; i++){ int[] t = new int[9]; for(int j=0; j<board.length; j++){ char ch = board[i][j]; if(ch=='.') continue; if(t[ch-'1']==1){ return false; } else { t[ch-'1']=1; } } } for(int j=0; j<board.length; j++){