
Leetcode
文章平均质量分 61
lime1991
这个作者很懒,什么都没留下…
展开
-
Two Sum(LeetCode)
小硕即将面临找工作的压力,没事编编Leetcode。分享一些经验大家。刚开始,博主低估了LeetCode的要求,采用了一个最直观偷懒的方法。两层循环遍历,时间复杂度O(N^2)。LeetCode返回结果Time Limited Exceeded。贴出挫代码引以为戒。class Solution {public: vector twoSum(vector &numbers, i原创 2015-01-07 21:35:43 · 644 阅读 · 1 评论 -
合并有序链表
/从链表的头结点开始,若phead1>phead2,那么合并后链表的头结点是phead1.反之,则是phead2//剩余链表仍然有序,合并步骤同上一步一样,可采用递归的方法解决//边界条件 phead1为NULL,phead2为NULL#include<iostream>using namespace std;struct ListNode{ int value; ListN原创 2015-10-11 09:27:58 · 529 阅读 · 0 评论 -
树的子结构
//两棵二叉树判断B是不是A的子结构//1.在A中遍历,找到节点与B根节点的值相等的节点//2.对该节点和B树遍历,判断结构是否一样//递归#include<iostream>using namespace std;struct Node{ int value; Node* left; Node* right;};bool Traver(Node* pa, N原创 2015-10-11 09:45:12 · 583 阅读 · 0 评论 -
树的最近公共祖先
//如果是二叉搜索树的话,//若当前节点t大于节点u,v,说明u,v的LCA在t的左节点中,故从t的左节点中继续查找//若当前节点t小于节点u,v,说明u,v的LCA在t的右节点中,从t的右节点继续查找//当找到一个节点满足u<t<v时,该节点就是u,v的LCA//当t的一个节点的值等于u或v时,说明其中一个节点是另一个节点的父节点,返回t的父节点struct TreeNode{原创 2015-10-10 22:35:27 · 433 阅读 · 0 评论 -
链表中环的入口
#include<iostream>using namespace std;//1.先判断是否有环,快慢指针,慢指针一次向后前进一格,快指针一次向后前进两格(注意判断是否为NULL)//当快指针为NULL时,两指针仍为相遇则不存在环。//2.判断环中包含元素的个数。//以两指针相遇处为初始点,慢指针依次往后挪,并计数,当再次回到相遇点时,可求得包含元素个数n。//3.找出环的入口。指针p1原创 2015-10-10 16:21:35 · 408 阅读 · 0 评论 -
<leetcode>Merge Sorted Array
题目描述:Given two sorted integer arrays nums1 andnums2, 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 ad原创 2015-05-22 17:42:02 · 890 阅读 · 0 评论 -
<leetcode>Add Two Numbers
题目描述:You are given two linked lists representingtwo non-negative numbers. The digits are stored in reverse order and each oftheir nodes contain a single digit. Add the two numbers and return it as a原创 2015-05-22 20:27:25 · 427 阅读 · 0 评论 -
<LeetCode> Single Number
题目描述:Given an array of integers, every elementappears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi原创 2015-05-22 15:15:45 · 506 阅读 · 0 评论 -
<leetcode>String to Integer (atoi)
问题描述:Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible i原创 2015-05-26 21:37:21 · 387 阅读 · 0 评论 -
<Leetcode>Median of Two Sorted Arrays
题目描述:There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).解法:常规解法是将nums1和n原创 2015-05-26 16:44:51 · 336 阅读 · 0 评论 -
Longest Substring Without Repeating Characters (LeetCode)
问题描述:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is原创 2015-01-23 15:54:08 · 614 阅读 · 0 评论 -
C++ STL学习笔记十二 hash_map映照容器
转载自http://blog.youkuaiyun.com/cumirror/article/details/5596916/* * ************************************************************************************ * hash_map映照容器的基础说明: *************转载 2015-01-12 16:11:01 · 568 阅读 · 0 评论 -
test
/*深度优先搜索用栈(stack)来实现,整个过程可以想象成一个倒立的树形:1、把根节点压入栈中。2、每次从栈中弹出一个元素,搜索所有在它下一级的元素,把这些元素压入栈中。并把这个元素记为它下一级元素的前驱。3、找到所要找的元素时结束程序。4、如果遍历整个树还没有找到,结束程序。广度优先搜索使用队列(queue)来实现,整个过程也可以看做一个倒立的树形:1、把根节点放到队列的末尾。2原创 2015-09-23 23:29:26 · 354 阅读 · 0 评论