61. Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.
Example:
Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL.
//如果k大于链表长度时,应该旋转 k%len
//一个快指针,一个慢指针,快的指针比慢的先走k个,这样当快的指针走到NULL时,慢的刚好是倒数第k个
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head || !head->next) return head;
ListNode *p, *q;
p = q = head;
int len = 0;
while(p)
{
p = p->next;
len++;
}
k %= len;
if(k == 0) return head;
p = head;
while(p && k--)
{
p = p->next;
}
if(!p) return head;
while(p->next)
{
p = p->next;
q = q->next;
}
ListNode *cur = q->next;
q->next = NULL;
p->next = head;
return cur;
}
};
62
.
Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
//记忆化搜索,暴力搜索会超时,动态规划也可以写,其实记忆化搜索就是变相的DP
class Solution {
public:
int vis[101][101];
int dfs(int i, int j, int m, int n)
{
if(vis[i][j]) return vis[i][j];
if(i == m && n == j) return 1;
int a, b;
a = b = 0;
if(i < m) a = vis[i+1][j] = dfs(i+1, j, m, n);
if(j < n) b = vis[i][j+1] = dfs(i, j+1, m, n);
return a + b;
}
int uniquePaths(int m, int n) {
memset(vis, 0, sizeof(vis));
return dfs(1, 1, m, n);
}
};
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 3x3 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.
//同上一题
class Solution {
public:
int vis[101][101];
int dfs(int i, int j, int m, int n, vector<vector<int>>& obstacleGrid)
{
if(vis[i][j] != -1) return vis[i][j];
if(i == m && n == j) return 1;
int a, b;
a = b = 0;
if(i < m && !obstacleGrid[i+1][j]) a = vis[i+1][j] = dfs(i+1, j, m, n, obstacleGrid);
if(j < n && !obstacleGrid[i][j+1]) b = vis[i][j+1] = dfs(i, j+1, m, n, obstacleGrid);
return a + b;
}
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
if(obstacleGrid.size() == 0) return 0;
memset(vis, -1, sizeof(vis));
int m = obstacleGrid.size()-1;
int n = obstacleGrid[0].size()-1;
if(obstacleGrid[m][n] || obstacleGrid[0][0]) return 0;
return dfs(0, 0, m, n, obstacleGrid);
}
};
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.
Example 1:
[[1,3,1], [1,5,1], [4,2,1]]Given the above grid map, return
7
. Because the path 1→3→1→1→1 minimizes the sum.
//同上一题,也可以DP
class Solution {
public:
int vis[509][509];
int dfs(int i, int j, int m, int n, vector<vector<int>>& grid)
{
if(vis[i][j] != 0x3f3f3f3f) return vis[i][j];
if(i == m && n == j) return grid[m][n];
if(i < m)
{
int k = dfs(i+1, j, m, n, grid);
vis[i][j] = min(vis[i][j], k+grid[i][j]);
}
if(j < n)
{
int k = dfs(i, j+1, m, n, grid);
vis[i][j] = min(vis[i][j], k+grid[i][j]);
}
return vis[i][j];
}
int minPathSum(vector<vector<int>>& grid) {
if(grid.size() == 0) return 0;
memset(vis, 0x3f, sizeof(vis));
return dfs(0, 0, grid.size()-1, grid[0].size()-1, grid);
}
};
65
.
Valid Number
Validate if a given string is numeric.
Some examples:
"0"
=> true
" 0.1 "
=> true
"abc"
=> false
"1 a"
=> false
"2e10"
=> true
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
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.
//WA了十万发,终于给A了,需要判断的地方有很多
// . + - ' ' e E等
class Solution {
public:
bool isNumber(string s) {
int len = s.length();
int falg = 0, i = 0;
while(i < len && s[i] == ' ') i++;
if(len == i) return false;
while(i < len)
{
if(isdigit(s[i])) i++;
else if((s[i] == 'e' || s[i] == 'E') && falg != 2)
{
if(i == 0 || s[i-1] == ' ') return false;
if(((i+1 < len) && isdigit(s[i+1])) || ((i+2 < len)&&((s[i+1] == '-'||s[i+1]=='+') && isdigit(s[i+2])))) i += 2;
else return false;
falg = 2;
}
else if(s[i] == '-' || s[i] == '+')
{
if(i - 1 >= 0 && isdigit(s[i-1])) return false;
if((i+1 < len && isdigit(s[i+1])) || (i+2 < len && s[i+1] == '.' && isdigit(s[i+2]))) i += 2;
else return false;
}
else if(s[i] == '.' && !falg)
{
i++;
if(i < len && isdigit(s[i])) i++;
else if(i - 2 >= 0 && isdigit(s[i-2])) ;
else return false;
falg = 1;
}
else if(s[i] == ' ')
{
while(i < len && s[i] == ' ') i++;
if(i == len) return true;
return false;
}
else return false;
}
return true;
}
};