- 博客(7)
- 资源 (9)
- 收藏
- 关注
原创 Valid Anagram
方法1 先排序 后判断是否相等#include "stdafx.h" #include <algorithm> #include <string> using namespace std; class Solution { public: bool isAnagram(string s, string t) { sort(s.begin(), s.end());
2016-02-29 22:21:06 329
原创 binary-search-tree-iterator
https://leetcode.com/problems/binary-search-tree-iterator/ 利用非递归 中序遍历的思想class BSTIterator { private: stack<TreeNode*> s; public: BSTIterator(TreeNode *root) { while (!s.empty())
2016-02-29 21:24:42 418
原创 ntersection-of-two-linked-lists
https://leetcode.com/problems/intersection-of-two-linked-lists/ 由于链表的最后是合在一起的 所以在遍历的时候到末尾的长度一定相等的 假设两条链表的长度是m n m>n 方法是从短链表的头部 和 从长链表 距离链表尾部和短链表一样长的地方开始遍历 如果指向相同的地址就把这个地址返回x指向长链表的首部 y指向短链表的首部 x
2016-02-28 15:27:23 422
原创 Search in Rotated Sorted Array
采用二分的思路对于数组 二分之后只可能出现上图3种情况 对于每种情况 每次搜索原来范围的一半#include <iostream> #include <vector> using namespace std; class Solution { public: int search(vector<int>& nums, int target) { return searchb
2016-02-26 14:44:14 398
原创 Remove Duplicates from Sorted Array II
这也是一道去除重复元素的题目 但是允许重复的元素最多为2 在上一道题目的启发下 利用last来记录生成的数组的最后一个元素的下标 curnum为当前扫描的位置的下标 num记录重复的元素的个数 if (nums[checknum] != nums[last]) { num = 1; nums[++
2016-02-25 21:57:23 341
原创 Remove Duplicates from Sorted Array
#include <vector> using namespace std;class Solution { public: int removeDuplicates(vector<int>& nums) { if (nums.size() == 0) return 0; int last, checknum;//last为不重复数组的
2016-02-25 20:43:37 307
原创 largestBSTSubtree
#include "stdafx.h" #include <iostream> #include "limits.h" using namespace std;typedef struct treenode { struct treenode *l; struct treenode *r; int value; }TreeNode,*TreeNodePtr; bool res
2016-02-24 20:41:06 373
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人