LeetCode 64. Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

分析:用一个dp二维数组标记当前方格的最小值~

0.当i == 0 && j == 0的时候,dp[i][j] = grid[i][j];

1.对于i==0的时候,为最上面一排,当前方格只能由左边方格来,所以dp[i][j] = dp[i][j-1] + grid[i][j];

2.对于j==0的时候,为最左边一排,当前方格只能由上边方格来,所以dp[i][j] = dp[i-1][j] + grid[i][j];

3.其他情况下,dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j];

最后直到一直递推输出到终点(m-1, n-1)的时候return dp[m-1][n-1];~~

LeetCode 63. Unique Paths II

Follow up for “Unique Paths”:

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

For example,
There is one obstacle in the middle of a 3×3 grid as illustrated below.

[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2.

Note: m and n will be at most 100.

分析:这道题是上一道题的升级版~加了障碍物~加不加障碍物差别就在于,当当前地域有障碍物的时候,a[i][j] = 0,其余的不变:
0.a[0][0] = 1; 1.对于i==0的时候,为最上面一排,当前方格只能由左边方格来,所以a[i][j] = a[i][j-1];  2.对于j==0的时候,为最左边一排,当前方格只能由上边方格来,所以a[i][j] = a[i-1][j];  3.其余情况,当前方格能由左边和上边两个方向过来,所以a[i][j] = a[i-1][j] + a[i][j-1];  最后直到一直递推输出到终点(m-1, n-1)的时候return a[m-1][n-1]; 

 

LeetCode 347. Top K Frequent Elements

Given a non-empty array of integers, return the k most frequent elements.
For example,
Given [1,1,1,2,2,3] and k = 2, return [1,2].
Note:
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.

分析:先遍历数组将数组的值与出现的次数用map映射,因为根据map的值来排序,所以用pair<int, int>类型的vector转存后用sort实现排序方法。从大到小排序,然后取前k个值储存在新的vector v中返回v。

 

LeetCode 144. 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
/
3
return [1,2,3].

 

LeetCode319. Bulb Switcher

319. Bulb Switcher
My Submissions QuestionEditorial Solution
Total Accepted: 17314 Total Submissions: 42815 Difficulty: Medium
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off if it’s on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

Example:

Given n = 3.

At first, the three bulbs are [off, off, off].
After first round, the three bulbs are [on, on, on].
After second round, the three bulbs are [on, off, on].
After third round, the three bulbs are [on, off, off].

So you should return 1, because there is only one bulb is on.

分析:可知题意是想判断1~n中那些数的因子个数是奇数,一个数 a = b x c 当 b = c 的时候 a 的因子个数就是奇数,所以就是判断1~n当中有多少个是某数的平方~

 

LeetCode 8. String to Integer (atoi)

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

spoilers alert… click to show requirements for atoi.

Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.