随笔分类 -  Algorithm

上一页 1 ··· 6 7 8 9 10 11 12 13 14 下一页

[Leetcode] 3Sum
摘要:Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:Elemen... 阅读全文

posted @ 2014-04-17 17:22 Eason Liu 阅读(150) 评论(0) 推荐(0)

[Leetcode] 3Sum Closest
摘要:Given an arraySofnintegers, find three integers inSsuch that the sum is closest to a given number, target. Return the sum of the three integers. You m... 阅读全文

posted @ 2014-04-14 00:13 Eason Liu 阅读(240) 评论(0) 推荐(0)

[Leetcode] Reverse Nodes in k-Group
摘要:Given a linked list, reverse the nodes of a linked listkat a time and return its modified list.If the number of nodes is not a multiple ofkthen left-o... 阅读全文

posted @ 2014-04-13 23:52 Eason Liu 阅读(233) 评论(0) 推荐(0)

[Leetcode] Sudoku Solver
摘要:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character'.'.You may assume that there will be on... 阅读全文

posted @ 2014-04-13 22:25 Eason Liu 阅读(1430) 评论(0) 推荐(0)

[Leetcode] Valid Sudoku
摘要:Determine if a Sudoku is valid, according to:Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with ... 阅读全文

posted @ 2014-04-13 20:22 Eason Liu 阅读(901) 评论(0) 推荐(0)

[Leetcode] Implement strStr()
摘要:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack.KMP算法! 1 class Solution { ... 阅读全文

posted @ 2014-04-12 18:19 Eason Liu 阅读(7049) 评论(7) 推荐(0)

[Leetcode] Distinct Subsequences
摘要:Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.K可能会大于链表的长度,坑爹! 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListN 阅读全文

posted @ 2014-04-11 17:36 Eason Liu 阅读(165) 评论(0) 推荐(0)

[Leetcode] Restore IP Addresses
摘要:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order does not matter)DFS! 1 class Solution { 2 public: 3 bool isVail(string s) { 4 if ( 阅读全文

posted @ 2014-04-11 17:23 Eason Liu 阅读(238) 评论(0) 推荐(0)

[Leetcode] Substring with Concatenation of All Words
摘要:You are given a string,S, and a list of words,L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.For example, given:S:"barfoothefoobarman"L:["foo", " 阅读全文

posted @ 2014-04-11 17:02 Eason Liu 阅读(538) 评论(0) 推荐(0)

[Leetcode] Palindrome Partitioning II
摘要:Given a strings, partitionssuch that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning ofs... 阅读全文

posted @ 2014-04-11 01:24 Eason Liu 阅读(519) 评论(0) 推荐(0)

[Leetcode] Palindrome Partitioning
摘要:Given a strings, partitionssuch that every substring of the partition is a palindrome.Return all possible palindrome partitioning ofs.For example, givens="aab",Return [ ["aa","b"], ["a","a","b"] ]像这样列举所有结果的只能DFS了。不过为什么我老是忘记写++low跟--high呢,都死 阅读全文

posted @ 2014-04-10 23:52 Eason Liu 阅读(186) 评论(0) 推荐(0)

[Jobdu] 题目1493:公约数
摘要:题目描述:给定两个正整数a,b(1 2 using namespace std; 3 4 int gcd(int a, int b) { 5 int m = a > b ? a : b; 6 int n = a > b ? b : a; 7 return n == 0 ? a : gcd(n, m % n); 8 } 9 10 int main() {11 int a, b;12 int max;13 int count;14 int i;15 while (cin >> a >> b) {16 count ... 阅读全文

posted @ 2014-04-10 20:38 Eason Liu 阅读(249) 评论(0) 推荐(0)

[Leetcode] Merge k Sorted Lists
摘要:Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.no comment. 1 /** 2 * Definition for singly-linked li... 阅读全文

posted @ 2014-04-10 18:01 Eason Liu 阅读(210) 评论(0) 推荐(0)

[Leetcode] Gray Code
摘要:he gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.For example, givenn= 2, return[0,1,3,2]. Its gray code se 阅读全文

posted @ 2014-04-10 17:32 Eason Liu 阅读(2064) 评论(0) 推荐(0)

[Leetcode] Roman to Integer
摘要:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.纯模拟,规则见:Integer to Roman。 1 class Solution { 2 public: 3 map roman; 4 void init() { 5 roman['I'] = 1; 6 roman['V'] = 5; 7 roman['X'] = 10; 8 roman['L'] = ... 阅读全文

posted @ 2014-04-10 17:18 Eason Liu 阅读(158) 评论(0) 推荐(0)

[Leetcode] Sqrt(x)
摘要:Implementint sqrt(int x).Compute and return the square root ofx.牛顿迭代法, 碉堡了。class Solution {public: int sqrt(int x) { double ans = x; while (abs(ans * ans - x) > 0.0001) { ans = (ans + x / ans) / 2; } return (int)ans; }}; 阅读全文

posted @ 2014-04-10 16:50 Eason Liu 阅读(152) 评论(0) 推荐(0)

[Leetcode] Largest Rectangle in Histogram
摘要:Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of each bar is 1, given height =[2,1,5,6,2,3].The largest rectangle is shown in the shaded area, which has ar 阅读全文

posted @ 2014-04-10 15:52 Eason Liu 阅读(809) 评论(0) 推荐(0)

[Leetcode] Unique Binary Search Trees II
摘要:Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ... 阅读全文

posted @ 2014-04-10 15:15 Eason Liu 阅读(1981) 评论(0) 推荐(0)

[Leetcode] Remove Duplicates from Sorted List II
摘要:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3-... 阅读全文

posted @ 2014-04-10 14:48 Eason Liu 阅读(155) 评论(0) 推荐(0)

[Leetcode] Container With Most Water
摘要:Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.Note: You 阅读全文

posted @ 2014-04-10 14:11 Eason Liu 阅读(2292) 评论(0) 推荐(0)

上一页 1 ··· 6 7 8 9 10 11 12 13 14 下一页