- 博客(89)
- 资源 (2)
- 收藏
- 关注
原创 Leetcode--Palindrome Partitioning II
Problem Description:Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example
2014-09-11 20:06:39
932
原创 Leetcode--Subsets II
Problem Description:Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution se
2014-09-09 21:05:21
955
原创 Leetcode--permutations II
Problem Description:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2
2014-09-09 13:02:12
1097
原创 Flatten Binary Tree to Linked List
Problem Description:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree s
2014-09-05 19:21:38
820
原创 Leetcode--Convert Sorted List to Binary Search Tree
Problem Description:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.分析:很容易想到的一种解法是将链表中所有的元素保存到数组中,然后每次取中间值进行构造,时间复杂度为O(n),空间复杂度为O(n)。具体
2014-09-05 17:23:46
803
原创 实现一个单例模式Singleton
分析:1.单例模式表明该类只有一个实例被创建,首先将类的构造方法设置为私有,然后写一个public静态函数用来创建实例,利用该类的一个静态指针来保存唯一实例,创建之前先判断是否已经存在,如果已经创建过了就不再创建。2.考虑到线程安全和异常安全可以在静态函数中加锁。3.考虑在合适的时机析构创建的实例,因此在该类中定义一个内嵌类delInstance,然后在singleton中添加一个de
2014-09-05 15:28:51
846
原创 自己实现的string类
自己实现的一个string类,包括基本构造,复制构造,赋值和析构函数,比较函数,输入输出函数,锻炼一下动手能力。#include #include #include using namespace std;class MyString{public: MyString(const char *s=NULL); MyString(const MyString& rh
2014-09-01 20:11:24
996
原创 Leetcode--Permutation Sequence
Problem Description:The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):
2014-08-30 17:05:19
898
原创 Leetcode--Reorder List
Problem Description:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given
2014-08-15 21:57:52
643
原创 Leetcode--Flatten Binary Tree to Linked List
Problem Description:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree sho
2014-08-15 10:20:17
792
原创 Leetcode--Sort List
Problem Description:Sort a linked list in O(n log n) time using constant space complexity.分析:对链表进行排序,思考排序算法时间复杂度为O(nlogn)的只有归并,快排和堆排序,应用到链表上的归并比较合适,这里利用快慢指针找到链表的中间节点,然后分别对两边递归归并排好序后将两边归并即可得到最终
2014-08-13 21:48:47
642
原创 leetcode--Reverse Words in a String
Problem Description:Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarificat
2014-08-13 20:42:24
636
原创 linux编程实例--简单多进程服务器
主要利用fork事先创建若干个进程,并发处理多个客户端的连接,返回当前系统时间。具体代码如下:server.c# include # include # include # include # include # include # include # include # include #define BUFFLEN 1024#define SERVER_PORT
2014-08-13 17:22:00
911
原创 Leetcode--Generate Parentheses
Problem Description:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())",
2014-08-08 21:15:31
768
原创 Leetcode--Recover Binary Search Tree
Problem Description:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straigh
2014-08-08 20:44:33
1015
原创 Leetcode--N-Queens
Problem Description:The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solution
2014-08-08 10:50:25
805
原创 Leetcode--Merge Intervals
Problem Description:Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].分析:按照要求将区间合并,首先将按照按起点排序,然后
2014-08-07 16:58:12
866
原创 c++中static和inline关键字
一、static变量和static函数static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别? 答: 1) 全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量。全局变量本身就是静态存储方式, 静态全局变量当然也是静态存储方式。 这两者在存储方式上并无不同。这两者的区别在于非静态全局变量的作
2014-08-07 16:39:44
3199
原创 Leetcode--Pow(x, n)
Problem Description:Implement pow(x, n).分析:题目意思很简单,要求计算x的n次幂,其中x给的是double类型,n需要考虑负数的情况,利用二分的思想每次将n减半,递归计算可以得到最终结果,其中一些细节需要注意,具体的实现代码如下:class Solution {public: bool isequal(double a,doubl
2014-08-07 15:41:22
750
原创 Leetcode--Jump Game
Problem Description: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 tha
2014-08-07 15:32:44
699
原创 Leetcode--Next Permutation
Problem Description:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rea
2014-08-05 21:01:43
680
原创 Leetcode--Roman to Integer
Problem Description:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.分析:题目的意思是将罗马数字转化成整数,首先是在网上找到关于罗马数字表示法的规则如下: 1、计数方法:① 罗马数字就
2014-08-05 20:08:07
721
原创 Leetcode--Divide Two Integers
Problem Description:Divide two integers without using multiplication, division and mod operator.分析:题目意思很容易理解,就是不用乘除法和模运算求来做除法,很容易想到的一个方法是一直做减法,然后计数,但是提交之后显示超时,在网上找到一种解法,利用位运算,意思是任何一个整数可以表示成以2的幂为
2014-08-04 20:43:31
810
原创 Leetcode--Multiply Strings
Problem Description:Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.分析:两个string相乘
2014-08-01 15:35:08
712
原创 Leetcode--Anagrams
Problem Description:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.分析:题目要求输出找出所有在字符串数组中的变形词,变形词的意思是指单词由相同的字母构成,只是字母在单词中的顺序
2014-08-01 15:04:52
768
原创 Leetcode--Remove Duplicates from Sorted Array
Problem Description:Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you
2014-07-31 21:13:50
598
原创 Leetcode--ZigZag Conversion
Problem Description:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
2014-07-31 20:55:57
648
原创 Leetcode--Add Two Numbers
Problem Description:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbe
2014-07-31 20:19:10
823
原创 Leetcode--Two Sum
Problem Description:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they
2014-07-31 20:08:25
733
原创 c++中STL之heap, priority_queue使用
C++中堆的应用:make_heap, pop_heap, push_heap, sort_heap, priority_queuemake_heap, pop_heap, push_heap, sort_heap都是标准算法库里的模板函数,用于将存储在vector/deque 中的元素进行堆操作,对不愿自己写数据结构堆的C++选手来说,这几个算法函数很有用,下面是这几个函数操作vecto
2014-07-30 20:36:39
15453
原创 c++中const关键字全面总结
一、const作用1、const定义常量注意:const只对它左边的东西起作用,唯一的例外就是const本身就是最左边的修饰符,那么它才会对右边的东西起作用。(1)const修饰变量,以下两种定义形式在本质上是一样的。它的含义是:const修饰的类型为TYPE的变量value是不可变的。 TYPE const ValueName = value; const
2014-07-29 19:20:00
901
原创 Leetcode--Combination Sum II
Problem Description:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only b
2014-07-26 10:00:09
821
原创 linux进程通信之SYSTEM V信号量
信号量的使用主要是用来保护共享资源,使得资源在一个时刻只有一个进程(线程)所拥有。信号量的值为正的时候,说明它空闲。所测试的线程可以锁定而使用它。若为0,说明它被占用,测试的线程要进入睡眠队列中,等待被唤醒。一、信号量的分类:在学习信号量之前,我们必须先知道——Linux提供两种信号量:(1) 内核信号量,由内核控制路径使用。(2) 用户态进程使用的信号量,这种信号量又分为POSI
2014-07-25 19:38:24
1418
原创 linux进程通信之共享内存
共享内存允许两个或多个进程共享一给定的存储区,因为数据不需要来回复制,所以是最快的一种进程间通信机制。共享内存可以通过mmap()映射普通文件(特殊情况下还可以采用匿名映射)机制实现,也可以通过系统V共享内存机制实现。应用接口和原理很简单,内部机制复杂。为了实现更安全通信,往往还与信号量等同步机制共同使用。下面主要介绍系统V共享内存机制,主要用到的系统API包括:1.shmget函数:获得一个
2014-07-25 16:01:01
929
原创 Leetcode--Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numb
2014-07-24 18:51:34
565
原创 找出所有最长连续重复子串及其个数
问题描述:找出字符串中所以最长连续重复子串及其个数比如:输入:123234,最大连续重复字符串为23,个数为2 输入:5555,最大连续重复字符串为555,个数为2 输入:aaabbb 最大连续重复字符串为aa,个数为2;和bb,个数为2必须存在重复的字符串才算,只出现一次的不算。可能存在多个相同长度的不同字符串,比如aaabbb。
2014-07-23 20:17:17
4604
原创 Leetcode--Reverse Nodes in k-Group
Problem Description: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 left-out nodes in the
2014-07-11 15:32:00
761
原创 linux 网络编程 socket bind failed 问题解决
今天写一个简单的socket网络通讯的程序的时候,用ctrl+c结束服务器端程序之后,再次启动服务器出现了bind failed:the address already in use的错误。在网上查了一下以后找到了原因,在此记录一下。这个IBM的官网上说到了这一点:http://www.ibm.com/developerworks/cn/linux/l-sockpit/。详细介绍如下:
2014-07-09 15:28:45
10976
原创 Leetcode--3Sum
Problem Description:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:El
2014-07-07 19:26:58
928
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人