
Coding-Leetcode等
故常无-欲以观其妙
行胜于言
展开
-
23-Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Examples:Given “abcabcbb”, the answer is “abc”, which the length is 3.Given “bbbbb”, the answer is “b”, with the le原创 2016-04-26 13:35:32 · 4392 阅读 · 0 评论 -
24-Longest Palindromic Substring-Leetcode
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. 这里给出一种AC的动态规划解法:时间和空间复杂度O原创 2016-04-27 18:18:07 · 4384 阅读 · 0 评论 -
25-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原创 2016-04-28 10:21:36 · 4999 阅读 · 0 评论 -
26-Palindrome Number
回文数的判定,不使用额外的空间 Determine whether an integer is a palindrome. Do this without extra space.思路:将一个整数逆转,判定和原来的数相不相等 注意负数一定不是回文数#define IMAX numeric_limits<int>::max()#define IMIN numeric_limits<int>::m原创 2016-04-28 12:11:55 · 5008 阅读 · 0 评论 -
27-Roman to Integer-Leetcode
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.思路:先构造罗马与数字的映射,接着对给定的字符串从头开始进行最长匹配,最大为4,依次进行,匹配完成则乘以相应位的量级,时间复杂度为O(n)#include <algorithm>#incl原创 2016-04-28 13:21:21 · 4977 阅读 · 0 评论 -
28-Merge Two Sorted Lists
easy 21. 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. 思路:利用map映射,值->指针集合,然后按值从原创 2016-04-28 13:31:00 · 5056 阅读 · 0 评论 -
2015百度之星之-IP聚合
IP聚合 Accepts: 138 Submissions: 293 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊想知道在原创 2016-05-09 21:06:52 · 1194 阅读 · 0 评论 -
65-Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order Traversal My Submissions QuestionEditorial Solution Total Accepted: 60410 Total Submissions: 210160 Difficulty: Medium Given a binary tree, return the zigzag level or原创 2016-05-10 21:13:17 · 304 阅读 · 0 评论 -
66-Reorder List
Reorder List My Submissions QuestionEditorial Solution Total Accepted: 64392 Total Submissions: 281830 Difficulty: Medium Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-原创 2016-05-10 22:35:27 · 631 阅读 · 0 评论 -
29-Regular Expression Matching-leetcode
‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be: bool isMatch(cons原创 2016-04-28 17:48:16 · 5175 阅读 · 0 评论 -
30-Container With Most Water-Leetcode
Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lin原创 2016-04-28 19:23:53 · 5215 阅读 · 0 评论 -
81-Next Permutation
-31. Next Permutation My Submissions QuestionEditorial Solution Total Accepted: 66747 Total Submissions: 250247 Difficulty: Medium Implement next permutation, which rearranges numbers into the lexico原创 2016-05-23 23:45:55 · 247 阅读 · 0 评论 -
82-Rotate List
-61. Rotate List My Submissions QuestionEditorial Solution Total Accepted: 69769 Total Submissions: 303483 Difficulty: Medium Given a list, rotate the list to the right by k places, where k is non-ne原创 2016-05-24 09:32:23 · 316 阅读 · 0 评论 -
83-Add Binary
-67. Add Binary My Submissions QuestionEditorial Solution Total Accepted: 85904 Total Submissions: 309911 Difficulty: Easy Given two binary strings, return their sum (also a binary string).For exampl原创 2016-05-24 10:16:33 · 603 阅读 · 0 评论 -
3.Median of Two Sorted Arrays Leetcode
#include <algorithm>#include <iostream>#include <sstream>#include <fstream>#include <iomanip>#include <cstdlib>#include <cstring>#include <vector>#include <string>#include <bitset>#include <c原创 2016-04-15 23:28:56 · 3425 阅读 · 0 评论 -
2016腾讯春招软件研发实习笔试
第一道题:求有删除情况的最长回文子串题目: 解题思路:这个题严格意义上来说,删除了字符就谈不上回文串了,既然有删除,那估计考察的不是回文串,而是其他的,但是这个东西又有回文串的特点,细想一下——那就是不连续的回文串,想到不连续,就容易使人想到最长公共子序列,把源字符串逆序之后对比两个字符串发现:我靠,这不就是求两个序列的最长公共子序列(好像跟回文串没多大关系)。转载 2016-04-16 10:22:37 · 4328 阅读 · 0 评论 -
深入理解动态规划DP
通过最近对于一些算法题的思考,越来越发现动态规划方法的在时间上高效性,往往该问题可以轻松的找到暴力破解的方法,其时间复杂度却不尽人意。下面来看看几个常见的动态规划思路的经典问题例一.有一段楼梯有10级台阶,规定每一步只能跨一级或两级,要登上第10级台阶有几种不同的走法?(腾讯电面题之一) 其状态转移方程为: f(n):表示n阶楼梯有多少种走法f(n):表示n阶楼梯有多少种走法f(n)=f原创 2016-04-16 12:38:22 · 22636 阅读 · 0 评论 -
67-Gray Code
Gray Code My Submissions QuestionEditorial Solution Total Accepted: 60277 Total Submissions: 165212 Difficulty: Medium The gray code is a binary numeral system where two successive values differ in原创 2016-05-11 12:12:22 · 325 阅读 · 0 评论 -
68-Binary Tree Postorder Traversal
Binary Tree Postorder Traversal My Submissions QuestionEditorial Solution Total Accepted: 97358 Total Submissions: 273744 Difficulty: Hard Given a binary tree, return the postorder traversal of its原创 2016-05-11 16:33:24 · 286 阅读 · 0 评论 -
5.Maximum Product Subarray-Leetcode
f(j+1)为以下标j结尾的连续子序列最大乘积值(1) 状态转移方程如何表示呢: 这里我们知道A[j]可能为正数(或0)或负数,那么当A[j]为正数,期望前j个乘积为正数,若为负数,则期望前面的为负数。故我们需定义两个函数来确定我们的状态转移方程: fmax(j+1)=max(max(fmax(j)∗A[j],A[j]),fmin(j)∗A[j]) fmin(j+1)=min(min(fmin原创 2016-04-16 22:18:46 · 3686 阅读 · 0 评论 -
4.Reverse Words in a String-Leetcode
class Solution {public: void reverseWords(string &s) { for(string::size_type ix=0;ix!=s.size();++ix) { if(s[ix]==' ') if(s[ix+1]==' '){原创 2016-04-15 23:34:01 · 3295 阅读 · 0 评论 -
7. Minimum Depth of Binary Tree-LeetCode
难度系数:easy/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2016-04-16 22:58:51 · 3313 阅读 · 0 评论 -
6. Reverse Linked List 逆转单链表
逆转单链表,比较简单,不细讲,扫描依次改变指针指向。class Solution {public: ListNode* reverseList(ListNode* head) { if(head==nullptr)return head; ListNode * tmp = head->next; ListNode *prenode = hea原创 2016-04-16 22:40:58 · 4157 阅读 · 0 评论 -
8.Maximum Depth of Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class原创 2016-04-16 23:07:19 · 3370 阅读 · 0 评论 -
31-Longest Common Prefix
Longest Common Prefix My Submissions Difficulty: Easy Write a function to find the longest common prefix string amongst an array of strings.难度:easy 思路:求解所有字符串的最长前缀 首先拿第一个字符串作为标准,从第一个字符串开始检测,如果其原创 2016-04-28 20:20:40 · 5349 阅读 · 0 评论 -
16-4SUM
思路:先保存两个整数的和,然后两层循环搞定 平均时间复杂度:O(n2)O(n^2) 还是time limit,期待更好的解法。。。。class Solution {public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int> > result;原创 2016-04-19 23:20:09 · 3539 阅读 · 0 评论 -
32-3Sum
3Sum My Submissions QuestionEditorial Solution Total Accepted: 115154 Total Submissions: 612489 Difficulty: Medium Given an array S of n integers, are there elements a, b, c in S such that a + b +原创 2016-04-30 12:44:37 · 10848 阅读 · 0 评论 -
69-Contains Duplicate
Contains Duplicate My Submissions QuestionEditorial Solution Total Accepted: 88863 Total Submissions: 214375 Difficulty: Easy Given an array of integers, find if the array contains any duplicates.原创 2016-05-12 11:59:20 · 319 阅读 · 0 评论 -
84. Permutations
–46. Permutations My Submissions QuestionEditorial Solution Total Accepted: 101087 Total Submissions: 280282 Difficulty: Medium Given a collection of distinct numbers, return all possible permutation原创 2016-05-25 09:58:06 · 360 阅读 · 0 评论 -
85-Permutations II
class Solution {public: vector<vector<int>> permute(vector<int>& nums) { //int n=nums.size(); vector<vector<int>> res; sort(nums.begin(),nums.end()); do{原创 2016-05-25 11:59:18 · 250 阅读 · 0 评论 -
33. Implement strStr()
Implement strStr() My Submissions QuestionEditorial Solution Total Accepted: 104556 Total Submissions: 420107 Difficulty: Easy Implement strStr().Returns the index of the first occurrence of needl原创 2016-04-30 23:08:33 · 3734 阅读 · 0 评论 -
34. Swap Nodes in Pairs
Swap Nodes in Pairs My Submissions QuestionEditorial Solution Total Accepted: 95230 Total Submissions: 270562 Difficulty: Easy Given a linked list, swap every two adjacent nodes and return its head原创 2016-04-30 23:27:07 · 3776 阅读 · 0 评论 -
35-Remove Element
Remove Element My Submissions QuestionEditorial Solution Total Accepted: 115367 Total Submissions: 340963 Difficulty: Easy Given an array and a value, remove all instances of that value in place an原创 2016-04-30 23:39:37 · 3737 阅读 · 0 评论 -
36-Same Tree
Same Tree My Submissions QuestionEditorial Solution Total Accepted: 126116 Total Submissions: 291884 Difficulty: Easy Given two binary trees, write a function to check if they are equal or not.Two原创 2016-04-30 23:45:11 · 3668 阅读 · 0 评论 -
37-Invert Binary Tree
Invert Binary Tree My Submissions QuestionEditorial Solution Total Accepted: 87818 Total Submissions: 193396 Difficulty: Easy Invert a binary tree.4 / \ 2 7 / \ / \ 1 3 6 9 to 4原创 2016-04-30 23:52:59 · 3652 阅读 · 0 评论 -
38- Majority Element
Majority Element My Submissions QuestionEditorial Solution Total Accepted: 110538 Total Submissions: 268290 Difficulty: Easy Given an array of size n, find the majority element. The majority elemen原创 2016-05-01 00:01:48 · 3713 阅读 · 0 评论 -
39-Remove Duplicates from Sorted Array
Remove Duplicates from Sorted Array My Submissions QuestionEditorial Solution Total Accepted: 127836 Total Submissions: 381794 Difficulty: Easy Given a sorted array, remove the duplicates in place原创 2016-05-01 10:33:38 · 3559 阅读 · 0 评论 -
40-3Sum Closest
3Sum Closest My Submissions QuestionEditorial Solution Total Accepted: 76185 Total Submissions: 262100 Difficulty: Medium Given an array S of n integers, find three integers in S such that the sum原创 2016-05-01 11:08:18 · 3643 阅读 · 0 评论 -
41-Climbing Stairs-leetcode
Climbing Stairs My Submissions QuestionEditorial Solution Total Accepted: 106498 Total Submissions: 290003 Difficulty: Easy You are climbing a stair case. It takes n steps to reach to the top.Each原创 2016-05-01 12:30:42 · 3421 阅读 · 0 评论 -
42-Remove Nth Node From End of List
Remove Nth Node From End of List My Submissions QuestionEditorial Solution Total Accepted: 106592 Total Submissions: 361392 Difficulty: Easy Given a linked list, remove the nth node from the end of原创 2016-05-01 13:38:45 · 3482 阅读 · 0 评论