C++
zhihua_bupt
极客码农,Coding the life,Coding the world!!!
展开
-
C++ delete错误
1. 指针通过 new 或 new[] ,向系统“申请”得到一段内存空间,这段内存空间必须在不需要将它释放了。int* p = new int[100]; int girl[100]; p = girl; delete [] p; 灾难在 delete [] p 时发生。我们原意是要释放p最初通过new int[100]而得到的内存空间,但事转载 2015-06-01 15:53:42 · 1158 阅读 · 1 评论 -
_CRT_SECURE_NO_WARNINGS
Warning 1 warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.在编程过程中难免转载 2015-06-26 09:41:25 · 739 阅读 · 0 评论 -
“支付宝”角度看“两个元素互换的中间变量”
void swap(int &a, int &b){ int t ; //中间变量t就像网购中支付宝的作用 t = a; //买家a将钱先打到支付宝t中 a = b; //卖家b把货发给买家a b = t; //买家a收到货并确认收货后,支付宝t将钱打给卖家b}原创 2015-06-04 21:56:16 · 864 阅读 · 1 评论 -
汉诺塔问题
汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具。大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。汉诺塔问题:古代有一个梵塔,塔内有三个柱子A、B、C,A柱子上有若干个盘子,盘子大的在下,小的在上,目标是要将原创 2015-09-02 09:17:17 · 3155 阅读 · 0 评论 -
栈转置与栈排序
1. 问题描述(1) 用递归颠倒一个栈。例如输入栈{1, 2, 3, 4, 5},1在栈顶。颠倒之后的栈为{5, 4, 3, 2, 1},5处在栈顶。(2) 用递归方法对栈进行排序。2. 解决方案这两个问题主要是考察应聘者对递归的理解。递归程序有两个关键因素:递归定义和递归终止条件。(1) 问题1经过分析后,很容易得到该问题的递归定义和递归终转载 2015-09-04 11:01:04 · 1148 阅读 · 0 评论 -
用递归颠倒一个栈
题目:用递归颠倒一个栈。例如输入栈{1, 2, 3, 4, 5},1在栈顶。颠倒之后的栈为{5, 4, 3, 2, 1},5处在栈顶。分析:乍一看到这道题目,第一反应是把栈里的所有元素逐一pop出来,放到一个数组里,然后在数组里颠倒所有元素,最后把数组中的所有元素逐一push进入栈。这时栈也就颠倒过来了。颠倒一个数组是一件很容易的事情。不过这种思路需要显示分配一个长度为O(n)的数组,转载 2015-09-06 08:49:02 · 1766 阅读 · 0 评论 -
栈转置的实现
栈转置:一个栈依次压入0,1,2,3,4,5,从栈顶到栈底分别为5,4,3,2,1,0。将这个栈转置后,从栈顶到栈底依次为0,1,2,3,4,5,也就是实现栈中元素的逆序,只能用递归函数来实现,而不能用其他的数据结构。#include#include;using namespace std;//将栈顶元素移动至栈底void move_top_to_bottom(std::stack&原创 2015-09-06 09:21:31 · 1323 阅读 · 0 评论 -
HOG+SVM
#include "cv.h"#include "highgui.h"#include "stdafx.h"#include #include #include #include #include using namespace cv;using namespace std;int main(int argc, char** argv) { vector img_转载 2015-09-16 15:33:39 · 838 阅读 · 0 评论 -
STL vector用法介绍
介绍这篇文章的目的是为了介绍std::vector,如何恰当地使用它们的成员函数等操作。本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用。通过阅读这篇文章读者应该能够有效地使用vector容器,而且应该不会再去使用C类型的动态数组了。 Vector总览vector是C++标准模板库中的部分内容,它是一个多功能转载 2015-10-04 15:52:14 · 385 阅读 · 0 评论 -
LeetCode16:3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exa原创 2015-11-06 21:39:15 · 538 阅读 · 0 评论 -
LeetCode1: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, whe原创 2015-10-07 16:49:40 · 741 阅读 · 0 评论 -
LeetCode17:Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digi原创 2015-11-08 15:03:16 · 526 阅读 · 0 评论 -
LeetCode18:4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:原创 2015-11-08 15:44:58 · 617 阅读 · 0 评论 -
LeetCode9:Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.判断一个整数是否为回文,解决思路很简单,就是不断取整数的第一位和最后一位进行比较,相等则继续取第二位和倒数第二位,直到整数的所有位都比较完成或者中途找到了不一致的位。#include#includeusing namespace std;原创 2015-10-24 23:23:08 · 693 阅读 · 0 评论 -
LeetCode10:Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input原创 2015-10-26 10:43:30 · 750 阅读 · 0 评论 -
vs2010 问题 >LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
VS2010编译过程中出现问题:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏解决方法如下:项目\属性\配置属性\清单工具\输入和输出\嵌入清单:原来是“是”,改成“否”。说明:这种方法每个工程均需要修改配置。原创 2015-11-12 17:12:15 · 622 阅读 · 0 评论 -
MATLAB和C/C++混合编程实现图像处理(一)
MATLAB具有丰富的图像处理函数库,运算速度慢,特别是在多重循环的情况下,不适合直接应用于工程当中。如果能把MATLAB和另一种适合工程的编程语言结合到一起运用到数字图像处理领域,则会更加方便的进行图像处理,MATLAB和C/C++的混合编程,既继承了MATLAB的优点,又拥有了C/C++运算速度快、适合工程应用的特点。一、MATLAB引擎与运行环境配置1.MATLAB引擎MATLA原创 2015-11-13 22:22:46 · 4828 阅读 · 0 评论 -
LeetCode19:Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example,Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, th原创 2015-11-14 00:24:39 · 615 阅读 · 0 评论 -
LeetCode12:Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.#include#include#includeusing namespace std;class Solution {public: string int原创 2015-10-29 22:27:05 · 570 阅读 · 0 评论 -
LeetCode13:Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.#include#include#include#includeusing namespace std;class Solution {public: in原创 2015-10-29 23:17:09 · 588 阅读 · 0 评论 -
LeetCode14:Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.要求找出字符串数组中所有字符串的最长公共前缀。解题思路:两个字符串的最长公共前缀,其长度肯定不会超过最短的字符串的长度,设最短的字符串长度为m,那么只要比较这两个字符串的前m个字符即可。每次得出两个字符串的最长公共前原创 2015-10-30 22:20:33 · 561 阅读 · 0 评论 -
LeetCode23:Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity./*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;原创 2015-11-23 12:27:16 · 700 阅读 · 0 评论 -
LeetCode 169:Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element原创 2015-12-10 23:47:39 · 1113 阅读 · 0 评论 -
LeetCode 70:Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?//设f(n)表示爬n阶楼梯的不同方法,有两种选择:/原创 2015-12-11 16:34:34 · 926 阅读 · 0 评论 -
LeetCode20:Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all va原创 2015-11-18 22:21:13 · 720 阅读 · 0 评论 -
LeetCode 191:Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 000000原创 2015-12-11 09:38:56 · 970 阅读 · 0 评论 -
LeetCode 206:Reverse Linked List
Reverse a singly linked list.一、题目描述 翻转单链表二、解题思路利用两个结点指针和一个中间结点指针temp(用来记录当前结点的下一个节点的位置),分别指向当前结点和前一个结点,每次循环让当前结点的指针域指向前一个结点即可,翻转结束后,记得将最后一个节点的链域置为空。class Solution {public: List原创 2015-12-11 11:21:42 · 670 阅读 · 0 评论 -
LeetCode15:3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c原创 2015-11-02 11:03:51 · 605 阅读 · 0 评论 -
LeetCode 235:Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw原创 2015-12-11 00:31:53 · 1343 阅读 · 0 评论 -
LeetCode 83:Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.Subscribe to see wh原创 2015-12-11 17:11:56 · 1441 阅读 · 0 评论 -
LeetCode 22: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-11-19 15:45:10 · 539 阅读 · 0 评论 -
LeetCode 263:Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly sinc原创 2015-12-12 23:20:24 · 2877 阅读 · 0 评论 -
LeetCode21:Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.//Definition for singly-linked list. struct ListNo原创 2015-11-19 00:42:10 · 586 阅读 · 0 评论 -
LeetCode 24:Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y原创 2015-11-25 23:54:57 · 678 阅读 · 0 评论 -
LeetCode 232:Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.em原创 2015-12-13 19:31:59 · 1902 阅读 · 0 评论 -
LeetCode 231:Power of Two
Given an integer, write a function to determine if it is a power of two.//题目要求:求一个数是否是2的幂次方//解题方法://方法一:如果某个值是2的幂次方所得,其对应二进制则是最高位为1,其余位为0.//n-1则相反,除了最高位,其余比特位均为1,则我们可以判断n&(n-1)是否等于0来判断n是否是2的幂原创 2015-12-13 20:11:40 · 1732 阅读 · 2 评论 -
LeetCode 99:Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devis原创 2015-12-28 15:37:36 · 3434 阅读 · 0 评论 -
LeetCode 111: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.//递归方法求二叉树的最小深度 //注意判断树的深度应该到叶子节点原创 2015-12-28 16:16:40 · 1465 阅读 · 0 评论 -
LeetCode 202:Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2015-12-13 00:45:52 · 1344 阅读 · 0 评论 -
LeetCode 110: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-12-14 22:39:54 · 2437 阅读 · 2 评论