L_J_SHOU

2014年2月22日

链表操作----将单链表向右旋转 K 个位置

摘要: 给定一个单链表,设计一个算法实现链表向右旋转 K 个位置。举例: 给定 1->2->3->4->5->6->NULL, K=3 则 4->5->6->1->2->3->NULL分析:1. 这道题目的关键在于找到 尾节点和旋转后新链表的尾节点。 假设是 tail, new->tail.然... 阅读全文

posted @ 2014-02-22 11:40 L_J_SHOU 阅读(489) 评论(0) 推荐(0)

2014年2月21日

LeetCode----3 Sum

摘要: Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:Elemen... 阅读全文

posted @ 2014-02-21 22:43 L_J_SHOU 阅读(103) 评论(0) 推荐(0)
LeetCode----Reverse Nodes in k-Group

摘要: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then le... 阅读全文

posted @ 2014-02-21 20:34 L_J_SHOU 阅读(101) 评论(0) 推荐(0)

2014年2月20日

LeetCode----Swap Nodes in Pairs

摘要: Given a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2->1->4->3.Your algor... 阅读全文

posted @ 2014-02-20 23:43 L_J_SHOU 阅读(120) 评论(0) 推荐(0)
LeetCode----Merge K sorted Lists

摘要: Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.采用最小堆,首先将 k 个首节点放入堆中,弹出最小的节点并插入到新的链表中; 弹出的节点如... 阅读全文

posted @ 2014-02-20 23:04 L_J_SHOU 阅读(144) 评论(0) 推荐(0)
STL 源码分析《1》---- list 归并排序的 迭代版本, 神奇的 STL list sort

摘要: 最近在看 侯捷的 STL源码分析,发现了以下的这个list 排序算法,乍眼看去,实在难以看出它是归并排序。平常大家写归并排序,通常写的是 递归版本。。为了效率的考虑,STL库 给出了如下的 归并排序的迭代版本.1. MergeSort 的递归版本首先分析下 MergeSort 的递归版本是如何工作的... 阅读全文

posted @ 2014-02-20 19:38 L_J_SHOU 阅读(256) 评论(0) 推荐(0)

2014年2月19日

STL源码分析----神奇的 list 的 sort 算法实现

摘要: STL中有一个std::sort算法,但它是不支持std::list的,因为list不提供RandomIterator的支持,但list自己提供了sort算法,把list的元素按从小到大的方式来排序,代码长度到不长,但真是难以读懂,后来扣持了一下午终于搞明白了,贴个总结上来。list::sort的代... 阅读全文

posted @ 2014-02-19 20:23 L_J_SHOU 阅读(412) 评论(0) 推荐(1)
数据结构---设计一个栈,push, pop, min 时间复杂度都是 O(1)

摘要: 普通的栈,push, pop 操作的复杂度是 O(1), 但是如果要找出其中的最小值,则需要 O(N)的时间。题目要求 min 复杂度也是 O(1), 做法便是 空间换时间,每一步栈的最小值都用一个数组记录下来。。// copyright @ L.J.SHOU Feb.17, 2014// desi... 阅读全文

posted @ 2014-02-19 08:48 L_J_SHOU 阅读(305) 评论(0) 推荐(0)

2014年2月16日

LeetCode----Longest Palindromic Substring

摘要: Description:Given a stringS, find the longest palindromic substring inS. You may assume that the maximum length ofSis 1000, and there exists one uniqu... 阅读全文

posted @ 2014-02-16 20:54 L_J_SHOU 阅读(126) 评论(0) 推荐(0)
LeetCode----Insertion sort list

摘要: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; *... 阅读全文

posted @ 2014-02-16 17:11 L_J_SHOU 阅读(114) 评论(0) 推荐(0)