leetcode-第八周
来源:互联网 发布:企业ip网络设计 编辑:程序博客网 时间:2024/05/29 19:28
50. Pow(x, n)
Implement pow(x, n).
/**
* 思路:二分,注意要考虑:
* 1. n为负数
* 2. x为0
*/
class Solution {
private:
const double EPS = 1e-10;
public:
double myPow(double x, int n) {
if (0 == n) return 1.0;
if (fabs(x) < EPS) return x;
bool neg = n < 0;
double ret = myPow(x, n / 2);
ret *= ret;
if (n & 1) {
if (neg) ret /= x;
else ret *= x;
}
return ret;
}
};
127. Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
- Only one letter can be changed at a time.
- Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog"
,
return its length 5
.
typedef unordered_map<string, int> MAP;
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
MAP dict, dp;
queue<string> que;
for (auto s: wordList) dict[s] = 1;
dp[beginWord] = 1;
que.push(beginWord);
while (!que.empty()) {
string current = que.front();
que.pop();
for (int i = 0; i < current.size(); i++) {
for (int j = 0; j < 26; j++) {
string nxt = current;
nxt[i] = 'a' + j;
if (dict.find(nxt) == dict.end()) continue;
if (dp.find(nxt) != dp.end()) continue;
dp[nxt] = dp[current] + 1;
que.push(nxt);
}
}
}
return dp[endWord];
}
};
542. 01 Matrix
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
Example 2:
Input:
0 0 00 1 01 1 1
Output:0 0 00 1 01 2 1
/**
* 原来的想法是DFS记忆化搜索的,结果WA
* 题解是用暴力BFS(从1向0)实现的,时间复杂度会比较高
* 一种优化的方案是,逆向思维,从0向1进行BFS
*/
class Solution {
private:
typedef vector<int> VI;
typedef vector<VI> VVI;
int bfs(VVI &ret, VVI &vis, int x, int y) {
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = { 0, 0, 1,-1};
const int n = ret.size(), m = ret[0].size();
queue<pair<int, int> > que;
que.push(make_pair(x * (m + n) + y, 0));
for (auto &v: vis) fill(v.begin(), v.end(), 0); // 注意这里的v要用引用
vis[x][y] = 1;
while (!que.empty()) {
int idx, dep;
tie(idx, dep) = que.front(); que.pop();
x = idx / (m + n), y = idx % (m + n);
if (ret[x][y] == 0) return dep;
for (int dir = 0; dir < 4; dir++) {
int nx = x + dx[dir], ny = y + dy[dir];
if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue;
if (vis[nx][ny]) continue;
vis[nx][ny] = 1;
que.push(make_pair(nx * (m + n) + ny, dep + 1));
}
}
return -1;
}
public:
vector<vector<int>> updateMatrix(VVI& A) {
if (A.empty() || A[0].empty()) return VVI();
const int n = A.size(), m = A[0].size();
VVI ret(n, VI(m, -1)), vis(n, VI(m, 0));
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) ret[i][j] = A[i][j] == 0? 0: -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (-1 != ret[i][j]) continue;
ret[i][j] = bfs(ret, vis, i, j);
}
}
return ret;
}
};
0 0
- leetcode-python 第八周
- leetcode-第八周
- 第八周leetcode题
- 第八周LeetCode
- 第八周LeetCode
- [leetcode]第八周作业
- LeetCode题解 第八周
- leetcode第八周解题总结
- 第八周LeetCode算法题两道
- Leetcode 算法习题 第八周
- 第八周:[leetcode] 228. Summary Ranges
- 第八周:[leetCode] 91. Decode Ways
- 第八周:[Leetcode]5. Longest Palindromic Substring
- 第八周:[Leetcode]516. Longest Palindromic Subsequence
- 第八周:[Leetcode]300. Longest Increasing Subsequence
- leetcode【第八周】链表划分
- Leetcode算法课程第八周(补)
- 第八周:[leetcode] 209. Minimum Size Subarray Sum
- 函数 _CrtSetBreakAlloc()使用blog
- 前端框架Vue(6)——Vue 介绍和相关操作
- Android报错:JNI/NDK相关
- Java Servlet完全教程
- 05.JS Call()与Apply()
- leetcode-第八周
- POJ_2752_Seek the Name, Seek the Fame【kmp】
- OC语言概念解释
- IOS开发--支付宝支付
- 谷歌浏览器开发工具使用教程
- makefile 增加宏定义
- Android studio 简单方便的使用JNI调用c语言中的方法
- js中数组与对象的区别
- 抓包路由器