
数据结构
文章平均质量分 53
自犬邦
热爱生活,珍惜时间。
展开
-
C++零碎知识整理
unordered_map 被推荐取代hash map,其被包含在头文件中,leetcode 第一题可用。原创 2015-04-13 13:35:51 · 281 阅读 · 0 评论 -
leetcode 002 —— add two numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link原创 2015-04-13 21:08:32 · 291 阅读 · 0 评论 -
leetcode 005 —— Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.从每个位置,向两边开始扫描。原创 2015-04-16 14:28:00 · 329 阅读 · 0 评论 -
leetcode 006 —— ZigZag Conversion
xxxThe 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原创 2015-04-17 13:42:37 · 314 阅读 · 0 评论 -
leetcode 007 —— Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321class Solution {public: int reverse(int x) { bool sign = x > 0 ? false : true; /原创 2015-04-27 14:50:27 · 334 阅读 · 0 评论 -
leetcode 004 —— Median of Two Sorted Arrays
There are two sorted arrays A and B 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)).方法一:先合并数组,在排序,在搜索中间值。(繁琐)方法二:可以原创 2015-04-14 14:17:50 · 328 阅读 · 0 评论 -
leetcode 003 —— 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-04-14 10:35:11 · 319 阅读 · 0 评论 -
算法与数据结构学习 11 排序二叉树
1.节点定义struct TreeNode{ int data; TreeNode *lChild; TreeNode *rChild;};2.前序,中序,后续遍历void PreOrder(TreeNode *&p){ if (NULL == p) return; cout data << ' '; PreOrder(p->lChild); PreOrder(p->rC原创 2015-05-27 14:52:01 · 280 阅读 · 0 评论 -
算法与数据结构学习 01 基础与规范
学习数据结构与算法,总结与反思。一个有良好编程规范的代码(转)template //定义模板,使得type代表任意数据类型,但是要保持一致(同int或者同double...)int find(type array[], int length, type value){ if(NULL == array || 0 == length) //做参数合法性检测 return原创 2015-05-21 17:23:40 · 297 阅读 · 0 评论 -
算法与数据结构学习 02 循环和递归
所谓的递归就是函数自己调用自己而已,循环本质上也是一种递归。1.求和递归int calculate(int m){ if(m == 0) return 0; else return calculate(m -1) + m;}2.查找递归函数int _find(int index, int array[], int length, int value){ if(i原创 2015-05-21 21:28:32 · 270 阅读 · 0 评论 -
算法与数据结构学习 07 快速排序
快速排序int get_middle(int a[], int low,int high){ int pos = a[low]; while (low<high){ while (a[high] >= pos&&high > low) high--; //从low开始,则先从高位开始替换,至多到low+1, swap(&a[high], &a[low]); //因为此时原创 2015-05-22 21:30:55 · 307 阅读 · 0 评论 -
算法与数据结构学习 03 递归和堆栈
递归函数的调用与普通函数的调用,在堆栈的实现是一直的,牵涉到编译原理,往后再深入学习。原创 2015-05-22 14:03:16 · 313 阅读 · 0 评论 -
算法与数据结构学习 04 内存
内存中存在三种数据全局中的数据、堆中的数据、临时堆栈中的数据1.全局数据static int value = 100;void process(){ static int number = 10;}value和number的数据其实都属于全局数据,这里的变量是不随着函数的调用发生变化的。2.堆中的数据void process(){ char* point =原创 2015-05-22 14:18:17 · 280 阅读 · 0 评论 -
算法与数据结构学习 05 查找
1. 普通查找int find(int array[], int length, int value){ if(NULL == array || 0 == length) return -1; for(int index = 0; index < length; index++){ if(value == array[index]) return index;原创 2015-05-22 14:25:13 · 294 阅读 · 0 评论 -
算法与数据结构学习 06 非递归排序
1.冒泡排序void bubble_sort(int array[], int length){ int inner = 0, outer = 0; int median = 0; int flag = 1; if(NULL == array || 0 == length) return; for(outer = length-1; outer >= 1 && flag;原创 2015-05-22 20:39:15 · 378 阅读 · 0 评论 -
算法与数据结构学习 08 归并排序
总体思想,将两个有序表合成一个有序表方法一:迭代法方法二:递归法原创 2015-05-24 21:35:33 · 241 阅读 · 0 评论 -
算法与数据结构学习 10 链表操作
基础,单链表节点struct list_node{ int data; list_node *next;};1.数组到链表的转化list_node* array_to_list(int *a, int len){ list_node *p,*q; list_node *head; head = new list_node; head->data = a[0];原创 2015-05-25 21:31:17 · 350 阅读 · 0 评论 -
算法与数据结构学习 09 堆排序
一个介绍堆排序的好网站 http://www.cnblogs.com/dolphin0520/archive/2011/10/06/2199741.html#include#includeusing namespace std;void print(int a[], int n){ for (int j = 0; j<n; j++){ cout << a[j] << "原创 2015-05-25 15:50:36 · 271 阅读 · 0 评论 -
leetcode 008 —— String to Integer (atoi)
Implement atoi to convert a string to an integer.详细的转换方法在此处点击打开链接思路:使用流操作class Solution {public: int myAtoi(string str) { stringstream ss; //在vs2013中 需要设置为 strstream ss; #incl原创 2015-07-07 21:23:26 · 275 阅读 · 0 评论 -
leetcode 018 —— 4Sum (nsum)
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:Element原创 2015-07-09 15:21:02 · 403 阅读 · 0 评论 -
leetcode 010 —— Regular Expression Matching(hard)
题目:'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool is原创 2015-07-07 21:57:44 · 328 阅读 · 0 评论 -
leetcode 011 —— Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin原创 2015-07-08 19:39:44 · 315 阅读 · 0 评论 -
leetcode 012 —— Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999知识补充:I、V、 X、L、 C、D 和M,分别表示 1、5、 10、50、 100、500 和1000;思路:依次求千,百,十,个,转换成字符串原创 2015-07-08 19:53:02 · 363 阅读 · 0 评论 -
leetcode 013 —— Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.class Solution {public: int romanToInt(string s){ vector vec_ge = { "I", "II", "原创 2015-07-08 20:06:16 · 335 阅读 · 0 评论 -
leetcode 016 —— 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 exact原创 2015-07-09 10:04:48 · 268 阅读 · 0 评论 -
leetcode 015 —— 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-07-08 20:13:49 · 244 阅读 · 0 评论 -
leetcode 017 —— 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:Digit st原创 2015-07-09 10:33:20 · 558 阅读 · 0 评论 -
leetcode 019 —— 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, the原创 2015-07-09 16:58:35 · 223 阅读 · 0 评论 -
leetcode 020 —— 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-07-09 19:34:03 · 392 阅读 · 0 评论 -
leetcode 021 —— 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.思路:类似归并排序,双指针扫描链表,值小的,就往新链表后面放,最后补齐多余的链表class Solut原创 2015-07-09 19:51:57 · 293 阅读 · 0 评论 -
leetcode 022 —— 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-07-09 20:09:30 · 273 阅读 · 0 评论 -
leetcode 023 —— Merge k sorted linked 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; *原创 2015-07-13 14:24:55 · 321 阅读 · 0 评论 -
leetcode 024 —— 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-07-13 14:28:59 · 348 阅读 · 0 评论 -
leetcode 026 —— Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2015-07-13 15:51:47 · 222 阅读 · 0 评论 -
leetcode 059 —— Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [原创 2015-07-27 15:34:15 · 270 阅读 · 0 评论 -
leetcode 060 —— Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""3原创 2015-07-27 18:43:55 · 295 阅读 · 0 评论 -
leetcode 057 —— Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.E原创 2015-07-27 14:39:12 · 245 阅读 · 0 评论 -
leetcode 058 —— Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is原创 2015-07-27 15:02:40 · 224 阅读 · 0 评论 -
leetcode 028 —— Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.思路:KMP算法原创 2015-07-13 20:27:28 · 249 阅读 · 0 评论 -
leetcode 031 —— Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible原创 2015-07-14 20:48:06 · 225 阅读 · 0 评论