Example: confidence

LeetCode Solutions - ProgramCreek.com

LeetCode SolutionsProgram Array in Java72 Evaluate Reverse polish Notation93 Solution of Longest Palindromic Substring in Java114 Solution Word Break155 Word Break II186 Word Ladder207 Median of Two Sorted Arrays Java238 Regular Expression Matching in Java259 Merge Intervals2710 Insert Interval2911 Two Sum3112 Two Sum II Input array is sorted3213 Two Sum III Data structure design3314 3 Sum3415 4 Sum3616 3 Sum Closest3817 String to Integer (atoi)3918 Merge Sorted Array4019 Valid Parentheses4220 Implement strStr()432|181 Contents21 Set Matrix Zeroes4422 Search Insert Position4623 Longest Consecutive Sequence Java4724 Valid Palindrome4925 Spiral Matrix5226 Search a 2D Matrix5527 Rotate Image5628 Triangle5829 Distinct Subsequences Total6030 Maximum Subarray6231 Maximum Product Subarray6332 Remove Duplicat

Feb 27, 2015 · Contents 1Rotate Array in Java 7 2Evaluate Reverse Polish Notation 9 3Solution of Longest Palindromic Substring in Java 11 4Solution Word Break 15 5Word Break II 18 ...

Tags:

  Reserve, Notation, Polish, Reverse polish notation, Leetcode

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of LeetCode Solutions - ProgramCreek.com

1 LeetCode SolutionsProgram Array in Java72 Evaluate Reverse polish Notation93 Solution of Longest Palindromic Substring in Java114 Solution Word Break155 Word Break II186 Word Ladder207 Median of Two Sorted Arrays Java238 Regular Expression Matching in Java259 Merge Intervals2710 Insert Interval2911 Two Sum3112 Two Sum II Input array is sorted3213 Two Sum III Data structure design3314 3 Sum3415 4 Sum3616 3 Sum Closest3817 String to Integer (atoi)3918 Merge Sorted Array4019 Valid Parentheses4220 Implement strStr()432|181 Contents21 Set Matrix Zeroes4422 Search Insert Position4623 Longest Consecutive Sequence Java4724 Valid Palindrome4925 Spiral Matrix5226 Search a 2D Matrix5527 Rotate Image5628 Triangle5829 Distinct Subsequences Total6030 Maximum Subarray6231 Maximum Product Subarray6332 Remove Duplicates from Sorted Array6433 Remove Duplicates from Sorted Array II6734 Longest Substring Without Repeating Characters6935 Longest Substring Which Contains 2 Unique Characters7136 Palindrome Partitioning7337 Reverse Words in a String7538 Find

2 Minimum in Rotated Sorted Array7639 Find Minimum in Rotated Sorted Array II7740 Find Peak Element7841 Min Stack7942 Majority Element8043 Combination Sum8244 Best Time to Buy and Sell Stock8345 Best Time to Buy and Sell Stock II84 Program Creek3|181 Contents46 Best Time to Buy and Sell Stock III8547 Best Time to Buy and Sell Stock IV8648 Longest Common Prefix8849 Largest Number8950 Combinations9051 Compare Version Numbers9252 Gas Station9353 Candy9554 Jump Game9655 Pascal s Triangle9756 Container With Most Water9857 Count and Say9958 Repeated DNA Sequences10059 Add Two Numbers10160 Reorder List10561 Linked List Cycle10962 Copy List with Random Pointer11163 Merge Two Sorted Lists11464 Merge k Sorted Lists11665 Remove Duplicates from Sorted List11766 Partition List11967 LRU Cache12168 Intersection of Two Linked Lists12469 Java PriorityQueue Class Example12570 Solution for Binary Tree Preorder Traversal in Java1274|181 Program CreekContents71 Solution of Binary Tree Inorder Traversal in Java12872 Solution of Iterative Binary Tree Postorder Traversal in Java13073

3 Validate Binary Search Tree13174 Flatten Binary Tree to Linked List13375 Path Sum13476 Construct Binary Tree from Inorder and Postorder Traversal13677 Convert Sorted Array to Binary Search Tree13778 Convert Sorted List to Binary Search Tree13879 Minimum Depth of Binary Tree14080 Binary Tree Maximum Path Sum14281 Balanced Binary Tree14382 Symmetric Tree14583 Clone Graph Java14684 How Developers Sort in Java?14985 Solution Merge Sort LinkedList in Java15186 Quicksort Array in Java15487 Solution Sort a linked list using insertion sort in Java15688 Maximum Gap15889 Iteration vs.

4 Recursion in Java16090 Edit Distance in Java16391 Single Number16592 Single Number II16693 Twitter Codility Problem Max Binary Gap16694 Number of 1 Bits16795 Reverse Bits168 Program Creek5|181 Contents96 Permutations16997 Permutations II17198 Permutation Sequence17399 Generate Parentheses175100 Reverse Integer176101 Palindrome Number178102 Pow(x, n)1796|181 Program Creek1 Rotate Array in JavaYou may have been using Java for a while. Do you think a simple Java array questioncan be a challenge? Let s use the following problem to : Rotate an array of n elements to the right by k steps.

5 For example, with n=7and k =3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].How many different ways do you know to solve this problem? Solution 1 - Intermediate ArrayIn a straightforward way, we can create a new array and then copy elements to thenew array. Then change the original array by using ().public void rotate(int[] nums, int k) {if(k > )k=k% ;int[] result = new int[ ];for(int i=0; i < k; i++){result[i] = nums[ +i];}int j=0;for(int i=k; i< ; i++){result[i] = nums[j];j++;} ( result, 0, nums, 0, );}Space is O(n) and time is O(n). Solution 2 - Bubble RotateCan we do this in O(1) space?

6 This solution is like a bubble static void rotate(int[] arr, int order) {if (arr == null || order < 0) {throw new IllegalArgumentException("Illegal argument!");7|1811 Rotate Array in Java}for (int i = 0; i < order; i++) {for (int j = - 1; j > 0; j--) {int temp = arr[j];arr[j] = arr[j - 1];arr[j - 1] = temp;}}}However, the time is O(n*k). Solution 3 - ReversalCan we do this in O(1) space and in O(n) time? The following solution we are given1,2,3,4,5,6and order2. The basic idea is:1. Divide the array two parts: 1,2,3,4 and 5, 62. Rotate first part: 4,3,2,1,5,63. Rotate second part: 4,3,2,1,6,54.

7 Rotate the whole array: 5,6,1,2,3,4public static void rotate(int[] arr, int order) {order = order % ;if (arr == null || order < 0) {throw new IllegalArgumentException("Illegal argument!");}//length of first partint a = - order;reverse(arr, 0, a-1);reverse(arr, a, );reverse(arr, 0, );}public static void reverse(int[] arr, int left, int right){if(arr == null || == 1)return;while(left < right){int temp = arr[left];arr[left] = arr[right];arr[right] = temp;left++;right--;8|181 Program Creek}}2 Evaluate Reverse polish NotationThe problem:Evaluate the value of an arithmetic expression in Reverse polish operators are +, -,*, /.

8 Each operand may be an integer or examples:["2", "1", "+", "3", "*"] -> ((2 + 1)*3) -> 9["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> Naive ApproachThis problem is simple. After understanding the problem, we should quickly realizethat this problem can be solved by using a stack. We can loop through each elementin the given array. When it is a number, push it to the stack. When it is an operator,pop two numbers from the stack, do the calculation, and push back the following is the code. It runs great by feeding a small test. However, this code9|1812 Evaluate Reverse polish Notationcontains compilation errors in LeetCode .

9 Why?public class Test {public static void main(String[] args) throws IOException {String[] tokens = new String[] { "2", "1", "+", "3", "*" }; (evalRPN(tokens));}public static int evalRPN(String[] tokens) {int returnValue = 0;String operators = "+-*/";Stack<String> stack = new Stack<String>();for (String t : tokens) {if (! (t)) { (t);} else {int a = ( ());int b = ( ());switch (t) {case "+" ( (a + b));break;case "-" ( (b - a));break;case "*" ( (a*b));break;case "/" ( (b / a));break;}}}returnValue = ( ());return returnValue;}}The problem is that switch string statement is only available from Leetcodeapparently use versions below |181 Program Accepted SolutionIf you want to use switch statement, you can convert the above by using the followingcode which use the index of a string "+-*/".

10 Public class Solution {public int evalRPN(String[] tokens) {int returnValue = 0;String operators = "+-*/";Stack<String> stack = new Stack<String>();for(String t : tokens){if(! (t)){ (t);}else{int a = ( ());int b = ( ());int index = (t);switch(index){case 0 ( (a+b));break;case 1 ( (b-a));break;case 2 ( (a*b));break;case 3 ( (b/a));break;}}}returnValue = ( ());return returnValue;}}11|1813 Solution of Longest Palindromic Substring in Java3 Solution of Longest PalindromicSubstring in JavaFinding the longest palindromic substring is a classic problem of coding interview.


Related search queries