- 博客(36)
- 资源 (9)
- 收藏
- 关注
原创 leetcode 两个有序数组合成
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additio
2015-08-15 16:09:18 1010 1
原创 leetcode 判断两棵树是否一致Same Tree
思路:递归判断即可: bool isSameTree(struct TreeNode* p, struct TreeNode* q) { if(p==NULL&&q==NULL)return 1; if(p==NULL||q==NULL)return 0; if(p->val!=q->val)return 0; else { return isSameTree(p->le
2015-08-15 13:55:21 983
原创 leetcode 二叉树层次遍历输出Binary Tree Level Order Traversal
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},
2015-08-15 10:07:51 1834
原创 leetcode 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 differ
2015-08-13 20:42:19 756
原创 leetcode Minimum&&Max Depth of Binary Tree 求树的最小&&最大深度
求树的最小深度采用递归思想,分别求得左右子树最小深度:/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ int minget(int x,
2015-08-13 17:30:37 448
原创 leetcode 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.For example:Given the below binary tree and sum
2015-08-13 17:14:37 355
原创 leetcdoe 帕斯卡尔三角形2 Pascal's Triangle II
帕斯卡尔三角形:[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].
2015-08-13 14:37:15 438
原创 leetcode 字符串匹配Implement strStr()
mplement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.判断字符串needle是否在字符串haystack中,若在在返回第一次出现的下标。class Solution {public:int s
2015-08-12 13:47:12 534
原创 leetcode Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.给定一数组,删除与给定元素相同的元素并返回新数组长度:对于给定条件删除满足条件的数组元素问题,都可以采用:先统计满足条件的个数k;然后将数组前移k位:int removeElement
2015-08-12 10:56:00 274
原创 leetcode 排序数组去重复并返回新数组长度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 c
2015-08-12 10:50:23 585
原创 leetcode 判断有效字符串是回文串Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a pa
2015-08-12 09:58:09 1154
原创 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.即将两个以排好序的链表按照顺序合并成一个链表并返回:/** * Definition for singl
2015-08-11 21:17:42 351
原创 leetcode 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 valid bu
2015-08-10 10:29:52 369
原创 leetcode Longest Common Prefix不同字符串的公共前缀
思路:当任意2个字符串的最长公共前缀,其长度肯定不会超过最短的字符串的长度,设最短的字符串长度为n,那么只要比较这2个字符串的前n个字符即可。如此得出这2个字符串的最长公共前缀prefix后,再拿prefix作为新的字符串和数组中的下一个字符串比较,方法同上。需要注意的是,如果数组中的某个字符串长度为0,或者求得的当前最长公共前缀的长度为0,就直接返回空字串。class Solut
2015-08-09 17:51:53 556
原创 leetcode 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() -- Ge
2015-08-07 21:31:30 535
原创 leetcode Roman to Integer罗马数字与阿拉伯数字互转
罗马数字规则:1, 罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。罗马数字中没有“0”。2, 重复次数:一个罗马数字最多重复3次。3, 右加左减:在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。4, 左减的数字有限制,仅限于I、X、C
2015-08-07 19:07:33 448
原创 leetcode Intersection of Two Linked Lists 两链表是否相交
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘
2015-08-07 14:42:40 640
原创 C/C++中sscanf && sprintf()
sscanf() - 从一个字符串中读进与指定格式相符的数据:函数原型: int sscanf( string str, string fmt, mixed var1, mixed var2 ... ); int scanf( const char *format [,argument]... );与scanf类似都是用于输入,只不过scanf用于从屏幕(stdio)输入,
2015-08-06 19:40:02 1530 1
原创 leetcode Compare Version Numbers版本号比较
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 You may assume that the version strings are non-empty and contain only digits and the . character.T
2015-08-06 17:10:31 392
原创 leetcode 删除单链表指定元素
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node p.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value
2015-08-05 18:01:22 1060
原创 leetcode Valid Anagram 异位体
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.一开始简单想法就是先将两个字符串排序,最后比
2015-08-05 17:23:35 406
原创 leetcode Pascal's Triangle 帕斯卡尔三角形输出
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]]帕斯卡三角形,是一个三角形矩阵,其顶端是 1,视为(ro
2015-08-05 16:46:58 925
原创 leetcode 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 th
2015-08-05 15:45:01 419
原创 leetcode excel表格列标题数字与字符串的转换
1 数字转换为字符: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB int getindex(int l){if(l==1)return 1;else return (getindex(l-1)+pow(26.0,(l-1)));}//获取字符数为l的第一个字符
2015-08-04 21:57:17 751
原创 leetcode Majority Element 寻找出现次数最多元素
给定一个大小n的数组,求出现次数大于⌊ n/2 ⌋的元素:假定数组总是存在而且这样的元素总是存在:int com(const void *s1,const void *s2){int *p1=(int*)s1;int *p2=(int *)s2;return *p1-*p2;}int majorityElement(int* nums, int numsSize) { qsort(
2015-08-04 19:30:58 859
原创 leetcode Factorial Trailing Zeroes非负整数阶乘后尾0个数
非负整数阶乘后尾0个数,只有2和5相乘才会出现0,其中整十也可以看做是2和5相乘的结果,所以,可以在n之前看看有多少个2以及多少个5就行了,又发现2的数量一定多于5的个数,于是我们只看n前面有多少个5就行了,于是n/5就得到了5的个数,还有一点要注意的就是25这种,5和5相乘的结果,所以,还要看n/5里面有多少个5,也就相当于看n里面有多少个25,还有125,625.即:如n=32,n
2015-08-04 15:25:21 379
原创 Leetccode String to Integer (atoi) 字符串转数字
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input case
2015-08-04 11:34:03 399
原创 Leetcode Reverse Integer 反转整数
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321一开始没考虑溢出情况:int reverse(int x) { long sum=0; while(x){ sum*=10; sum+=x%10; x=x/10;} return
2015-08-03 21:04:27 364
原创 leetcode Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].即给定移动数k,向右循环移动k位:最简单想到的就是借助一个临时数组存储循环移动k位后的数:void
2015-08-03 17:28:23 566
原创 leetcode Hamming weight汉明权重计算
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 0000000000
2015-08-02 20:20:31 1518
原创 leetcode 抢房子House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house
2015-08-02 16:37:00 394
原创 Leetcode 快乐数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 o
2015-08-02 15:13:52 2936
原创 Leetcode 单链表删除Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5/** * Definition for si
2015-08-02 14:27:10 380
原创 计算素数个数 Count Primes
题意为给定整数n,求解n以内的整数中有多少个素数(质数)1.最朴素的做法是:先从2到n-1判断是不是素数,每个数字i都用2到i-1的数字去除,有一个可以整除它,就不是质数。bool isPrime(int x){if(x<=1)return false;for(int i=2;i<x;i++) if(x%i==0)return false;return true;}int
2015-08-02 10:04:56 1801
原创 Leetcode Isomorphic Strings
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
2015-08-01 18:43:10 354
原创 Leetcode ZigZag Conversion
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
2015-08-01 14:36:52 474
clang_library
2015-11-28
tags_vim_master
2015-11-28
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人