This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Definition for a binary tree node. | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; | |
| * TreeNode right; | |
| * TreeNode(int x) { val = x; } | |
| * } | |
| */ | |
| public class Solution { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public List<List<Integer>> combine(int n, int k) { | |
| List<List<Integer>> solution = new ArrayList<>(); | |
| dfs(n, k, 1, solution, new ArrayList<Integer>()); | |
| return solution; | |
| } | |
| private void dfs(int n, int k, int start, List<List<Integer>> l, List<Integer> path) { | |
| if(k == 0) { | |
| l.add(path); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Solution { | |
| public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { | |
| int areaA = (D - B) * (C - A); | |
| int areaB = (H - F) * (G - E); | |
| Point a = new Point(A,B); | |
| Point b = new Point(C,D); | |
| Point c = new Point(E,F); | |
| Point d = new Point(G,H); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Definition for a binary tree node. | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; | |
| * TreeNode right; | |
| * TreeNode(int x) { val = x; } | |
| * } | |
| */ | |
| public class Solution { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: | |
| Integers in each row are sorted from left to right. | |
| The first integer of each row is greater than the last integer of the previous row. | |
| */ | |
| public class Solution { | |
| public boolean searchMatrix(int[][] matrix, int target) { | |
| int m = matrix.length - 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. | |
| */ | |
| public class Solution { | |
| public boolean containsNearbyDuplicate(int[] nums, int k) { | |
| Map<Integer,Integer> map = new HashMap<Integer,Integer>(); | |
| for (int i = 0; i < nums.length; i++) { | |
| if (map.containsKey(nums[i])) { | |
| int value = map.get(nums[i]); | |
| if ((i - value) <= k) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. | |
| Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) | |
| Output: 7 -> 0 -> 8 | |
| * Definition for singly-linked list. | |
| * public class ListNode { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Given a binary tree, determine if it is a valid binary search tree (BST). | |
| Assume a BST is defined as follows: | |
| The left subtree of a node contains only nodes with keys less than the node's key. | |
| The right subtree of a node contains only nodes with keys greater than the node's key. | |
| Both the left and right subtrees must also be binary search trees. | |
| * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Given two binary trees, write a function to check if they are equal or not. | |
| Two binary trees are considered equal if they are structurally identical and the nodes have the same value. | |
| * Definition for binary tree | |
| * public class TreeNode { | |
| * int val; | |
| * TreeNode left; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Given two sorted integer arrays A and B, merge B into A as one sorted array. | |
| Note: | |
| You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. | |
| */ | |
| public class Solution { | |
| public void merge(int A[], int m, int B[], int n) { | |