- 博客(34)
- 收藏
- 关注
原创 30.leetcode题目104: 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.分析:要深刻理解树的结构定义是一个递归的定义,
2016-03-30 13:18:51 255
原创 29.leetcode题目258: Add Digits
题目:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2
2016-03-30 12:56:56 297
原创 28.leetcode题目292: Nim Game
题目:You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone wi
2016-03-30 12:24:10 453
原创 27.leetcode题目137: Single Number II(考察位操作)
题目:Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it wi
2016-03-29 23:24:08 280
原创 26.leetcode题目136: 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
2016-03-29 16:27:28 202
原创 25.leetcode题目190: Reverse Bits(Follow up未解决)
题目:Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as0
2016-03-27 19:50:58 477
原创 24.leetcode题目20: Valid Parentheses
前言:这道题目在《数据结构》这本书上有讲,学习真的很重要啊!题目:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correc
2016-03-23 22:38:56 222
原创 23.leetcode题目203: 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分析:方法一:用一个新指针Li
2016-03-17 20:58:54 194
原创 22.leetcode题目206: Reverse Linked List
题目:Reverse a singly linked list.click to show more hints.Hint:A linked list can be reversed either iteratively or recursively. Could you implement both?分析:翻转链表。根据提示:方法一,依次翻转c
2016-03-16 21:04:16 251
原创 21.leetcode题目234: Palindrome Linked List(第2种方法是链表翻转,等做完206题再做!)
题目:Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?分析:方法1:将链表里的数据都提出来放在vector里,然后循环vector作比较,判断是否是回文。这是一种很直接的方法。链表结束的时
2016-03-16 19:58:37 256
原创 20.leetcode题目67: Add Binary
题目:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".分析:二进制加法,用数组或者字符串class Solution {public: string addBinary(string
2016-03-16 19:33:32 256
原创 19.leetcode题目303: Range Sum Query - Immutable
题目:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) ->
2016-03-15 19:06:35 187
原创 18.leetcode题目28: Implement strStr()
题目:Implement 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 {publi
2016-03-15 19:00:07 182
原创 4.线程和进程
关于进程和线程描述正确的是()正确答案: A B D 你的答案: A B C D (错误)线程不拥有系统资源,但可以访问隶属于进程的资源在创建或销毁进程时,系统开销明显大于创建或销毁线程时开销进程是调度和拥有资源的基本单位不仅进程可以并发执行,同一个进程的多个线程之间也可以并发执行线程作为调度和分配的基本单位,进程
2016-03-15 15:51:13 406
原创 3.Linux中fork()函数
一个进程,包括代码、数据和分配给进程的资源。fork()函数通过系统调用创建一个与原来进程(父进程)几乎完全相同的进程(子进程),这两个进程共享代码空间,但数据空间是独立的,子进程数据空间中的内容是父进程的完整拷贝,指令指针也完全相同,但只有一点不同,fork调用的一个奇妙之处就是它仅仅被调用一次,却能够返回两次,如果fork成功,子进程中fork的返回值是0,父进程中fork的返回值是子进程的进
2016-03-15 15:12:35 450
原创 2.堆和栈
注::基本是网上查找的答案heap堆:是由malloc和new之类函数分配的空间所在地,由程序员分配和释放,若程序员不释放,程序结束时可能由os回收。地址是由低向高增长的。heap的空间是很大的自由区。 stack栈:是自动分配变量,以及函数调用的时候所使用的一些空间,存放函数的参数值,局部变量值等。地址是由高向低减少的。 stack空间有限。全局区(静态区)(static):
2016-03-14 19:39:10 230
原创 17.leetcode题目204: Count Primes(不是最快的!)
前言:这道题目一开始觉得挺简单的,编写一个判断每一个数是否是质数的函数isPrime: bool isPrime(int m){ if(m<=1){ return false; } for(int i=2;i<m/2;i++){ //根据 hint 2修改 for(int i=2;i<=sqrt(m);i++){ //根据 hint 3修改 if(m%i==0){ return false; }
2016-03-14 16:32:34 218
原创 1.学习1: 埃拉托色尼筛选法(求一定范围内的质数)
前言:这是在刷leetcode第204题: Count Primes时hint提示的。埃拉托色尼选筛法(the Sieve of Eratosthenes)简称埃氏筛法,是古希腊数学家埃拉托色尼(Eratosthenes 274B.C.~194B.C.)提出的一种筛选法。 是针对自然数列中的自然数而实施的,用于求一定范围内的质数,它的容斥原理之完备性条件是p=H~。
2016-03-14 15:21:41 1389
原创 16.leetcode题目228: Summary Ranges
题目:Given a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].分析:第一个问题,为什么所有测试用例都没有判断降序的情况?题目只是说已经排序的数组而已,
2016-03-14 12:30:23 208
原创 15.leetcode题目125: 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
2016-03-14 10:08:18 334
原创 14.leetcode题目278: First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the
2016-03-12 17:37:45 341
原创 13.leetcode题目1: Two Sum
又是好几天没刷题了,这几天零零碎碎的面试。。。Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution
2016-03-12 16:20:54 337
原创 12.leetcode题目12: Integer to Roman
罗马字母:I(1) V(5) X(10) L(50) C(100) D(500) M(1000)class Solution {public: string intToRoman(int num) { string s=""; vector digit={"","I","II","III","IV","V"
2016-03-08 17:38:37 199
原创 11.leetcode题目171: Excel Sheet Column Number
对应168题:数字转成表题本题:实质上就是26进制数转换class Solution {public: int titleToNumber(string s) { if(s=="")//非if(s==NULL),NULL是针对字符的 return 0; int res=0; for(int i=0;i!=
2016-03-08 10:17:53 166
原创 10.leetcode题目168: Excel Sheet Column Title
昨晚走之前看的题目。想法1:class Solution {public: string convertToTitle(int n) { if(n==0) return NULL; string title=""; while(n){ char c=char((n-1)%26+'A');
2016-03-08 09:23:51 157
原创 9.leetcode题目189: 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].class Solution {public:
2016-03-07 22:22:29 208
原创 8.leetcode题目165: Compare Version Numbers
题目:Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the version strings are non-empty
2016-03-04 15:14:34 226
原创 7.leetcode题目19: Remove Nth Node From End of List
从单链表中删除倒数第n个节点。题目: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 secon
2016-03-04 14:29:27 201
原创 6.leetcode题目14: Longest Common Prefix
最开始没搞懂什么叫最长公共前缀,后来明白就是最长的相同字符数我最开始自己做的:class Solution {public: string longestCommonPrefix(vector& strs) { if(strs.empty()) return ""; string ref=strs[0];
2016-03-03 22:27:04 218
原创 5.leetcode题目13: Roman to Integer
题目:罗马数字-》整形数字规则:1.罗马字母只有I V X L C D M 对应整数 1 5 10 50 100 500 1000 2.同一罗马字母连写不超过三次; 3.大的罗马字母在左小的罗马字母在右时:+
2016-03-03 20:26:59 217
原创 4.leetcode题目9:Palindrome Number
这道题目不难,但是要屡清楚还是蛮有点复杂的首先,负数不是回数,0是回数,要单独考虑这两种情况。方法:比较整数的头和尾,相等则循环,不相等则return false;取头尾的办法是: int n=1; while(x/n>=10){ n=n*10; }//这算是一种比较好的表示方法了,速度快我想到的方法是:
2016-03-03 18:11:11 222
原创 3.leetcode题目8:String to Integer (atoi)
这道题目理解起来有点费劲,反正我理解了很久都没懂。。。题目要求:将字符串转换为整数输出1.空字符串时返回0;2.丢弃前面的空白字符直到第一个非空白字符;3.判断第一个字符是否为“+”或“-”,记录(也有可能第一个字符就是数字);4.对后面的字符进行判断,若为数字则记录,否则,终止转换;5.考虑转换后的结果是否溢出,溢出则输出相应符号的边界值。过程中遇到了很多错误
2016-03-03 15:12:18 333
原创 2.leetcode题7:Reverse Integer
这是昨天晚上离开实验室的时候看的一道题,晚上回寝室思考了很久,当然也查了一些资料,比如INT_MAX和INT_MIN的使用是查资料才知道的,这道题要注意边界条件,其实题目已经提示了,要注意翻转后是否溢出,或输入0时候的处理,return 0;
2016-03-03 08:56:43 249
原创 1.leetcode题6:zigzag
第一次刷题,拿着题目完全没有思路,不知如何下手,在网络上百度了别人的答案,把题理解后自己再动手写的总结一点:一定要先思考!经过数学分析,设计大概思路,然后再动手码代码我的目标是:easy-medium思路:设置N个新的字符串(用到了动态分配内存new),把每一行的字符放进对应的新字符串,最后把N个字符串连接起来。运行时间28ms(不快)注意点:注意边界条件(字符
2016-03-02 22:38:29 218
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人