
algorithms
zxrzhang
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Leetcode - 429 - N-ary Tree Level Order Traversal
Recursive:Java:private List<List<Integer>> dfs(Node node, List<List<Integer>> res, int level) { if (node == null) return res; if (res.size() == level) res.add(new ArrayList<>()); res.get(level).a..原创 2021-02-09 11:42:35 · 195 阅读 · 0 评论 -
Leetcode - 589 - N-ary Tree Preorder Traversal
Recursive:Java:class Solution { public List<Integer> list = new ArrayList<>(); public List<Integer> preorder(Node root) { if (root == null) return list; list.add(root.val); for(N..原创 2021-02-09 11:30:46 · 242 阅读 · 0 评论 -
Leetcode - 590 - N-ary Tree Postorder Traversal (easy)
Recursive:Java:class Solution { List<Integer> list = new ArrayList<>(); public List<Integer> postorder(Node root) { if (root == null) return list; for(Node node: root.children) ..原创 2021-02-09 11:18:24 · 188 阅读 · 0 评论 -
Leetcode - 144 - Binary Tree Preorder Traversal (medium)
Recursive:Java:public List<Integer> preorderTraversal(TreeNode root) { List<Integer> pre = new LinkedList<Integer>(); preHelper(root,pre); return pre;}public void preHelper(TreeNode root, List<Integer> pre) { if(root==nul原创 2021-02-09 11:06:38 · 159 阅读 · 0 评论 -
Leetcode - 94 - Binary Tree Inorder Traversal (medium)
Iterativeright child doesn't exist: save all the children on the left chain does exist: save the top of the stack in the resultPython:def inorderTraversal(self, root): res, stack = [], [] while (root or stack...原创 2021-02-09 10:57:02 · 154 阅读 · 1 评论 -
Leetcode - 49 - Group Anagrams (medium)
Given an array of stringsstrs, groupthe anagramstogether. You can return the answer inany order.AnAnagramis a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once....原创 2020-12-19 23:28:56 · 157 阅读 · 2 评论 -
Leetcode - 242 - Valid Anagram (easy)
Solutions:(1) Arrays.sort(str) directlyJava:public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { return false; } char[] str1 = s.toCharArray(); char[] str2 = t.toCharArray(); Arrays.sort(str1);..原创 2020-12-19 23:09:48 · 198 阅读 · 1 评论 -
Leetcode - 239 - Sliding Window Maximum (hard)
You are given an array of integersnums, there is a sliding window of sizekwhich is moving from the very left of the array to the very right. You can only see theknumbers in the window. Each time the sliding window moves right by one position.Return...原创 2020-12-19 18:18:36 · 139 阅读 · 1 评论 -
Leetcode - 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 ele原创 2020-12-19 16:22:06 · 203 阅读 · 1 评论 -
Leetcode - 20 - Valid Parentheses (easy)
Given a stringscontaining 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 ...原创 2020-12-17 19:36:46 · 219 阅读 · 2 评论 -
Leetcode - 42 - Trapping Rain Water (hard)
Givennnon-negative integers representing an elevation map where the width of each bar is1, compute how much water it can trap after raining.Solutions:ans = sum {min(left_max,right_max)−height[i]min(left_max,right_max)−height[i] for all element...原创 2020-12-10 10:15:45 · 160 阅读 · 0 评论 -
Leetcode - 641 - Design Circular Deque (medium)
Solutions:C++:#include <vector>#include <iostream>using namespace std;class MyCircularDeque {private: vector<int> buffer; int cnt; int k; int front; int rear;public: /** Initialize your data structure..原创 2020-12-10 00:21:16 · 109 阅读 · 0 评论 -
Leetcode - 66 - Plus One (easy)
Given anon-emptyarray of decimal digitsrepresenting a non-negative integer, incrementone to the integer.The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.You...原创 2020-12-09 08:00:45 · 112 阅读 · 0 评论 -
Leetcode - 88 - Merge Sorted Array (easy)
Given two sorted integer arraysnums1andnums2, mergenums2intonums1as one sorted array.Note:The number of elements initialized innums1andnums2aremandnrespectively. You may assume thatnums1has enough space (size that isequaltom+n) t...原创 2020-12-08 20:54:26 · 133 阅读 · 0 评论 -
Leetcode - 84 - Largest Rectangle in Histogram (hard)
Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.原创 2020-12-02 00:41:53 · 128 阅读 · 2 评论 -
Leetcode - 21 - Merge Two Sorted Lists (easy)
Merge two sorted linked lists and return it as a newsortedlist. The new list should be made by splicing together the nodes of the first two lists.Solutions:(1) Recursive:The recursive function mergeTwoLists(ListNode l1, ListNode l2) mean merge...原创 2020-12-01 15:15:08 · 173 阅读 · 0 评论 -
Leetcode - 189 - Rotate Array (medium)
Given an array, rotate the array to the right byksteps, wherekis non-negative.Follow up:Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. Could you do it in-place with O(1) extra space?So...原创 2020-12-01 13:39:55 · 155 阅读 · 0 评论 -
Leetcode - 25 - Reverse Nodes in k-Group (hard)
Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.kis 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 ofkthen left-out nodes, in the...原创 2020-11-29 00:20:42 · 308 阅读 · 0 评论 -
Leetcode - 26 - Remove Duplicates from Sorted Array
Given a sorted arraynums, remove the duplicatesin-placesuch that each element appears onlyonceand returns the new length.Do not allocate extra space for another array, you must do this bymodifying the input arrayin-placewith O(1) extra memory.C...原创 2020-11-28 21:12:49 · 94 阅读 · 0 评论 -
Leetcode - 142 - Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following thenextpointer. Internally,posis us...原创 2020-11-27 14:31:36 · 128 阅读 · 0 评论 -
Leetcode - 141 - Linked List Cycle
Givenhead, the head of a linked list, determine if the linked list has a cycle in it.There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following thenextpointer. Internally,posis used to den...原创 2020-11-27 13:37:26 · 157 阅读 · 0 评论 -
Leetcode - 24 - Swap Nodes in Pairs
Given alinked list, swap every two adjacent nodes and return its head.You maynotmodify the values in the list's nodes. Only nodes itself may be changed.Solutions:(1) Recursive: when head.next.next pairs are swapped, only need to swap the first t...原创 2020-11-25 14:10:19 · 128 阅读 · 1 评论 -
Leetcode - 206 - Reverse Linked List
Reverse a singly linked list.Example:Follow up:A linked list can be reversed either iteratively or recursively. Could you implement both?Solutions:(1) IterativeWe want the curr->next to be prev and in order to iterate in the loop, three.原创 2020-11-24 14:01:47 · 121 阅读 · 0 评论 -
Leetcode - 70 - Climbing Stairs
You are climbing a staircase. It takes n steps to reach the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Solutions:(1) Appliesto a small nTry to compose the big abstract problem by simila..原创 2020-11-23 23:49:29 · 169 阅读 · 0 评论 -
Leetcode - 11 - Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a cont.原创 2020-11-23 15:06:05 · 111 阅读 · 0 评论 -
Leetcode - 283 - move zeros
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.Solutions:(1) In the first loop, count the number of zeros. In the second loop, if not zero, move forward; ...原创 2020-11-21 19:45:28 · 207 阅读 · 0 评论 -
Leetcode - 15 - Three Sum
Given an arraynumsofnintegers, are there elementsa,b,cinnumssuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Notice that the solution set must not contain duplicate triplets.Solutions:(1) O(n^3...原创 2020-11-21 18:00:02 · 204 阅读 · 0 评论 -
Leetcode - 1 - Two Sum
1. Two SumGiven an array of integersnumsand an integertarget, returnindices of the two numbers such that they add up totarget.You may assume that each input would haveexactlyone solution, and you may not use thesameelement twice.You can retu...原创 2020-11-21 17:26:42 · 119 阅读 · 0 评论 -
circular queue 多语言多种方法对比 (整理自 leetcode discussion)
解析:每种方法都需要设置代表首、尾的变量,设为front、rear。 在rear位置插入即入队列,front位置删除即出队列。 循环队列找到首尾的算法都是 (rear或front + 1) % length。C++class MyCircularQueue {public: /** Initialize your data structure here. Set th...原创 2019-01-07 16:28:40 · 394 阅读 · 0 评论