摘要: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer's last digit is 0, what should the output be? 阅读全文
posted @ 2014-03-28 22:52 beehard 阅读(122) 评论(0) 推荐(0) 编辑
摘要: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)- Set or insert the value if the key is not 阅读全文
posted @ 2014-03-28 07:26 beehard 阅读(193) 评论(0) 推荐(0) 编辑
摘要: 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 "bbbbb" the longest substring is "b", with the length of 1. 阅读全文
posted @ 2014-03-28 05:36 beehard 阅读(119) 评论(0) 推荐(0) 编辑
摘要: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. Solution: Find left bound and right bound for each element. O(n). 1 class Solution { 2 publi. 阅读全文
posted @ 2014-03-26 22:41 beehard 阅读(116) 评论(0) 推荐(0) 编辑
摘要: 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 differ by more than 1. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * ... 阅读全文
posted @ 2014-03-15 01:51 beehard 阅读(109) 评论(0) 推荐(0) 编辑
摘要: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. Solution: Binary search. 阅读全文
posted @ 2014-03-14 21:40 beehard 阅读(101) 评论(0) 推荐(0) 编辑
摘要: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. Note: You are not suppose to use 阅读全文
posted @ 2014-03-14 10:54 beehard 阅读(110) 评论(0) 推荐(0) 编辑
摘要: http://oj.leetcode.com/problems/add-two-numbers/Note:You are given two linked lists representing two non-negative numbers. The digits are stored in r... 阅读全文
posted @ 2014-03-13 04:51 beehard 阅读(189) 评论(0) 推荐(0) 编辑
摘要: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. Solution: For each element, calculate the max differe. 阅读全文
posted @ 2014-03-12 09:27 beehard 阅读(156) 评论(0) 推荐(0) 编辑
摘要: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].Solution: dfs(recursion) 1 class Solution { 2 public: 3 vector > res; 4 5 void permute(vector& num, vector& per.. 阅读全文
posted @ 2014-03-12 07:54 beehard 阅读(157) 评论(0) 推荐(0) 编辑