面试笔试杂项积累-leetcode 61-65
来源:互联网 发布:php mysql 开启预编译 编辑:程序博客网 时间:2024/05/16 04:43
61.61-Rotate List-Difficulty:Medium
Given a list, rotate the list to the right by k places, where k is non-negative.
For example:
Given 1->2->3->4->5->NULL
and k = 2
,
return 4->5->1->2->3->NULL
.
思路
给一个链表返回旋转k个之后的链表。很简单,先遍历一遍使head接在end上,顺便求出长度,然后直接在length-k处断掉即可
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */public class Solution { public ListNode RotateRight(ListNode head, int k) { if(head==null||k == 0) return head; ListNode last = head; ListNode temp; int i = 1; while (last.next != null) { last = last.next; ++i; } while(i <=k) k = k-i; i = i - k; last.next = head; last = head; while (i > 1) { last = last.next; --i; } temp = last.next; last.next = null; return temp; }}
62.62-Unique Paths-Difficulty:Medium
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.
思路
找到左上角到右下角所有可能的路径数量。
使用动态规划能轻松解决
对于格点(i,j)。由于只能从上格点(i-1,j)或左格点(i,j-1)到达,并且两者路径是不重复的
因此path[i][j] = path[i-1][j]+path[i][j-1]
附上动态规划教程链接:
五大常用算法之二:动态规划算法
public class Solution { public int UniquePaths(int m, int n) { int[,] fin = new int[m, n]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) fin[i, j] = 1; for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { fin[i, j] = fin[i - 1, j] + fin[i, j - 1]; } } return fin[m - 1, n - 1]; }}
63.63-Unique Paths II-Difficulty:Medium
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
and0
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.
思路
和上一题大体相同,但加入了障碍物的情况,有障碍物的地方不能到达,如果终点本身为障碍物则不能到达
public class Solution { public int UniquePathsWithObstacles(int[,] obstacleGrid) { int m = obstacleGrid.GetLength(0); int n = obstacleGrid.GetLength(1); if(obstacleGrid[m - 1, n - 1] == 1) return 0; int[,] fin = new int[m, n]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) fin[i, j] = 0; for (int i = 0; i < m; i++) { if (obstacleGrid[i,0] != 1) fin[i,0] = 1; else break; } for (int i = 0; i < n; i++) { if (obstacleGrid[0,i] != 1) fin[0,i] = 1; else break; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (obstacleGrid[i,j] == 1) fin[i,j] = 0; else fin[i, j] = fin[i - 1, j] + fin[i, j - 1]; } } return fin[m - 1, n - 1]; }}
64.64-Minimum Path Sum-Difficulty:Medium
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right whichminimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
思路
和上两道题相近,返回左上到右下最小的路径节点和,依旧动态规划
public class Solution { public int MinPathSum(int[,] grid) { int m = grid.GetLength(0); int n = grid.GetLength(1); if(m == 1&&n ==1) return grid[0,0]; int[,] fin = new int[m, n]; Array.Copy(grid, fin, grid.Length); for (int i = 2; i < n; i++) { fin[0, i] += fin[0, i - 1]; } for (int i = 2; i < m; i++) { fin[i, 0] += fin[i - 1, 0]; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { fin[i, j] += fin[i - 1, j] > fin[i, j - 1] ? fin[i, j - 1] : fin[i - 1, j]; } } return fin[m - 1, n - 1]+fin[0,0]; }}
65.65-Minimum Path Sum-Difficulty:Hard
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.
思路
判断字符串是不是数字,不用考虑前后缀空格。
需要注意:
1.小数的"."的位置
2."e"存在的位置和次数
3.正负号
public class Solution { public bool IsNumber(string s) { s = s.TrimEnd(' '); s = s.TrimStart(' '); if (s.Length == 0) return false; int start = 0; if (s[0] == 43 || s[0] == 45) start = 1; bool isDecimals = false; bool isE = false; for (int i = start; i < s.Length; i++) { if (!isDecimals && !isE) { if (s[i] == 46) { if (i == s.Length - 1 && i <= start) { return false; } isDecimals = true; continue; } } if (s[i] < 48 || s[i] > 57) { if (!isE ) if (s[i] == 101 && i > start && i < s.Length-1) { if(s[i-1] == 46&&i-1 == start) return false; isE = true; if ((s[i+1] == 43 || s[i+1] == 45)&&i+1<s.Length-1) ++i; continue; } return false; } } return true; }}
- 面试笔试杂项积累-leetcode 61-65
- 面试笔试杂项积累-leetcode 1-5
- 面试笔试杂项积累-leetcode 6-10
- 面试笔试杂项积累-leetcode 11-15
- 面试笔试杂项积累-leetcode 16-20
- 面试笔试杂项积累-leetcode 21-25
- 面试笔试杂项积累-leetcode 26-30
- 面试笔试杂项积累-leetcode 31-35
- 面试笔试杂项积累-leetcode 36-40
- 面试笔试杂项积累-leetcode 41-45
- 面试笔试杂项积累-leetcode 46-50
- 面试笔试杂项积累-leetcode 51-55
- 面试笔试杂项积累-leetcode 56-60
- 面试笔试杂项积累-leetcode 66-70
- 面试笔试杂项积累-leetcode 71-75
- 面试笔试杂项积累-leetcode 76-80
- 面试笔试杂项积累-leetcode 81-85
- 面试笔试杂项积累-leetcode 86-90
- Codeforce 621(A,B,C,D,E)
- Android初学习 - implements OnClickListener
- 南阳OJ 28:大数阶乘
- Java 异步通信 程序
- 详解赋范空间——图像处理中的数学原理详解24
- 面试笔试杂项积累-leetcode 61-65
- 理解RESTful架构
- 安卓进阶之自定义View
- Android 调整图标和字体大小
- jQuery性能提升方法
- androidStudio导入SlidingMenu第三方框架中出现的问题
- 第13讲-数值型数据的存储-练习
- HDU 1520 Anniversary party (树状DP)
- 设计模式之抽象工厂