# Daily Archives: April 9, 2015

## [leetcode] Length of Last Word

Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space characters only. […]

## [leetcode] Insert Interval

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9]. Example 2: Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and […]

## [leetcode] Merge Intervals

Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 10/7/2015 update Runtime error, quite strange. I don’t figure out why yet. /** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) […]

## [leetcode] Substring with Concatenation of All Words

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 […]

## [leetcode] Jump Game1

Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example: A = [2,3,1,1,4], return true. A […]

## [leetcode] Spiral Matrix

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. […]

## [leetcode] sqrt(x)

Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 惭愧，看了tag才知道需要用binary search。 二分查找，需要注意把x改为long long以免数据溢出。 另外，一个正数的平方根只能在[1, x / 2 + 1)之间变动。 class Solution { public: int sqrt(int x) { if(x < 2) return x; return _sqrt(x, 1, x / 2 + 1); } //[start, end) int _sqrt(int x, […]