LeetCode – Min Stack

题目: 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); minStack.getMin(); --> Returns -3. minStack.pop(); minStack.top(); --> Returns 0. minStack.getMin(); --> Returns -2. 解题: 与普通栈不同,增加了一个返回栈最小值的功能, 这里使用了两个栈, 一个保存数据 , 一个保存压栈过程中出现过的最小值。 ...

2019-07-10 · 2 min · 224 words

LeetCode – Perfect Squares

题目: Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. Example 1: Input: n = 12 Output: 3 Explanation: 12 = 4 + 4 + 4. Example 2: Input: n = 13 Output: 2 Explanation: 13 = 4 + 9. 解题1: 数学方式的解法:基于四平方和理论(Lagrange’s four-square theorem), 每个正整数均可表示为4个整数的平方和。 1. 如果整数包含因子4, 可以去掉不影响结果; 2. 如果除8余7的话, 肯定是由四个完全平方数组成; 3. 之后按照两个数的平方和拆解整数, 其中!!逻辑取反判断是否正整数还是0, 正整数!!后为1, 0!!后为0; ...

2019-07-02 · 2 min · 349 words

LeetCode – Open the Lock

题目: You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot. The lock initially starts at '0000', a string representing the state of the 4 wheels. ...

2019-06-23 · 2 min · 400 words

LeetCode – Rotate List

题目: 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: 1->2->0->NULL rotate 3 steps to the right: 0->1->2->NULL rotate 4 steps to the right: 2->0->1->NULL ...

2019-06-03 · 1 min · 178 words

LeetCode-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. Example 1: Input: {"$id":"1","next":{"$id":"2","next":null,"random":{"$ref":"2"},"val":2},"random":{"$ref":"2"},"val":1} Explanation: Node 1's value is 1, both of its next and random pointer points to Node 2. Node 2's value is 2, its next pointer points to null and its random pointer points to itself. Note: ...

2019-05-28 · 2 min · 371 words

LeetCode – Flatten a Multilevel Doubly Linked List

题目: You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below. Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list. ...

2019-05-20 · 1 min · 213 words

LeetCode – Add Two Numbers

题目: 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 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807. 解题: 生成结果链表哨兵节点, 从输入两个链表的开始节点相加 再加上进位值 carry, 得到结果sum, sum/10 存放到变量进位结果变量carry中, 将sum%/10为当前位的值,存放到链表当前节点,同时两个输入链表,结果链表移动到下一个节点操作。 循环处理直到链表遍历完毕或者进位为0,返回哨兵节点的下一个节点。 ...

2019-05-12 · 2 min · 300 words