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
| import java.util.*; | |
| /** | |
| * 20. Valid Parentheses | |
| * Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. | |
| * The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. | |
| */ | |
| class Solution { | |
| public boolean isValid(String s) { | |
| Map<Character, Character> brackets = new HashMap<>(); |
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
| import java.util.HashMap; | |
| import java.util.Map; | |
| /** | |
| * 19. Remove Nth Node From End of List | |
| * Given a linked list, remove the nth node from the end of list and return its head. Given n will always be valid. | |
| */ | |
| class Solution { | |
| public ListNode removeNthFromEnd(ListNode head, int n) { | |
| Map<Integer, ListNode> map = new HashMap<>(); |
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
| import java.util.ArrayList; | |
| import java.util.List; | |
| /** | |
| * 17. 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. | |
| */ | |
| 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
| import java.util.*; | |
| /** | |
| * 15. 3Sum | |
| * 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. | |
| */ | |
| class Solution { | |
| public List<List<Integer>> threeSum(int[] nums) { | |
| Map<Integer, Integer> numNum = new HashMap<>(); |
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
| /** | |
| * 14. Longest Common Prefix | |
| * Write a function to find the longest common prefix string amongst an array of strings. | |
| */ | |
| class Solution { | |
| public String longestCommonPrefix(String[] strs) { | |
| String ans = ""; | |
| if (strs.length >= 1) { | |
| String refer = strs[0]; | |
| int common = 0; |
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
| /** | |
| * 11. Container With Most Water | |
| * You may not slant the container and n is at least 2. | |
| */ | |
| class Solution { | |
| public int maxArea(int[] height) { | |
| int area = 0; | |
| int length = height.length; | |
| int lHighest = 0; | |
| boolean leftIsHigher = false; |
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
| import java.util.ArrayList; | |
| import java.util.HashMap; | |
| import java.util.List; | |
| import java.util.Map; | |
| /** | |
| * 10. Regular Expression Matching | |
| * Implement regular expression matching with support for '.' and '*'. | |
| */ | |
| 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
| import java.util.HashMap; | |
| import java.util.Map; | |
| /** | |
| * 9. Palindrome Number | |
| * Determine whether an integer is a palindrome. Do this without extra space. | |
| */ | |
| class Solution { | |
| public boolean isPalindrome(int x) { | |
| if (x < 0) { |
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
| /** | |
| * 7. Reverse Integer | |
| * Reverse digits of an integer. | |
| */ | |
| class Solution { | |
| public int reverse(int x) { | |
| // 0x80000000 = ~0x80000000 + 1 | |
| if (x == Integer.MIN_VALUE) { | |
| return 0; | |
| } |
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
| class Solution { | |
| public String convert(String s, int numRows) { | |
| String ans; | |
| if (!s.isEmpty() && numRows > 0) { | |
| if (numRows == 1) { | |
| return s; | |
| } | |
| int length = s.length(); | |
| char[] chars = new char[length]; | |
| int lIndex; |