![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++ leetcode
宁静的银杏树
这个作者很懒,什么都没留下…
展开
-
leetcode:Remove Nth Node From End of List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *re原创 2014-04-30 15:10:05 · 519 阅读 · 0 评论 -
leetcode:Reorder List
题目思路比较直接:1)把整个链表划分成2个等长的子链表,如果原链表长度为奇数,那么第一个子链表的长度多1。2)翻转第二个子链表;3)将两个子链表合并。代码里有些变量可以省去,为了看起来逻辑清晰,还是保留了。整个链表遍历了3次,但是没有使用额外空间。原创 2014-04-22 14:16:03 · 469 阅读 · 0 评论 -
leetcode:Sqrt(x) 牛顿迭代法求整数开方
牛顿迭代法求Sqrt(x) 为了方便理解,就先以本题为例: 计算x2 = n的解,令f(x)=x2-n,相当于求解f(x)=0的解,如左图所示。 首先取x0,如果x0不是解,做一个经过(x0,f(x0))这个点的切线,与x轴的交点为x1。 同样的道理,如果x1不是解,做一个经过(x1,f(x1))这个点的切线,与x轴的交点为x2。 以此类转载 2014-04-10 15:46:23 · 1556 阅读 · 0 评论 -
leetcode:Sort List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *so原创 2014-04-11 11:11:22 · 449 阅读 · 0 评论 -
leetcode:Plus One
class Solution {public: vector plusOne(vector &digits) { int len=digits.size(); int i,carry=1; for(i=len-1;i>=0;i--) { digits[i]=digits[i]+carry; carr原创 2014-04-11 14:01:04 · 477 阅读 · 0 评论 -
leetcode:Swap Nodes in Pairs
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *sw原创 2014-05-13 16:55:54 · 449 阅读 · 0 评论 -
leetcode:3Sum Closest
class Solution {public: int threeSumClosest(vector &num, int target) { int mingaps,gaps; sort(num.begin(),num.end()); mingaps=num[0]+num[1]+num[2]-target; if(mingaps==0) return ta原创 2014-04-28 15:06:38 · 531 阅读 · 0 评论 -
leetcode:3Sum
求一个队列中三个之和为0的子列,并以非原创 2014-04-28 14:13:11 · 543 阅读 · 0 评论 -
leetcode:4Sum
class Solution {public: vector > fourSum(vector &num, int target) { vector> result; result.clear(); sort(num.begin(),num.end()); int i,j,cur; for(i=0;i<num.size();i++) { i原创 2014-04-28 15:32:31 · 556 阅读 · 0 评论 -
Sum Root to Leaf Numbers
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2014-07-24 22:40:35 · 400 阅读 · 0 评论 -
leetcode: Merge Sorted Array O(n)解法
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B.原创 2014-08-04 22:03:57 · 1406 阅读 · 0 评论 -
leetcode:Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1转载 2014-09-04 22:58:33 · 450 阅读 · 0 评论 -
蓄水池抽样及实现
原文链接:蓄水池抽样(Reservoir Sampling )是一个很有趣的问题,它能够在o(n)时间内对n个数据进行等概率随机抽取,例如:从1000个数据中等概率随机抽取出100个。另外,如果数据集合的量特别大或者还在增长(相当于未知数据集合总量),该算法依然可以等概率抽样。说蓄水池抽样之前,先说一下等概率随机抽取问题,等概率随机抽取是一个很有用的东西,因为在很多情况下,尤其是搞模式转载 2014-08-23 19:44:28 · 651 阅读 · 0 评论 -
leetcode:Insertion Sort List
这道题算法思路很简单,就是简单插入算法,但是由于是对原创 2014-04-22 23:31:23 · 483 阅读 · 0 评论 -
leetcode:Longest Substring Without Repeating Characters
这道题比较简单,我用了暴力破解的解法,但要注意字符串s为空的特殊情况。class Solution {public: int lengthOfLongestSubstring(string s) { int i=0,j=0,max=1; int len=s.size(); int flag=len; if(len==0) ret原创 2014-03-26 16:26:21 · 433 阅读 · 0 评论 -
leetcode:String to Integer (atoi)
由于以前对atoi()函数理解的不够透彻,原创 2014-04-08 11:21:34 · 488 阅读 · 0 评论 -
leetcode:Palindrome Number
晚上做了两道回文的题目,一道是回文数,一道是回文字符串,自己写的代码考虑的不清楚,出现了许多逻辑错误,下面列了三种比较简洁的代码,供后面学习。1.第一种class Solution {public: bool isPalindrome(int x) { if(x<0) return false; if(x==0) return true;原创 2014-03-30 22:52:38 · 480 阅读 · 0 评论 -
Two Sum Leetcode 解法实现
这道题不难,只要用对工具的话,下面是时间复杂度为o(n)的解法,具体如下:class Solution {public: vector twoSum(vector &numbers, int target) { int len = numbers.size(); assert(len >= 2);原创 2014-03-22 16:41:41 · 1085 阅读 · 0 评论 -
leetcode:Binary Tree Postorder Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2014-04-17 22:23:06 · 498 阅读 · 0 评论 -
Leetcode:Evaluate Reverse Polish Notation
这道题主要是用stack栈来进行逆波兰式的算法描述,实现不是很难,不过注意string类型的一些基本函数,比如:stoi(),atoi()等。#include #include class Solution {public: int evalRPN(vector &tokens) { stack stas; int ret=0,len=tokens.siz原创 2014-03-23 10:29:05 · 706 阅读 · 0 评论 -
LeetCode题解:Single Number
class Solution {public: int singleNumber(int A[], int n) { int result=0; result=A[0]; for(int i=1;i<n;i++) { result=result^A[i]; }原创 2014-03-23 10:55:30 · 847 阅读 · 0 评论 -
二叉树的非递归遍历
二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的。对于二叉树,有前序、中序以及后序三种遍历方法。因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易理解而且代码很简洁。而对于树的遍历若采用非递归的方法,就要采用栈去模拟实现。在三种遍历中,前序和中序遍历的非递归算法都很容易实现,非递归后序遍历实现起来相对来说要难一点。一.前序遍历 前序转载 2014-04-17 22:27:30 · 484 阅读 · 0 评论 -
leetcode:Word Break
class Solution {public: bool wordBreak(string s, unordered_set &dict) { int n=(int)s.size(); vector dp(n+1,false); dp[0]=true; for(int i=1;i<=n;i原创 2014-03-25 16:49:30 · 1143 阅读 · 0 评论 -
leetcode: Unique Binary Search Trees
class Solution {public: int numTrees(int n) { if(n<=1) return 1; if(n==2) return 2; vector sum(n+1,0); sum[0]=1; sum[1]=1; int i,j; for(i=2;i<=n;i++)原创 2014-05-06 16:44:27 · 441 阅读 · 0 评论 -
Leetcode:Median of Two Sorted Arrays
求两个排队序列的中位数,我首先想到的是二路归并排序算法,这样做避免了各种情况的分析,相对比较简单,时间复杂度是O(m+n).class Solution {public: double findMedianSortedArrays(int A[], int m, int B[], int n) { int mid=(m+n)/2; int i=原创 2014-03-26 14:27:19 · 785 阅读 · 0 评论 -
leetcode:Binary Tree Inorder Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2014-05-07 20:39:06 · 476 阅读 · 0 评论 -
leetcode:Copy List with Random Pointer
比起标准的链表深拷贝,还需要了解一个random pointer的信息。需要建立起新的链表单元和旧的链表单元之间的一一对应关系,然后将所有旧的random pointer更换为新的random pointer。这样就要求两次扫描,O(2n)。原创 2014-04-27 17:35:23 · 436 阅读 · 0 评论 -
leetcode:Binary Tree Level Order Traversal
如果直接是层次遍历的话比较容易,关键是每层要原创 2014-05-07 21:06:35 · 589 阅读 · 0 评论 -
leetcode:Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3,原创 2014-09-08 12:17:58 · 553 阅读 · 0 评论