- 博客(27)
- 资源 (1)
- 收藏
- 关注
原创 Leetcode|Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in parentheses.
2015-06-29 17:07:59 198
原创 Leetcode|Anagrams
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.anangrams:举例说明,就是如果几个单词里面的元素完全相同(种类和个数),只是排列方式不同而形成的不同单词。他们就是anangrams;比如;r
2015-06-29 15:45:23 274
原创 Leetcode|Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.E
2015-06-29 15:17:27 230
原创 Leetcode|Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be ve
2015-06-29 11:03:21 219
原创 Leetcode|Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted form.Try to solve it in linear time/space.Return 0 if the array contains less than 2 elements
2015-06-29 10:01:39 276
原创 Leetcode|Sort List
Sort a linked list in O(n log n) time using constant space complexity.这个题有不少细节需要注意。完全实现不是很容易。注意点:1,如何像数组一样,把链表分组? 答:可以用快慢两个指针来搞定,但是注意链表一定要断开。 2,如何防止无限的归并下去。就是有一组一直是NULL;注意检验,快慢指
2015-06-28 20:01:19 171
原创 Leetcode|Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.借助合并两个链表的函数。超时的解法:最坏时间复杂度O((n-1)*n1+(n-2)*n2+....)class Solution {public: ListNode* mer
2015-06-28 16:51:29 205
原创 Leetcode|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.这是归并排序链表的基础。代码如下(C++):ListNode* mergeTwoLists(Lis
2015-06-28 16:21:11 202
原创 Leetcode|Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold add
2015-06-28 15:43:10 302
翻译 不用sqrt()函数,如何求平方根
1,牛顿迭代法牛顿法的作用是使用迭代的方法来求解函数方程的根。简单地说,牛顿法就是不断求取切线的过程。他可以快速求出平方根的近似值。例如,我们想求n的平方根(n>0),他的标准值是x.f(x)=x^2-n 就是这个函数与x轴交点出的正值就是所求x。首先随便猜一个近似值x,然后不断令x等于x和a/x的平均数,迭代个六七次后x的值就已经相当精确了。我们不断用过
2015-06-26 17:34:11 3075
原创 Leetcode|4 sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:
2015-06-26 12:25:38 231
原创 Leetcode|3 sum
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:Elements in a triplet (a,b,c
2015-06-26 11:54:54 190
原创 Leetcode|happy number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares
2015-06-26 10:47:26 212
原创 Leetcode|Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Wri
2015-06-26 09:51:38 197
原创 Leetcode|Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add and find.add - Add the number to an internal data structure.find - Find if there exists any pair of num
2015-06-26 09:33:53 310
原创 C++的函数模板
1,基本知识先了解下C++的引用作为函数返回值的情况;引用作为函数返回值,不产生值的副本,提高效率。2,初识函数模板#undef __STRICT_ANSI__#includeusing namespace std;templateT const& max_element(const T* var,size_t size){//引用作为函数返回值 T co
2015-06-25 14:46:42 331
转载 正确使用stl map的erase方法
来源:http://www.cnblogs.com/kex1n/archive/2011/12/06/2278505.html先声明:下面的文章是针对windows的用法,因为std::map的erase函数的windows的实现版本是返回一个std::map的迭代器,但是STL标准里面的该函数的返回值确是:map.erase有3个重载:void erase ( itera
2015-06-24 13:26:46 238
原创 LeetCode|Substring with Concatenation of All Words(所有单词的串联的子字符串)
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and
2015-06-24 12:10:26 228
原创 leetcode|36. Valid Sudoku
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.Note:A val...
2015-06-23 20:40:37 257
原创 Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.很简单的一道题,纯考写代码。注意边界
2015-06-23 19:00:21 209
翻译 Count primes
一般的解法:1,构建一个isPrime函数,对每一个n检测,从2扫到n-1,看是否有整除的。每个判断的时间复杂度就是O(n),整体时间复杂度O(n^2);2,其实扫到n/2就可以判断;3,其实扫到i*i4,可是如果n很大,这些方法都不好。因为终究要对每个n判断是否为质数。如果采用排除法,直接将已知的质数的倍数剔除。如果每个质数的从2开始的倍数进行剔除,很不划算,因为有很多重复的。
2015-06-23 18:11:13 304
原创 Leetcode|Max Points on a Line
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.开始的思路想用k和b两个参数来表示一条直线,这样就需要一个类或者结构体来实现。考虑到应用map容器的自动排序功能,需要写一个重载操作符的函数。于是用一个类来实现。class Solution;
2015-06-23 17:31:16 225
原创 链表的基本操作应用2---remove
链表经常需要删除的一些操作。虽然简单但是容易出错。1,Remove Nth Node From End of ListFor example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked...
2015-06-20 20:20:10 382
原创 链表反转
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */链表尤其是单向链表是一个比较简答的问题,一般容易出错的点是对结尾处NU...
2015-06-20 12:51:23 496
原创 有趣的二叉查找树
1,Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's.题目来源(leetcode) 1 3
2015-06-17 14:17:52 222
转载 error:swprintf and vswprintf not declared.解决办法
在codeblocks IDE环境下,借助MinGW编译出现的error。error: '::swprintf' has not been declarederror: '::vswprintf' has not been declared查了查网上的资料在stackoverflow上有人回答这个问题。在#include#undef __STRICT_ANSI__ 则问题解
2015-06-09 09:20:36 883
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人