数据结构和算法
文章平均质量分 57
jamesqinwhu
这个作者很懒,什么都没留下…
展开
-
leetcode_Number of Islands_medium--dfs
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. You may assu原创 2016-02-22 11:13:59 · 370 阅读 · 1 评论 -
leetcode_Contains Duplicate II_easy
Given an array of integers and an integer k, find out whether there there are two distinct indicesi and j in the array such that nums[i] = nums[j] and the difference betweeni and j is at most k.原创 2015-06-01 09:47:14 · 558 阅读 · 0 评论 -
leetcode_Contains Duplicate_easy
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is原创 2015-06-01 09:17:51 · 562 阅读 · 0 评论 -
leetcode_题解_path sum 2
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2014-11-19 10:15:30 · 427 阅读 · 0 评论 -
leetcode_Majority Element (找出现次数大于一半的元素)-easy
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-02 23:30:00 · 445 阅读 · 0 评论 -
leetcode_length of last word_easy
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-01-21 19:42:57 · 356 阅读 · 0 评论 -
leetcode_Roman to Integer_easy_可以看看方法
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.主要弄清楚罗马数字的表示方法:I - 1II- 2III-3IV-4V - 5X - 10L - 50原创 2015-01-21 21:37:20 · 453 阅读 · 0 评论 -
leetcode_Longest Common Prefix_easy
Write a function to find the longest common prefix string amongst an array of strings.直接进行扫描即可。class Solution {public: string longestCommonPrefix(vector &strs) { int findFlag=0,po原创 2015-01-21 20:53:52 · 415 阅读 · 0 评论 -
leetcode_Sum Root to Leaf Numbers _DFS_中等难度
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-02-06 21:36:04 · 393 阅读 · 0 评论 -
leetcode_Permutations II _hard_暴力枚举法--回溯法
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].原创 2015-02-04 20:02:18 · 426 阅读 · 0 评论 -
leetcode_Number of 1 Bits_easy
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 00000000原创 2015-06-02 20:10:38 · 669 阅读 · 0 评论 -
leetcode_Reverse Bits_easy
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as0011100101原创 2015-06-02 20:18:52 · 482 阅读 · 0 评论 -
leetcode_Isomorphic Strings _easy
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with anot原创 2015-06-03 09:53:11 · 568 阅读 · 0 评论 -
leetcode_Maximum Product Subarray _medium(最大子数组之积)
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 the larges原创 2015-09-05 22:52:15 · 379 阅读 · 0 评论 -
leetcode_Maximum Subarray _medium(最大子数组的和)
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] ha原创 2015-09-05 10:58:30 · 876 阅读 · 0 评论 -
leetcode_Unique Binary Search Trees_easy
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \原创 2015-09-05 09:51:05 · 363 阅读 · 0 评论 -
用栈实现队列-用队列实现栈
用栈实现队列leetcode :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原创 2015-08-02 15:40:23 · 782 阅读 · 0 评论 -
leetcode_Power of Two_easy
Given an integer, write a function to determine if it is a power of two.题目意思:判断某个数是否是2的幂。方法:直接进行bit运算,判断是否这个数二进制位里有且仅有一个1。class Solution {public: bool isPowerOfTwo(int n) { int c=0;原创 2015-07-31 00:06:02 · 527 阅读 · 0 评论 -
leetcode_Valid Anagram_easy
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:You may a原创 2015-08-08 20:56:50 · 470 阅读 · 0 评论 -
leetcode_Factorial Trailing Zeroes_easy
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.方法:找1...n中每个数因式分解中,5的个数(5的个数一般大于2的个数),即为10的个数注意:题目要求时间复杂度:原创 2015-06-09 15:38:23 · 533 阅读 · 0 评论 -
leetcode_Rectangle Area _easy
Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.Assume that the tota原创 2015-06-24 19:39:15 · 485 阅读 · 0 评论 -
leetcode_same tree_easy
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.直接递归调用判断就好原创 2015-01-14 23:13:11 · 364 阅读 · 0 评论 -
leetcode_Binary Tree Level Order Traversal _easy
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20原创 2015-02-01 21:53:36 · 2559 阅读 · 0 评论 -
八皇后问题--递归调用
参考了算法竞赛入门经典那本书p125:#include const int n=4;int tol=0;int arr[n];//输出某个解void print_res(int arr[],int n){ for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(arr[i]==j) printf(原创 2015-01-31 12:00:46 · 379 阅读 · 0 评论 -
leetcode_题解_Evaluate Reverse Polish Notation逆波兰式
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1",原创 2014-10-31 14:10:07 · 538 阅读 · 0 评论 -
leetcode_题解_single number_小技巧
Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using ext原创 2014-11-18 17:09:42 · 321 阅读 · 0 评论 -
leetcode_题解_Pascal's Triangle II _easy
class Solution {public: vector getRow(int rowIndex) { vector ivCur; if(rowIndex<0) return ivCur; else { vector ivPre; for(int r原创 2014-10-29 09:37:02 · 504 阅读 · 0 评论 -
leetcode_题解_Pascal's Triangle_easy
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]原创 2014-10-29 09:15:59 · 534 阅读 · 0 评论 -
leetcode_解题_reorder list
没有利用多余的存储空间,将链表分成两个子链表,然后对一个进行原创 2014-10-12 14:29:07 · 323 阅读 · 0 评论 -
leetcode_题解_Clone Graph
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separator for each原创 2014-10-27 10:04:08 · 461 阅读 · 0 评论 -
leetcode_题解_Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.思路:原创 2014-10-26 15:17:43 · 370 阅读 · 0 评论 -
leetcode_题解_Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region .For example,X X X XX O O XX X O原创 2014-10-26 12:51:41 · 491 阅读 · 0 评论 -
leetcode_题解_Merge Sorted Array _简单题
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from原创 2014-11-11 22:14:30 · 417 阅读 · 0 评论 -
leetcode_题解_path sum_easy
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.For example:Given the below binary tree and sum原创 2014-11-19 09:15:43 · 383 阅读 · 0 评论 -
leetcode_题解_Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width o原创 2014-11-05 10:18:36 · 386 阅读 · 0 评论 -
数据结构--线性表顺序实现
主要参考严老师那边教材上的实现的:#include #include #define LIST_INIT_SIZE 100//线性表存储空间初始分配量#define LISTINCREMENT 10//空间不够时,增量分配#define ElemType inttypedef struct{ ElemType *elem;//当前分配的存储空间基址 int length;/原创 2015-01-06 13:49:01 · 366 阅读 · 0 评论 -
leetcode_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.题目就是合并两个有序的链表,具体方法就是:直接操作两个链表的各个指针域,使得最后两个链表连成一个有原创 2015-01-31 10:36:35 · 403 阅读 · 0 评论 -
leetcode_Valid Sudoku and Sudoku Solver (数独游戏) _easy
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially fille原创 2015-01-31 15:48:09 · 401 阅读 · 0 评论 -
leetcode_题解_Palindrome Number _easy_回文数(空间O(1))
题目:判断是否是回文数(空间要求不能用额外的)注意:负数不是回文数class Solution {public: bool isPalindrome(int x) { if(x<0) return false; int tmp=x,res=0; while(tmp) {原创 2015-01-10 13:53:42 · 441 阅读 · 0 评论 -
leetcode_Symmetric Tree_判断二叉树镜像对称_easy_方法
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2015-01-28 16:09:33 · 573 阅读 · 0 评论