![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 50
hzj379805931
这个作者很懒,什么都没留下…
展开
-
49.leetcode题目18. 4Sum
题目: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:原创 2016-05-30 14:36:38 · 311 阅读 · 0 评论 -
40.leetcode题目:
这两天在刷leetcode上的动态规划题目,昨天大概刷了三题,但是没有记录博客,因为下周要做报告,所以最近比较忙,故过段时间再添上。我还是希望自己能够坚持下去,仿佛这就意味着我人生的一大步——坚持!!今天的题目有:96:unique binary search tree309. Best Time to Buy and Sell Stock with Cooldown这原创 2016-04-06 13:19:42 · 264 阅读 · 0 评论 -
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 · 214 阅读 · 0 评论 -
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 · 333 阅读 · 0 评论 -
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 · 333 阅读 · 0 评论 -
39.leetcode题目121、 122、 123
121题:动态规划Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share原创 2016-04-02 21:14:05 · 334 阅读 · 0 评论 -
38.leetcode题目231:231. Power of Two
题目:Given an integer, write a function to determine if it is a power of two.分析:判断是否是2的整数次幂。class Solution {public: bool isPowerOfTwo(int n) { if(n<=0) return false;原创 2016-04-02 16:23:56 · 230 阅读 · 0 评论 -
37.leetcode题目191: Number of 1 Bits
题目: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 representati原创 2016-04-02 15:51:30 · 265 阅读 · 0 评论 -
36.leetcode题目338: Counting Bits(再做一遍)
题目:Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:Fo原创 2016-04-02 15:24:18 · 247 阅读 · 0 评论 -
35.leetcode题目100: 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.分析:原创 2016-04-02 14:02:21 · 228 阅读 · 0 评论 -
34.leetcode题目260: Single Number III
题目:Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.For example:原创 2016-04-02 12:26:38 · 210 阅读 · 0 评论 -
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 · 324 阅读 · 0 评论 -
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 · 201 阅读 · 0 评论 -
46.leetcode题目:94. Binary Tree Inorder Traversal
题目:Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursiv原创 2016-04-19 18:15:06 · 245 阅读 · 0 评论 -
47.leetcode题目2. Add Two Numbers
class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode *result=NULL,*next,*temp;//ListNode *result,*next,*temp;未通过编译 ListNode *l11=l1,*l22=l2;原创 2016-05-03 14:29:32 · 242 阅读 · 0 评论 -
45.leetcode题目:144. Binary Tree Preorder Traversal
题目:Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursi原创 2016-04-17 18:42:54 · 223 阅读 · 0 评论 -
44.leetcode题目:268. Missing Number(位操作没想到)
题目:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algori原创 2016-04-17 17:12:00 · 334 阅读 · 0 评论 -
43.leetcode题目:319. Bulb Switcher
题目;There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or tur原创 2016-04-17 16:48:13 · 260 阅读 · 0 评论 -
48.leetcode题目17. 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 (jus原创 2016-05-24 22:44:28 · 337 阅读 · 0 评论 -
42.leetcode题目:169. 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原创 2016-04-15 13:38:36 · 308 阅读 · 0 评论 -
41.leetcode题目217. Contains Duplicate(哈希方法还没做)
题目: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 ele原创 2016-04-14 21:13:37 · 238 阅读 · 0 评论 -
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 · 469 阅读 · 0 评论 -
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 · 210 阅读 · 0 评论 -
33.leetcode题目237: Delete Node in a Linked List
题目:Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node w原创 2016-04-02 11:05:26 · 198 阅读 · 0 评论 -
32.leetcode题目283: Move Zeroes
题目:Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after ca原创 2016-04-01 14:41:44 · 201 阅读 · 0 评论 -
31.leetcode题目226: Invert Binary Tree
题目:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1分析:递归调用class Solution {public: TreeNode* invertTree(Tre原创 2016-04-01 13:52:45 · 192 阅读 · 0 评论 -
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 · 242 阅读 · 0 评论 -
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 · 247 阅读 · 0 评论 -
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 · 196 阅读 · 0 评论 -
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 · 208 阅读 · 0 评论 -
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 · 207 阅读 · 0 评论 -
4.leetcode题目9:Palindrome Number
这道题目不难,但是要屡清楚还是蛮有点复杂的首先,负数不是回数,0是回数,要单独考虑这两种情况。方法:比较整数的头和尾,相等则循环,不相等则return false;取头尾的办法是: int n=1; while(x/n>=10){ n=n*10; }//这算是一种比较好的表示方法了,速度快我想到的方法是:原创 2016-03-03 18:11:11 · 217 阅读 · 0 评论 -
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 · 180 阅读 · 0 评论 -
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 · 174 阅读 · 0 评论 -
3.leetcode题目8:String to Integer (atoi)
这道题目理解起来有点费劲,反正我理解了很久都没懂。。。题目要求:将字符串转换为整数输出1.空字符串时返回0;2.丢弃前面的空白字符直到第一个非空白字符;3.判断第一个字符是否为“+”或“-”,记录(也有可能第一个字符就是数字);4.对后面的字符进行判断,若为数字则记录,否则,终止转换;5.考虑转换后的结果是否溢出,溢出则输出相应符号的边界值。过程中遇到了很多错误原创 2016-03-03 15:12:18 · 327 阅读 · 0 评论 -
2.leetcode题7:Reverse Integer
这是昨天晚上离开实验室的时候看的一道题,晚上回寝室思考了很久,当然也查了一些资料,比如INT_MAX和INT_MIN的使用是查资料才知道的,这道题要注意边界条件,其实题目已经提示了,要注意翻转后是否溢出,或输入0时候的处理,return 0;原创 2016-03-03 08:56:43 · 241 阅读 · 0 评论 -
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 · 194 阅读 · 0 评论 -
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 · 224 阅读 · 0 评论 -
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 · 192 阅读 · 0 评论 -
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 · 157 阅读 · 0 评论