
oj-leetcode
文章平均质量分 54
Sissi_cici
这个作者很懒,什么都没留下…
展开
-
Leetcode_num4_Reverse Integer
题目:Reverse digits of an integer.Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!If the inte原创 2014-09-18 18:09:28 · 1045 阅读 · 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_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_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 评论 -
leetcode_num118_Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.class Solution {public: vector > generate(int numRows) { vector> rs; if (numRows<=0){ return rs;原创 2015-03-10 20:58:56 · 830 阅读 · 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 评论 -
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_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_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_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 评论 -
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_num20_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-03-04 16:57:22 · 757 阅读 · 0 评论 -
Leetcode_num3_Same Tree
题目: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.刷题打卡中原创 2014-09-18 15:56:43 · 961 阅读 · 0 评论 -
Leetcode_num5_Best Time to Buy and Sell Stock II
题目:Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i原创 2014-09-21 00:02:42 · 1152 阅读 · 0 评论 -
Leetcode_num7_Linked List Cycle
题目:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?这是一道关于链表比较简单的题,很顺利就解决了,不多说啦,上代码啦# Definition for singly-linked list.#原创 2014-09-22 20:35:45 · 734 阅读 · 0 评论 -
Leetcode_num6_Unique Binary Search Trees
题目: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原创 2014-09-22 17:07:55 · 895 阅读 · 0 评论 -
Leetcode_num8_Binary Tree Preorder Traversal
题目:Given a binary tree, return the preorder traversal of its nodes' values.此题即为二叉树的前序遍历,递归的方法很简单:先节点再左子树再右子树;迭代的方法可以利用栈存储来完成遍历过程。补充递归与迭代的区别:许多问题是以递归的形式进行解释的,这只是因为它比非递归形式更为清晰。但是,这些问题的迭代往往比递归实现效率更原创 2014-09-24 09:46:24 · 945 阅读 · 0 评论 -
Leetcode_num9_Binary Tree Inorder Traversal
同num8一样,此题考查的是二叉树的中序遍历,即先左子树再节点再右子树、使用迭代法时,采用将节点和左子树均压入栈的方法,当左子树为NULL时,将top节点弹出,并存入结果列表,将next指针指向该节点的右节点代码如下:/** * Definition for binary tree * struct TreeNode { * int val; * Tre原创 2014-09-24 16:27:03 · 831 阅读 · 0 评论 -
Leetcode_num10_Populating Next Right Pointers in Each Node
题目:Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.Initially, all next pointers are set to NULL.You may原创 2014-09-24 20:10:05 · 823 阅读 · 0 评论 -
Leetcode_num12_Search Insert Position
题目:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in t原创 2014-09-25 18:59:29 · 897 阅读 · 0 评论 -
Leetcode_num1_Single Number
好久没有做题啦,从今天开始刷Leetcode的题,希望坚持的时间能长一点。先从ac率最高的Single Number开始吧。题目:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should hav原创 2014-09-17 19:00:14 · 987 阅读 · 0 评论 -
Leetcode_num11_Remove Duplicates from Sorted List
题目:Given a sorted linked list, delete all duplicates such that each element appear only once.这道链表的题也比较简单,目标是为了去重,只是需要注意在去重的过程中需要使用while循环,使一个节点的下一个节点必须指向与其不同的节点上代码咯# Definition for singly-li原创 2014-09-25 17:15:09 · 893 阅读 · 0 评论 -
Leetcode_num13_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?很简单的思路,因为一次可以走1~2步,所以原创 2014-09-28 21:38:38 · 783 阅读 · 0 评论 -
Leetcode_num14_Roman to Integer
题目:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.题目意思很简单,即将一个罗马数字的字符串,转化为整数。首先我们需要对罗马数字有一个基本的认识,由于题目已将数字大小限定在1~3999,所以我们只需考虑原创 2014-10-10 11:22:29 · 950 阅读 · 0 评论 -
Leetcode_num15_Maximun Subarray
题目: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]原创 2014-10-12 00:39:43 · 1089 阅读 · 0 评论 -
Leetcode_num2_Maximum Depth of Binary Tree
题目:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.AC率第二高的题啦,二项树的最长路径。初看此题原创 2014-09-17 20:15:50 · 1234 阅读 · 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 评论