
c++
文章平均质量分 60
Sissi_cici
这个作者很懒,什么都没留下…
展开
-
Lining up(uva 270)
第一次做oj,用c++,选了lining up问题,总结一下。本题目的即找出一条直线,使得其穿过的点数最多。故可依次遍历每一点,假定其在该直线上;计算其与其他点连线的斜率,找出相同斜率值的个数(此处可用到排序),即得到从这点出发直线的最大穿过点数;最后取这些点数中的最大值作为该case的结果。ps:做题过程中查的一点资料1、sort()(1)默认的sort函数是按升原创 2014-01-18 16:19:36 · 1374 阅读 · 1 评论 -
指针和引用的区别
总结一下指针和引用的相同点和不同点:★相同点:●都是地址的概念;指针指向一块内存,它的内容是所指内存的地址;而引用则是某块内存的别名。★不同点:●指针是一个实体,而引用仅是个别名;●引用只能在定义时被初始化一次,之后不可变;指针可变;引用"从一而终",指针可以"见异思迁";●引用没有const,指针有const,const的指针不可变;(具体转载 2015-03-12 20:14:30 · 616 阅读 · 0 评论 -
螺旋矩阵2——顺时针打印任意大小的矩阵
#includeusing namespace std;void PrintCircle(int **matrix,int start,int row,int column){ int endX=column-1-start; int endY=row-1-start; for(int i=start;i<=endX;i++){ cout<<matrix[start][i]<<'原创 2015-03-22 11:39:41 · 1009 阅读 · 0 评论 -
leetcode_num148_Sort list
链表的归并排序特别注意取中值函数的书写/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {原创 2015-03-30 19:57:09 · 831 阅读 · 0 评论 -
leetcode_num155_Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get原创 2015-03-30 16:24:40 · 751 阅读 · 0 评论 -
层序遍历及其进阶版
输入为:abd##eh###cfi##j##g##1、普通层序遍历:输出为一行2、进阶版1:输出每一层,从左向右依次输出3、进阶版2:S型输出每一层,即从右向左和从左向右交替输出#include#includeusing namespace std;templatestruct BiNode { T data; BiNod原创 2015-03-18 18:50:49 · 991 阅读 · 1 评论 -
leetcode_num165_Compare Version numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.class Solution {public: int compareVersion(string ver原创 2015-03-18 23:41:20 · 605 阅读 · 0 评论 -
leetcode_num75_sort colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers原创 2015-04-01 11:01:01 · 907 阅读 · 0 评论 -
leetcode_num179_Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.两两比较 可以利用sort函数来排序,自定义comp原创 2015-04-01 19:22:55 · 753 阅读 · 0 评论 -
leetcode_num179_Insertion Sort list
Sort a linked list using insertion sort.举例子真是写对代码的好方法!/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NU原创 2015-04-01 21:54:16 · 792 阅读 · 0 评论 -
leetcode_num98_Validate Binary Search Tree
判断是否为二叉搜索树定义上下确界/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} *原创 2015-04-01 22:40:18 · 732 阅读 · 0 评论 -
leetcode_num95_Unique BT II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3原创 2015-04-04 17:17:02 · 891 阅读 · 0 评论 -
排序算法总结(三)逆序对
求数组中的逆序对#includeusing namespace std;int MergeArray(int r[],int s,int m,int e,int temp[]){ int i=m,j=e,count=0,k=0; while(i>=s&&j>=m+1){ if (r[i]>r[j]){ temp[k]=r[i]; k++; count=count+原创 2015-03-12 10:57:51 · 1420 阅读 · 0 评论 -
排序算法总结(二)归并法
递归法#includeusing namespace std;void Merge(int r[],int r1[],int b,int m,int e){ int i=b; int j=m+1; int k=b; while((i<=m)&&(j<=e)){ if(r[i]<=r[j]){ r1[k]=r[i]; i++; k++;} else{ r1[k原创 2015-03-12 10:56:53 · 985 阅读 · 0 评论 -
排序算法总结(一)
#includeusing namespace std;void Insert(int r[],int n){ for(int i=2;i<=n;i++){ if(r[i]<r[i-1]){ r[0]=r[i]; int j=i-1; for(;r[0]<r[j];j--){ r[j+1]=r[j]; } r[j+1]=r[0];//游标指向j原创 2015-03-12 10:52:27 · 614 阅读 · 0 评论 -
Slash Maze(uva 705)
本题的亮点是dfs和‘\’ ‘/’的转化方法。我采用了比较普遍和简单的方法,使用九宫格,这样由于斜着走可以变成横着走和竖着走的组合形式,所以在移步遍历时只用考虑上下左右四个方向。编程步骤可分为三步:(1)输入:通过键盘输入‘\’ ‘/’,将‘\’转化为100 010 001 ,‘/’ 转化为 001 010 100,构造map数组。(2)dfs:判断移动一步之后,是否超过了边界,由此筛原创 2014-01-22 17:47:05 · 890 阅读 · 0 评论 -
Palindrome(uva 401)
比较简单的字符串问题。通过练习,重温了一下读入字符串的方法,测量字符串的长度。下面是代码,注释的部分为我不明白的地方,为什么这样一一对应的写出来,却一直是WA!!! 求指点,不过我也承认存成两个数组比较简洁。#include#include#include#includeusing namespace std;char b;char m[25];const char one[]原创 2014-01-29 17:01:48 · 658 阅读 · 0 评论 -
Tree(uva 548)
本题的思路还是比较简单的,利用中序和后序遍历构造出二叉树,然后深度优先遍历,选出最小权重值路径,输出其叶子节点。是一道练习二叉树的好题。参考了一些大牛的代码,并作了一些修改。#include#include#includeusing namespace std;int in[10001];//inorderint pos[10001];//postorderstruct Tre原创 2014-02-13 20:10:14 · 693 阅读 · 0 评论 -
leetcode_num112_Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum./** * Definition for binary tree * struct Tree原创 2015-03-08 17:04:09 · 839 阅读 · 0 评论 -
leetcode_num190_Reverse Bits
Reverse bits of a given 32 bits unsigned integer.归并法class Solution {public: uint32_t reverseBits(uint32_t n) { n=(n>>16)|(n<<16); n=((n&0xff00ff00)>>8)|((n&0x00ff00ff)<<8);原创 2015-03-08 17:56:50 · 1041 阅读 · 0 评论 -
leetcode_num6_Zigzag
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I原创 2015-03-27 18:23:38 · 742 阅读 · 0 评论 -
leetcode_num169_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-03-09 21:17:27 · 738 阅读 · 0 评论 -
leetcode_num168&171_excel title&number
Given a positive integer, return its corresponding column title as appear in an Excel sheet.class Solution {public: string convertToTitle(int n) { string s; while(n){原创 2015-03-09 22:59:51 · 849 阅读 · 0 评论 -
螺旋矩阵——正逆序
输入数字N,输出大小为N*N的螺旋矩阵#includeusing namespace std;void Spiral_Matrix1(int N,int **r){//正序输出 int k=N;int sum=1; while(k>0){ int m=(N-k)/2; int i=m,j=m; if(k==1){ //奇数情况下 r[m][m]=sum; br原创 2015-03-17 16:27:56 · 1369 阅读 · 2 评论 -
atoi函数编写
#include#includeusing namespace std;int ai(const char *p){ bool negflag = false; int rs = 0; if(*p=='+'||*p=='-'){ negflag=(*p=='-'); p++; } while(isdigit(*p)){ rs=rs*10+(*p-'0'); p++;原创 2015-03-11 21:05:41 · 966 阅读 · 0 评论 -
char *a 与char a[] 的区别
char *a = "hello" 中的a是指向第一个字符‘a'的一个指针char a[20] = "hello" 中数组名a也是执行数组第一个字符‘h’的指针但二者并不相同:看实例:把两个字符串相加:结果:对比:结果:把字符串加到指针所指的字串上去,出现段错误,本质原因:*d="0123456789"存放在常转载 2015-03-10 21:00:53 · 646 阅读 · 0 评论 -
leetcode_num101_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-04-04 22:49:58 · 830 阅读 · 0 评论