- 博客(25)
- 收藏
- 关注
原创 Palindrome Linked List leetcode 234
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?判断给定的链表是不是回文的:把要判断的链表分成两部分,将第二部分逆置,逐个与前一部分的val相比较,若不同返回false/** * Definition for sin
2015-07-21 09:31:36
467
原创 Lowest Common Ancestor of a Binary Tree leetcode236
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and
2015-07-13 19:40:44
387
原创 Lowest Common Ancestor of a Binary Search Tree leetcode235
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two
2015-07-11 15:44:03
299
原创 Isomorphic Strings leetcode 205
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 another chara
2015-07-02 22:25:48
318
原创 Longest Common Prefix leetcode 14
Write a function to find the longest common prefix string amongst an array of strings. 找所有字符串的公共前缀,简单地实现题class Solution {public: string strPrefix(string s1,string s2){ if(s1.length()==0||s
2015-06-15 18:14:44
305
原创 Clone Graph Leetcode 133
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ’s undirected graph serialization:Nodes are labeled uniquely. We use # as a separator for each node, an
2015-06-14 22:50:11
321
原创 Distinct Subsequences leetcode 115
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of
2015-06-09 17:55:26
339
原创 Multiply Strings leetcode 43
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.经典的大数相乘问题: 1,从给定字符串最后一位开始操作,要弄清楚整个乘法的过程以及各进位的
2015-06-03 23:48:49
462
原创 Edit Distance Leetcode 72
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) In
2015-06-02 23:31:41
369
原创 Simplify Path Leetcode71
Given an absolute path for a file (Unix-style), simplify it.For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c”click to show corner cases. Corner Cases:Did you consider the ca
2015-05-31 00:52:17
470
原创 Minimum Path Sum leetcode 64
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any
2015-05-28 19:21:46
366
原创 Binary Tree Inorder Traversal leetCode11
Given a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2].Note: Recursive solution is trivial,
2015-05-27 19:16:31
283
原创 Longest Substring Without Repeating Characters leetcode3
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “
2015-05-24 01:30:48
233
原创 Longest Palindromic Substring leetcode 5
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.求字符串的最长回文串1,从字符串第一个字符开始遍历,向
2015-05-23 13:19:56
301
原创 ZigZag Conversion leetcode6
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
2015-05-18 00:22:19
253
原创 Reverse Linked List II Leetcode92
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the followi
2015-05-17 01:50:06
295
原创 Binary Tree Postorder Traversal leetcode145
Given a binary tree, return the postorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3},1 \ 2 / 3return [3,2,1]. Note: Recursive solution is trivial, cou
2015-05-14 17:27:31
443
原创 Binary Tree Preorder Traversal leetcode144
Given a binary tree, return the preorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3].Note: Recursive solution is trivial,
2015-05-10 21:41:13
317
原创 Reverse bits leetcode190
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110
2015-05-09 23:17:54
328
原创 happy number leetcode202
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 of i
2015-05-09 22:50:37
383
原创 Reverse Linked List LeetCode206
reverse a singly linked list 题目比较简单,就是反转一个链表.要求用iteratively (迭代)和recursively(递归)解决/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNo
2015-05-07 14:35:48
325
原创 字符串查找算法kmp
这样一个问题:给定一个文本串s,和一个匹配串p,要求查找p第一次在s中出现的位置。常见的方法如暴力搜素,逐个匹配s[i],p[j],若匹配,下标后移。不匹配,i回溯到这次匹配前的下一位置,j置为0,重新匹配。最坏情况,时间复杂度O(n*m)。int violenceSearch(char *s,char *p){ int i=0,j=0; int lenp=strl
2015-05-03 21:22:51
691
原创 字符串反转问题
一,第一类的字符串反转问题,也就是输入This is a string.,输出为.gnirts a si sihT,整个字符串反转:void reverse1(char *a){ int len=strlen(a)-1; int i=0; while(i<len){ char ch=a[i]; a[i++]=a[len]; a[
2015-05-02 21:27:06
426
原创 HDOJ-1002大数相加
参考这篇:点击打开链接 Problem DescriptionI have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B. InputThe first line of the input contains
2015-04-26 01:08:12
400
原创 栈的经典问题:括号配对
Parentheses BalanceYou are given a string consisting of parentheses() and []. A string of this type is said to be correct:(a)if it is the empty string(b)if A and B are correct,
2015-01-08 21:48:09
358
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人