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 October 23, 2017 02:34
31. Next Permutation
/**
* 31. Next Permutation
* 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.
*/
class Solution {
public void nextPermutation(int[] nums) {
int sortedIndex = 0;
int lastIndex = nums.length - 1;
@bnyu
bnyu / Solution.java
Last active October 19, 2017 07:36
29. Divide Two Integers
/**
* 29. Divide Two Integers
* Divide two integers without using multiplication, division and mod operator.
* If it is overflow, return MAX_INT.
*/
class Solution {
public int divide(int dividend, int divisor) {
if (dividend == 0)
return 0;
else if (divisor == 1)
@bnyu
bnyu / Solution.java
Created October 19, 2017 03:17
28. Implement strStr()
/**
* 28. Implement strStr()
* Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
*/
class Solution {
public int strStr(String haystack, String needle) {
if (haystack.length() < needle.length())
return -1;
else if (needle.length() == 0)
return 0;
@bnyu
bnyu / Solution.java
Created October 19, 2017 02:51
27. Remove Element
/**
* 27. Remove Element
* Given an array and a value, remove all instances of that value in place and return the new length.
* Do not allocate extra space for another array, you must do this in place with constant memory.
* The order of elements can be changed. It doesn't matter what you leave beyond the new length.
*/
class Solution {
public int removeElement(int[] nums, int val) {
if (nums.length == 0)
return 0;
@bnyu
bnyu / Solution.java
Created October 18, 2017 13:31
26. Remove Duplicates from Sorted Array
/**
* 26. Remove Duplicates from Sorted Array
* Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
* Do not allocate extra space for another array, you must do this in place with constant memory.
* For example, Given input array nums = [1,1,2],
* Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
*/
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0)
@bnyu
bnyu / Solution.java
Last active October 18, 2017 08:07
25. Reverse Nodes in k-Group
import java.util.ArrayList;
import java.util.List;
/**
* 25. Reverse Nodes in k-Group
* Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
* k is a positive integer and is less than or equal to the length of the linked list.
* If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
* You may not alter the values in the nodes, only nodes itself may be changed.
* Only constant memory is allowed.
@bnyu
bnyu / Solution.java
Created October 17, 2017 10:09
24. Swap Nodes in Pairs
/**
* 24. Swap Nodes in Pairs
* Given a linked list, swap every two adjacent nodes and return its head.
* For example, Given 1->2->3->4, you should return the list as 2->1->4->3.
* Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode ans;
if (head == null)
@bnyu
bnyu / Solution.java
Last active October 17, 2017 04:57
23. Merge k Sorted Lists
import java.util.ArrayList;
import java.util.List;
/**
* 23. Merge k Sorted Lists
* Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
@bnyu
bnyu / Solution.java
Created October 16, 2017 08:44
22. Generate Parentheses
import java.util.ArrayList;
import java.util.List;
/**
* 22. Generate Parentheses
* Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
*/
class Solution {
private final List<String> ans = new ArrayList<>();
@bnyu
bnyu / Solution.java
Created September 30, 2017 03:47
21. Merge Two Sorted Lists
/**
* 21. Merge Two Sorted Lists
* Merge two sorted linked lists and return it as a new list.
* The new list should be made by splicing together the nodes of the first two lists.
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode previous = new ListNode(0);
final ListNode ans = previous;
ListNode after;