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
/** | |
* 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; |
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
/** | |
* 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) |
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
/** | |
* 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; |
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
/** | |
* 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; |
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
/** | |
* 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) |
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; | |
/** | |
* 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. |
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
/** | |
* 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) |
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; | |
/** | |
* 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; |
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; | |
/** | |
* 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<>(); |
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
/** | |
* 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; |