algo

算法练习LinkedList(一)--P2

link on JianShu 已废弃,参考–算法练习LinkedList(三): P2、P19 linked-list 2. Add Two Numbers Medium You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain

算法练习LinkedList(七)--P82

link on JianShu Remove Duplicates from Sorted List 2 Medium Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Example 1: Input: 1->2->3->3->4->4->5 Output: 1->2->5 Example 2: Input: 1->1->1->2->3 Output: 2->3 昨天做得有点郁闷了。‘精准原子’操作的套路还是没有掌握,

算法练习LinkedList(三)--P2、P19

link on JianShu linked-list Medium 2. Add Two Numbers Medium You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example: Input: (2 -> 4

算法练习LinkedList(九)--P92

link on JianShu 92. Reverse Linked List II Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL 可以先复习一下翻转链表的实现。链表操作两个基本场景—— 将

算法练习LinkedList(二)--P19

link on JianShu 已废弃,参考–算法练习LinkedList(三): P2、P19 19. Remove Nth Node From End of List Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5,

算法练习LinkedList(五)--P61

link on JianShu Rotate List Medium Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3->4->NULL rotate 2 steps to the right: 4->5->1->2->3->NULL Example 2: Input: 0->1->2->NULL, k = 4 Output: 2->0->1->NULL Explanation: rotate 1 steps to the right: 2->0->1->NULL rotate 2 steps to the right:

算法练习LinkedList(八)--P86

link on JianShu 86. Partition List Medium Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Example: Input: head = 1->4->3->2->5->2, x = 3 Output: 1->2->2->4->3->5 梳理一下思路,应该很快

算法练习LinkedList(六)--P83

link on JianShu Remove Duplicates from Sorted List Easy Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2 Example 2: Input: 1->1->2->3->3 Output: 1->2->3 直接的实现对于最后一个节点的处理不够精细,调试后完成了算法。 Runtime: 2 ms, faster

算法练习LinkedList(十)--P138

link on JianShu 138. Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. Java中的深拷贝:对象中的对象需要重写clone方法,将

算法练习LinkedList(十一)--P109

link on JianShu 109. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Example: Given the sorted linked list: [-10,-3,0,5,9], One possible answer

算法练习LinkedList(四)--P21

link on JianShu 21. Merge Two Sorted Lists Easy 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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 这是个Easy级别的,有前两次的经验,一次做对,但执行

算法练习Stack(一)--P20

link on JianShu 栈stack valid parentheses Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid. 先从最

算法练习Stack(七)--P496

link on JianShu 栈stack 496. Next Greater Element I Easy You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2. The Next Greater Number of a number x in nums1 is the first greater number to its

算法练习Stack(三)--P42

link on JianShu 栈stack 42. Trapping Rain Water Hard Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Thanks Marcos for contributing this image! Example:

算法练习Stack(二)--P71

link on JianShu 栈stack 71. simplify path Medium Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path. In a UNIX-style file system, a period . refers to the current directory. Furthermore, a double period .. moves the directory up a level. For more information, see: Absolute path vs relative path in Linux/Unix Note that

算法练习Stack(五)--P225、232

link on JianShu 栈stack 225. Implement Stack using Queues Easy 232. Implement Queue using Stacks Easy 这两道提互为实现,刚好放到一起学习。既然两种数据结构可以互相实现,可以看看有什么相似之处。 队列qu

算法练习Stack(六)--P173

link on JianShu 栈stack 173. Binary Search Tree Iterator Medium Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Example: BSTIterator iterator = new BSTIterator(root); iterator.next(); // return 3 iterator.next(); // return 7 iterator.hasNext(); // return true iterator.next(); // return 9 iterator.hasNext();

算法练习Stack(四)--P155

link on JianShu 栈stack 155. Min Stack Easy Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. getMin() – Retrieve the minimum element in the stack. Example: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3);

算法练习Stack--P1021-easy

link on JianShu 栈stack 1021. Remove Outermost Parentheses A valid parentheses string is either empty (""), “(” + A + “)”, or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, “”, “()”, “(())()”, and “(()(()))” are all valid parentheses strings. A valid parentheses string S is primitive if it is nonempty, and there does not

算法练习Stack--P1047-easy

link on JianShu 栈stack 1047. Remove All Adjacent Duplicates In String Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them. We repeatedly make duplicate removals on S until we no longer can. Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique. Example 1:

算法练习Stack--P144-Medium

link on JianShu 栈stack 144. Binary Tree Preorder Traversal Medium Given a binary tree, return the preorder traversal of its nodes’ values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,2,3] Follow up: Recursive solution is trivial, could you do it iteratively? 前序就是根节点在最前根->左->

算法练习Stack--P145-Hard

link on JianShu 栈stack 145. Binary Tree Postorder Traversal Hard Given a binary tree, return the postorder traversal of its nodes’ values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [3,2,1] Follow up: Recursive solution is trivial, could you do it iteratively? 后序遍历在访问完左子树向上回退到根节点的时候

算法练习Stack--P844-easy

link on JianShu 栈stack 844. Backspace String Compare Easy Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. Example 1: Input: S = “ab#c”, T = “ad#c” Output: true Explanation: Both S and T become “ac”. Note: 1 <= S.length <= 200 1 <= T.length <= 200 S and T

算法练习Stack--P94-Medium

link on JianShu 栈stack 94. Binary Tree Inorder Traversal Medium Given a binary tree, return the inorder traversal of its nodes’ values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] Follow up: Recursive solution is trivial, could you do it iteratively? 先了解一下 preorder (前序),inorder(中序)

Linked list problems solution

linked-list Problem 2 Add Two Numbers 2. Add Two Numbers Medium You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example: Input: (2 ->