Skip to content

Instantly share code, notes, and snippets.

View bnyu's full-sized avatar
💕
hello world

xuyue bnyu

💕
hello world
View GitHub Profile
@bnyu
bnyu / Solution.java
Created September 30, 2017 03:00
20. Valid Parentheses
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<>();
@bnyu
bnyu / Solution.java
Created September 30, 2017 02:28
19. Remove Nth Node From End of List
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<>();
@bnyu
bnyu / Solution.java
Last active September 29, 2017 14:11
17. Letter Combinations of a Phone Number
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 {
@bnyu
bnyu / Solution.java
Last active November 5, 2017 09:47
15. 3Sum
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<>();
@bnyu
bnyu / Solution.java
Created September 27, 2017 06:51
14. Longest Common Prefix
/**
* 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;
@bnyu
bnyu / Solution.java
Created September 27, 2017 06:18
11. Container With Most Water
/**
* 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;
@bnyu
bnyu / Solution.java
Last active September 27, 2017 03:09
10. Regular Expression Matching
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 {
@bnyu
bnyu / Solution.java
Last active September 27, 2017 03:08
9. Palindrome Number
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) {
@bnyu
bnyu / Solution.java
Last active September 25, 2017 17:31
7. Reverse Integer
/**
* 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;
}
@bnyu
bnyu / Solution.java
Created September 25, 2017 16:43
6. ZigZag Conversion
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;