LTOJ
文章平均质量分 54
kyowill
这个作者很懒,什么都没留下…
展开
-
WordSearch
ACbool dfs(vector > &board,vector > &visit,string word, int i,int j,int dep,int maxdepth){ if(dep>=maxdepth) { return true; } // for(int i=0;i<4;++i) // { if(i+1<board.size()&&board[i+1][原创 2015-03-15 22:21:55 · 397 阅读 · 0 评论 -
Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the原创 2015-10-17 09:09:13 · 336 阅读 · 0 评论 -
Sort List
Sort a linked list in O(n log n) time using constant space complexity./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :原创 2015-09-14 22:49:35 · 196 阅读 · 0 评论 -
Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BANC"原创 2015-09-30 16:45:06 · 324 阅读 · 0 评论 -
Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, fi原创 2015-10-19 12:43:29 · 350 阅读 · 0 评论 -
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"].class Solution {public: string format(int原创 2015-10-19 14:25:06 · 253 阅读 · 0 评论 -
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 calling your原创 2015-10-19 18:31:41 · 237 阅读 · 0 评论 -
Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13,原创 2015-10-20 13:56:36 · 281 阅读 · 0 评论 -
Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the ent原创 2015-10-04 10:53:55 · 274 阅读 · 0 评论 -
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原创 2015-10-20 23:27:50 · 280 阅读 · 0 评论 -
Word Pattern
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.原创 2015-10-17 14:54:15 · 316 阅读 · 0 评论 -
Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each原创 2015-09-11 10:41:58 · 218 阅读 · 0 评论 -
Edit Distance
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:原创 2015-09-06 09:57:11 · 205 阅读 · 0 评论 -
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 assu原创 2015-09-04 16:26:33 · 218 阅读 · 0 评论 -
WordBreak
AC原创 2015-03-10 22:11:17 · 314 阅读 · 0 评论 -
Insert Interval
ACvector insert(vector &intervals, Interval newInterval) { if(intervals.empty()) { intervals.push_back(newInterval); return intervals; } vector val; for(int i=0;i <原创 2015-03-18 15:22:24 · 345 阅读 · 0 评论 -
Binary Tree Maximum Path Sum
AC:用一个vector保存经过以该节点为终点的最大值和以该节点为路径中间点的值int max(int a,int b){ if(a >= b) { return a; }else { return b; }}void dfs(TreeNode* root,vector& vec,int& sum){ if(root == NULL) { return;原创 2015-03-23 12:43:47 · 272 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant原创 2015-08-21 14:49:52 · 228 阅读 · 0 评论 -
N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.class Solution {public: bool isValid(vector& svec, int i, in原创 2015-09-27 20:27:14 · 238 阅读 · 0 评论 -
N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.原创 2015-09-27 20:20:33 · 237 阅读 · 0 评论 -
Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total numb原创 2015-09-06 16:05:22 · 202 阅读 · 0 评论 -
Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:Only one letter can be changed at a原创 2015-10-07 10:32:24 · 244 阅读 · 0 评论 -
Permutation总结
分析:求下一个排列,例如:1,2,3,4,51,2,3,5,41,2,4,3,5...1,2,5,4,31,3,2,4,5...大致分三个步骤:(1)从右[len-1]往左[i],找到num[i] > num[i-1],如没有,即i==0,则num为从大到小排列的数组,则此时的下一个排列,为所有元素从小到大的排列;(2)找到[num[i], num[len-转载 2015-09-15 19:22:10 · 352 阅读 · 0 评论 -
Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:You may ass原创 2015-10-23 14:12:54 · 277 阅读 · 0 评论 -
Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.关键思路:最短的含叶子节点子树,考虑如何缓存树的高度/** * D原创 2015-10-31 00:33:47 · 287 阅读 · 0 评论 -
Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's.1 3 3 2 1\ / / / \ \3 2 1 1 3 2/原创 2015-11-02 10:51:07 · 294 阅读 · 0 评论 -
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 with value原创 2015-12-23 10:35:37 · 266 阅读 · 0 评论 -
155. Min Stack
Min StackDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top()原创 2015-12-23 16:46:40 · 379 阅读 · 0 评论 -
232. Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) – Push element x to the back of queue. pop() – Removes the element from in front of queue. peek() – Get the front element. empty()原创 2015-12-23 22:00:14 · 295 阅读 · 0 评论 -
225. Implement Stack using Queues
Implement the following operations of a stack using queues.push(x) – Push element x onto stack. pop() – Removes the element on top of the stack. top() – Get the top element. empty() – Return whether原创 2015-12-24 17:26:59 · 346 阅读 · 0 评论 -
71. Simplify Path
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原创 2016-03-30 21:54:12 · 374 阅读 · 0 评论 -
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 representation 0000000000000原创 2016-03-10 15:42:15 · 258 阅读 · 0 评论 -
160. Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘原创 2016-03-10 16:54:18 · 306 阅读 · 0 评论 -
Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2015-10-30 14:39:34 · 319 阅读 · 0 评论 -
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 t原创 2015-07-09 00:08:55 · 250 阅读 · 0 评论 -
Basic Calculator
Implement a basic calculator to evaluate a simple expression string.The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty原创 2015-10-23 22:02:42 · 340 阅读 · 0 评论 -
Game of Life
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."Given a board with m by原创 2015-10-28 00:05:27 · 278 阅读 · 0 评论 -
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]./** * Definition for原创 2015-08-05 22:20:40 · 174 阅读 · 0 评论 -
Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since原创 2015-10-27 16:04:17 · 255 阅读 · 0 评论 -
Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one原创 2015-10-27 15:54:35 · 242 阅读 · 0 评论