文章 224
评论 6
浏览 177810
重排奇偶有序链表

重排奇偶有序链表

给定一个链表,其中奇数位是升序的,偶数位是降序的,实现链表的排序。

LinkedList - 21. Merge Two Sorted Lists

LinkedList - 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.

LinkedList - 328. Odd Even Linked List

LinkedList - 328. Odd Even Linked List

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

LinkedList - 206. Reverse Linked List

LinkedList - 206. Reverse Linked List

Reverse a singly linked list.

LinkedList - 92. Reverse Linked List II

LinkedList - 92. Reverse Linked List II

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 思路: 找到m的前一位,然后翻转n-m+1个节点,链表翻转类型的题目常规套路都是使用一个dummy来保存head链表头。因为题目说了链表长度比m和n都要大,所以不用额外关注m和n会不会导致空指针。 代码: java: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { publ....

接口实现幂等的几种方式

接口实现幂等的几种方式

最近处理的单据业务经常会因为接口不幂等导致数据出错的情况。所以幂等性在设计系统的时候一定要考虑进去,尤其是像支付宝、银行、互联网金融公司等涉及的都是钱的系统,既要高效,也要准确,所以不能出现多扣款、多打款等问题,不然这样会很难处理,用户体验也不会好。

Design - 146. LRU Cache

Design - 146. LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.

1496. Path Crossing

1496. Path Crossing

Return True if the path crosses itself at any point, that is, if at any time you are on a location you've previously visited. Return False otherwise.

Math - 50. Pow(x, n)

Math - 50. Pow(x, n)

Implement pow(x, n), which calculates x raised to the power n (xn).

680. Valid Palindrome II

680. Valid Palindrome II

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.

LinkedList - 142. Linked List Cycle II

LinkedList - 142. Linked List Cycle II

Given a linked list, return the node where the cycle begins.

LinkedList - 876. Middle of the Linked List

LinkedList - 876. Middle of the Linked List

Given a non-empty, singly linked list with head node head, return a middle node of linked list.

golang gctrace分析gc过程

golang gctrace分析gc过程

gctrace主要是用于跟踪GC的不同阶段的耗时与GC前后的内存量对比。可以用于对runtime本身进行调试之外,还可以观察线上应用的GC情况。

常见的限流算法

常见的限流算法

令牌桶:初始化一定数量的令牌,每个请求都需要先到桶里获取令牌才能获得服务,如果桶中的令牌数量为零,则拒绝服务,丢弃请求。

Array - 309. Best Time to Buy and Sell Stock with Cooldown

Array - 309. Best Time to Buy and Sell Stock with Cooldown

Design an algorithm to find the maximum profit.

Array - 55. Jump Game

Array - 55. Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position.

Array - 75. Sort Colors

Array - 75. Sort Colors

Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.

一致性哈希的golang实现

一致性哈希的golang实现

一致性hash算法在1997年由麻省理工学院 karger等人在解决分布式Cache中提出。一个好的hash算法应该满足四个条件:均衡性(Balance)、单调性(Monotonicity)、分散性(Spread)和负载(Load)。

正则表达式

正则表达式

正则表达式(regular expression)描述了一种字符串匹配的模式(pattern),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。

golang实现跳表(SkipList)

golang实现跳表(SkipList)

跳表的理解 如果要维护一组有序的整数序列,支持高效的插入删除和搜索,并且能维护序列的有序性。可以选择的数据结构是有限的,哈希表虽然支持常数时间复杂度的增删查,但是hashmap不能维护有序性。红黑树可以维持有序性,并且增加删除修改的性能也很好,但是红黑树不能支持范围搜索。跳表这种数据结构利用空间换时间,性能和红黑树比肩,还能支持区间搜索,在redis和leveldb等开源项目中都用被应用。 跳表的结构如图所示: 在最底层包含了所有的数据节点,每一层链表都有一个指向下一层和自己相同的节点,向后的指针指向随机的同一层比自己大的数据。 时间复杂度分析 不难理解,对于一个有n个节点的链表,如果每两个节点会提取出一个节点作为索引节点,那么第一层的节点个数为n/2,再往上一层,节点数变成n/4,以此类推,第k层的索引个数为n/(2^k),假设层数有x层,并且第x层的节点个数为2,也就是n/(2^x) = 2,所以x = (logn - 1),而第0层是链表本身,所以整个跳表的高度就是logn,如果每一层都需要遍历m个节点,那么在跳表中查询某个数的时间复杂度就是O(m * log(n))。 简单的....

Nothing just happens, it's all part of a plan.