- 博客(40)
- 收藏
- 关注
转载 浅拷贝和深拷贝
#include #include class A{public: A() // 构造函数,p指向堆中分配的一空间 { m_data = new char(100); printf("默认构造函数\n"); } A(const A&r) { m_data = new char(100); memcpy(m_data, r.m_data,
2016-07-31 16:50:42 244
原创 回溯法总结
一般回溯法可以用两种框架,一种遍历方式(for循环),选择方式(可以理解成到某一节点选择或者不选)。比较二者的差别:1.采用遍历方式,for(int i=dep;i选择的方式记得判断dep是否到达边界dep==nums.size()同时记得dep++;2.遍历方式中for循环的临时变量存储的是temp[i],选择方式中是temp[dep];3.采用选择方式中,pop完之后还需要继续
2015-10-07 09:02:15 657
原创 栈的压入、弹出序列
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。class Solution {public: //主要是分析为什么不是一个出栈,先找到对应的序号,比如进栈的序列是1 7 6
2015-09-25 14:01:23 352
原创 调整该数组中数字的顺序
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。class Solution {public: //类似插入排序,找到什么地方插入,i为最右边的奇数的位置,j为i的左边偶数的位置 void reOrderArray(vector &array) { if (ar
2015-09-24 21:47:23 328
原创 间隔K翻转链表
1->2->3->4->5->6->7 k=3 return 3->2->1->6->5->4->7ListNode * reverse(ListNode *head,int k) { ListNode *pre = NULL; while (k>0) { k--; ListNode *next = head->next; head->next = pre;
2015-09-24 20:45:25 428
原创 旋转数组的最小数字
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减序列的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。两种特殊情况,未旋转(左值小于右,若旋转过肯定左大于右),或者2 2 2 1 2这种,设两个指针指向两端,然后二分,如果mid大于等于left说明,左端有序,如果mid小于等于右说明右有序。
2015-09-24 10:55:00 287
原创 leetcodes
递归/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class So
2015-09-23 22:30:42 349
翻译 快排
class Solution {public: int Partition(vector &nums, int left, int right) { // 采用子序列的第一个元素为枢纽元素 int pviont = nums[left]; while (left<right) { // 从后往前在后半部分中寻找第一个小于枢纽元素的元素 while(nums[rig
2015-09-15 15:26:28 297
原创 leetcode 链表
Dummy node 是链表问题中一个重要的技巧,中文翻译叫“哑节点”或者“假人头结点”。Dummy node 是一个虚拟节点,也可以认为是标杆节点。Dummy node 就是在链表表头 head 前加一个节点指向 head,即 dummy -> head。Dummy node 的使用多针对单链表没有前向指针的问题,保证链表的head 不会在删除操作中丢失。除此之外,还有一种用法比较少
2015-09-13 15:33:53 385
原创 leetcode Palindrome Linked List 链表
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?Show TagsShow Similar Problems题目意思是给一个链表,判断是不是回文,比如1->2->3->
2015-07-14 15:58:14 293
原创 leetcode Fraction to Recurring Decimal 哈希表
leetcode Fraction to Recurring Decimal 哈希表
2015-07-14 13:12:26 424
原创 leetcode Two Sum 哈希表
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 add up to the target, where
2015-07-06 19:12:27 310
原创 leetcode Contains Duplicate II 哈希表
Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k.
2015-07-06 17:27:09 429
原创 leetcode Flatten Binary Tree to Linked List树
leetcode Flatten Binary Tree to Linked List树
2015-07-06 16:54:27 318
原创 leetcode Power of Two位运算
Given an integer, write a function to determine if it is a power of two.Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.Show Tags
2015-07-06 11:27:41 287
原创 leetcode Populating Next Right Pointers in Each Node 树 队列应用
leetcode Populating Next Right Pointers in Each Node 树 队列应用
2015-07-06 11:05:42 336
原创 leetcode Sum Root to Leaf Numbers 树
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.Find the tota
2015-07-06 09:57:18 307
转载 Longest Substring Without Repeating Characters求最长子串中没有重复字母的最长长度
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 3. Fo
2015-07-06 09:37:39 354
原创 leetcode Convert Sorted Array to Binary Search Tree 树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.把一个有序的数组编程平衡二叉搜索树。/** * Definition for a binary tree node. * struct TreeNode { * int val; *
2015-07-05 22:58:54 287
原创 leetcode Symmetric Tree 树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the f
2015-07-05 19:47:07 218
原创 leetcode Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node./** * Definition for a binary tre
2015-07-05 17:45:46 247
原创 leetcode Balanced Binary Tree 树
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe
2015-07-05 17:10:47 250
原创 leetcode Remove Nth Node From End of List 链表 双指针
leetcode Remove Nth Node From End of List 链表 双指针
2015-07-02 10:18:34 332
原创 leetcode Minimum Size Subarray Sum 双指针
leetcode Minimum Size Subarray Sum 双指针
2015-07-02 09:48:23 452
原创 leetcode Binary Tree Zigzag Level Order Traversal 层序遍历 双队列
双队列实现二叉树的层序遍历。
2015-07-01 19:08:02 372
原创 leetcode 二叉树的遍历栈的实现 Preorder Traversal Inorder Traversal Postorder Traversal
leetcode 二叉树的遍历用栈实现 非递归
2015-06-30 21:53:47 396
原创 leetcode Basic Calculator 栈
Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and em
2015-06-30 19:31:03 298
原创 leetcode Add and Search Word - Data structure design回溯算法+Trie字典树
leetcode Add and Search Word - Data structure design回溯算法+Trie字典树
2015-06-28 11:03:14 481
原创 leetcode Generate Parentheses 回溯算法
题目: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:"((()))", "(()())", "(())()", "()(()
2015-06-26 22:04:35 693
原创 leetcode Combination Sum II回溯问题
最近一直在看回溯算法的框架,这道题代表性比较强,用到剪枝(重复1),和约束(超过界限直接return)。先贴下一般的回溯算法的框架。引用自这个bolg http://blog.jqian.net/post/backtracking.htmlvoid backtrack(int t){ if(t > n) output(x); else for (i
2015-06-26 10:06:46 330
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人