↓ Skip to main content
  1. Posts/

leetcode 64. Minimum Path Sum (二维dp)

·371 words·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

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.

数字三角形。。。。从左上到右下问最短路径。。每次只能向下或者向右。。。

wa了一次。。。是因为边界值赋值成了0.。。求最短路径显然因为赋值成inf才对orz..果然傻了。。

简单的dp我们简单的A.

顺便吐槽一下。。(100,100)的答案会溢出int…然而答案就是负的。。。就没人check一下吗,,,

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2017年04月10日 星期一 10时11分26秒
 4File Name :64.cpp
 5************************************************ */
 6class Solution {
 7public:
 8    int minPathSum(vector<vector<int>>& grid) {
 9	int n = grid.size();
10	int m = grid[0].size();
11	vector<vector<int> > dp(n+1,vector<int>(m+1,0x3f3f3f3f)); //求最小路径,初始化为最大值。
12
13	dp[n-1][m-1] = grid[n-1][m-1];
14	for ( int i = n-1 ;  i >= 0 ; i--)
15	{
16	    for ( int j = m-1 ; j >= 0 ; j--)
17	    {
18		if (i==n-1&&j==m-1) continue;
19		dp[i][j] = min(dp[i+1][j],dp[i][j+1]) + grid[i][j];
20	    }
21	}
22
23	for ( int i = 0 ; i < n ; i++)
24	{
25	    for ( int j = 0 ; j < m ; j++)
26		printf("%d ",dp[i][j]);
27	    printf("\n");
28	}
29	int res = dp[0][0];
30	cout<<"res:"<<res<<endl;
31	return res;
32    }
33};

Related

leetcode 73. Set Matrix Zeroes (矩阵置0,乱搞)

·926 words·2 mins
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. **Follow up:**Did you use extra space? A straight forward solution using O(m__n) space is probably a bad idea. A simple improvement uses O(m + n) space, but still not the best solution. Could you devise a constant space solution? 直接放常数空间的做法。 这道题面hypereal的时候遇到过,基本思路就是用已经确定是0的位置来存储其他行和列的信息。

leetcode 238. Product of Array Except Self (乱搞)

·411 words·1 min
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. Solve it without division and in O(n). For example, given [1,2,3,4], return [24,12,8,6]. Follow up: Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.) 先来个O(n)空间的无脑解法。。。一个前缀积一个后缀积就好了。。。

leetcode 79. Word Search (dfs)

·374 words·1 min
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. 思路:dfs即可。记得要回溯一下… 代码实现 1/* *********************************************** 2Author :111qqz 3Created Time :2017年04月07日 星期五 14时32分54秒 4File Name :79.cpp 5************************************************ */ 6class Solution { 7 8public: 9 10 int n,m; 11 const int dx4[4]={1,-1,0,0}; 12 const int dy4[4]={0,0,-1,1}; 13 bool vis[1005][1005]; 14 int len; 15 16 bool yes( int x,int y) 17 { 18 if (x>=0&&x<=n-1&&y>=0&&y<=m-1) return true; 19 return false; 20 } 21 22 bool dfs( int x,int y,int cur,vector<vector<char> > & maze,string & st) 23 { 24// printf("x:%d y:%d cur : %d\n",x,y,cur); 25 if (cur>=len) return true; 26 for ( int i = 0 ; i < 4 ; i++) 27 { 28 int nx = x + dx4[i]; 29 int ny = y + dy4[i]; 30 if (!yes(nx,ny)) continue; 31 if (vis[nx][ny]) continue; 32 if (maze[nx][ny]!=st[cur]) continue; 33 vis[nx][ny] = true; 34 bool res = dfs(nx,ny,cur+1,maze,st); 35 if (res) return true; 36 vis[nx][ny] = false ;//好像还要回溯一下啊? 37 } 38 return false; 39 } 40 41 bool exist(vector<vector<char>>& board, string word) { 42 n = board.size(); 43 if (n==0) return false; 44 m = board[0].size(); 45 if (m==0) return false; 46 len = word.length(); 47 cout<<"len:"<<len<<endl; 48 char beg = word[0]; 49 for ( int i = 0 ; i < n ; i++) 50 for ( int j = 0 ; j < m ; j++) 51 if (board[i][j]==beg) 52 { 53 memset(vis,false,sizeof(vis)); 54 //起点忘记打标记了。。。智力-2.。。 55 vis[i][j] = true; 56 bool ok = dfs(i,j,1,board,word); 57 cout<<"ok:"<<ok<<endl; 58 if (ok) return true; 59 } 60 return false; 61 62 63 } 64 65};

leetcode 80 Remove Duplicates from Sorted Array II (有序数组去除重复元素)

·324 words·1 min
Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn’t matter what you leave beyond the new length. Subscribe to see which companies asked this question. 题意:一个有序数组,每个元素最多出现两次,如果大于两次,把多的去掉,返回去掉后的数组长度len,以及要求数组前len是去掉那些元素之后的元素。//语死早。。看原题好了。。

leetcode 81. Search in Rotated Sorted Array II (有重复元素的旋转数组找给定值)

·333 words·1 min
Suppose an array sorted in ascending order 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). Write a function to determine if a given target is in the array. The array may contain duplicates. 好像阿里一面的时候问过。。。 思路:肯定是二分。。。不过由于有重复元素。。。所以很恶心。。。