- 博客(19)
- 资源 (1)
- 收藏
- 关注
原创 Leetcode|Palindrome Linked List
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:翻转整个链表,存储翻转链表,从头比较。遍历两边了,空间O(n)也不行。解法2:借助栈,先找到中点,然后把前半部压入栈。
2015-07-30 23:36:09 198
原创 Leetcode|Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes’ values.For example, Given {1,2,3,4}, reorder it to {1,4,2,3}
2015-07-30 23:32:18 191
原创 Leetcode|Reverse Words in a String
Given s = “the sky is blue”, * return “blue is sky the”. 这种问题一般有三种解法: 1,One simple approach is a two-pass solution: First pass to split the string by spaces into an array of words, then second pa
2015-07-26 15:48:29 236
原创 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 N A P L S I I G
2015-07-11 16:33:36 177
原创 Leetcode|Reverse Integer(string转char*总结)
Reverse digits of an integer.Example1: x = 123, return 321 Example2: x = -123, return -321思路是转化为字符串。溢出的判断要进行字符串比较;这里string类型不能直接用strcmp;如果想用的话,需要类型转换。 下面我会说明到底怎么比较字符串。int strcmp(const char str1,
2015-07-11 10:05:52 307
原创 Leetcode|【69】Sqrt(x) |二分查找
Implement int sqrt(int x).Compute and return the square root of x.解法有两种:第一种是二分查找,注意边界上限可以从x/2+1开始查找。第二种方法是牛顿迭代法 解法1:int sqrt(int n){//二分查找,8ms int res=n/2+1; int x=0; while(x<=res){ int mid=(x+
2015-07-06 19:04:16 466
原创 Leetcode|Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.位操作,详见我的另一篇讲解位操作的博文。 此题在leetcode的通过率只有15%,主要是溢出的问题需要好好考虑。int divide(int x,int y){ asser
2015-07-06 18:59:03 285
原创 位操作的技巧
一,基本概念认知 1,为啥要用补码 计算机中的符号数有三种表示方法,即原码、反码和补码。三种表示方法均有符号位和数值位两部分,符号位都是用0表示“正”,用1表示“负”,而数值位,三种表示方法各不相同。 在计算机系统中,数值一律用补码来表示和存储。原因在于,使用补码,可以将符号位和数值域统一处理;同时,加法和减法也可以统一处理。此外,补码与原码相互转换,其运算过程是相同的,不需要额外的硬件电路。
2015-07-06 18:54:35 444
原创 Leetcode|Populating Next Right Pointers in Each Node
Given a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is no next right node,
2015-07-03 16:08:56 251
原创 Leetcode|Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 注意是高度平衡的数。所以选好每个点很关键。不用重新比较。 解法1:利用函数重载实现递归(20ms)TreeNode *sortedArrayToBST(vector<int>& nums,int sta
2015-07-03 10:58:56 184
原创 Leetcode|Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example, Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3
2015-07-03 10:24:23 252
原创 Leetcode|Candy
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.
2015-07-03 09:42:26 251
原创 Leetcode|Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, …1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off as “one 2, the
2015-07-03 08:42:15 238
原创 Leetcode|Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.Follow up: What if the BST is mod
2015-07-03 08:03:59 415
原创 Leetcode|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"].这题非常简单,但是注意最后一位的处理。最为直接的写法,有点麻烦写的。vector summ
2015-07-02 22:06:18 200
原创 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 X X O O X X X O X X O
2015-07-02 11:08:33 195
原创 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 X X O O X X X O X X O
2015-07-02 10:32:31 214
原创 Leetcode|Word Ladder
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a time Each interm
2015-07-01 22:34:58 200
原创 Leetcode|Number of Islands
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 assume
2015-07-01 22:31:22 233
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人