- 博客(25)
- 资源 (9)
- 收藏
- 关注
原创 默认类的方法实现
String类中系统默认实现了4个函数,分别是构造函数、析构函数、拷贝构造函数和赋值运算符重载,但是在程序中我们常常自己要去实现,下面就是实现的代码:在使用String的过程中,如果拷贝构造函数和赋值函数采用了系统设计的话,会出现错误,因为系统只是实现了浅拷贝,也就是只用了指针赋值的形式,这样往往在程序设计的过程中出现错误。class String{public: String(
2016-09-25 01:01:59 748
原创 malloc、free、printf、scanf函数原型
malloc和free这两个和内存相关的函数都在头文件stdlib.h中,所以在应用的时候,需要包含该文件:#include1、mallocvoid* malloc(size_t size);malloc向系统申请分配制定size个字节的内存空间。返回类型是void*类型。在C或者C++中,void*类型可以强制转换为任何其他类型的指针。2、freevoid fre
2016-09-25 00:54:14 3154
原创 内存拷贝函数memcpy函数深入剖析
memcpy(拷贝内存内容) 定义函数:void * memcpy( void * dest, const void *src, size_t n );memcpy()用来拷贝src所指的内存内容前n个字节到dest所指的内存地址上。与strcpy()不同的是,memcpy()会完整的复制n个字节,不会因为遇到字符串结束'/0'而结束。memcpy()函数可以拷贝任意类型的数据。memc
2016-09-25 00:31:28 9649
原创 单链表递归反转和非递归翻转
今晚抽时间复习了链表,整理了递归版本和非递归版本的单链表翻转,欢迎指出错误。1、递归版本调用:ListNode * head = NULL;reverseRec(root, head);//head即为反转后得链表头实现如下:void reverseRec(ListNode *head = reverse(head);*root, ListNode *&head){ if (
2016-09-21 23:19:09 435
原创 First Unique Character in a String
一、问题描述Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.Examples:s = "leetcode"return 0.s = "loveleetcode",return 2.
2016-09-11 16:32:46 223
原创 Linked List Random Node
一、问题描述Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen.Follow up:What if the linked list is extremely
2016-09-11 13:34:13 193
原创 Ransom Note
一、问题描述 Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can
2016-09-11 01:02:17 248
原创 搜狐2017笔试编程题
1、求二叉树的最大子树和,节点有正有负。int getSum(TreeNode * root, long &result){ if(root == NULL) return 0; long sum = root -> val; if(root -> left != NULL || root -> right != NULL){ long l = getSum(root -> lef
2016-09-08 10:10:52 1039
原创 Number of Islands
一、问题描述Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
2016-09-06 21:41:55 202
原创 京东2017校招笔试编程题2:进制转换、辗转相除法
如下一些内容大部分来自于维基百科:辗转相除法,又被称为欧几里德(Euclidean)算法, 是求最大公约数的算法。辗转相除法首次出现于欧几里得的《几何原本》(第VII卷,命题i和ii)中,而在中国则可以追溯至东汉出现的《九章算术》。两个数的最大公约数是指能同时整除它们的最大正整数。辗转相除法的基本原理是:两个数的最大公约数等于它们中较小的数和两数之差的最大公约数。例如,252和10
2016-09-06 10:32:25 1678
原创 Find Minimum in Rotated Sorted Array II
一、问题描述Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at
2016-09-05 10:17:12 210
原创 First Missing Positive
一、问题描述Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and u
2016-09-04 12:47:23 176
原创 Find Peak Element
一、问题描述A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multipl
2016-09-04 11:23:20 175
原创 Binary Search Tree Iterato
一、问题描述Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.
2016-09-04 00:55:00 236
原创 Reverse Words in a String
一、问题描述Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".二、思路首先我们跳过字符前的空格,然后简单判断字符串是否都为空格,接着将当前字符串翻转,并且在翻转后加上空格;然
2016-09-03 23:49:35 176
原创 Two Sum II - Input array is sorted
一、问题描述Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of th
2016-09-02 22:47:45 190
原创 Maximum Product Subarray
一、问题描述Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has
2016-09-02 20:45:19 227
原创 Group Anagrams
一、问题描述Given an array of strings, group anagrams together.For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]二、
2016-09-02 19:17:39 211
原创 Subsets
一、问题描述Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example,If nums = [1,2,3], a solution is:[
2016-09-02 16:28:39 188
原创 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)).Example 1:
2016-09-02 11:45:54 181
原创 Rotate Image
一、问题描述You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?二、思路矩阵旋转90度。原理如下:先上下翻转,再交换对角元素即可。 *
2016-09-02 10:57:18 161
原创 Find Minimum in Rotated Sorted Array
一、问题描述Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicat
2016-09-02 00:44:35 201
原创 Binary Tree Maximum Path Sum
一、问题描述Given a binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child conne
2016-09-01 15:16:54 207
原创 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.F
2016-09-01 10:59:57 173
原创 Flatten Binary Tree to Linked List
一、问题描述Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look lik
2016-09-01 00:19:45 206
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人