【LeetCode】未分类(tag里面没有)(共题)

【419】Battleships in a Board (2018年11月25日)(谷歌的题,没分类。)

给了一个二维平面,上面有 X 和 . 两种字符。 一行或者一列连续的 X 代表一个战舰,问图中有多少个战舰。(题目要求one pass, 空间复杂度是常数)

题目说了每两个战舰之间起码有一个 . 作为分隔符,所以不存在正好交叉的情况。

题解:我提交了一个 floodfill 的题解,能过但是显然不满足要求。

discuss里面说,我们可以统计战舰的最上方和最左方,从而来统计战舰的个数。(这个解法是满足要求的。)

 1 class Solution {
 2 public:
 3     int countBattleships(vector<vector<char>>& board) {
 4         n = board.size();
 5         m = board[0].size();
 6         int ret = 0;
 7         for (int i = 0; i < n; ++i) {
 8             for (int j = 0; j < m; ++j) {
 9                 if (board[i][j] == ‘.‘) {continue;}
10                 if ((i == 0 || board[i-1][j] != ‘X‘) && (j == 0 || board[i][j-1] != ‘X‘)) {ret++;}
11             }
12         }
13         return ret;
14     }
15     int n, m;
16 };

【427】Construct Quad Tree(2019年2月12日)

建立四叉树。https://leetcode.com/problems/construct-quad-tree/description/

题解:递归

 1 /*
 2 // Definition for a QuadTree node.
 3 class Node {
 4 public:
 5     bool val;
 6     bool isLeaf;
 7     Node* topLeft;
 8     Node* topRight;
 9     Node* bottomLeft;
10     Node* bottomRight;
11
12     Node() {}
13
14     Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {
15         val = _val;
16         isLeaf = _isLeaf;
17         topLeft = _topLeft;
18         topRight = _topRight;
19         bottomLeft = _bottomLeft;
20         bottomRight = _bottomRight;
21     }
22 };
23 */
24 class Solution {
25 public:
26     Node* construct(vector<vector<int>>& grid) {
27         const int n = grid.size();
28         vector<int> tl = {0, 0}, br = {n-1, n-1};
29         return construct(grid, tl, br);
30     }
31     Node* construct(vector<vector<int>>& grid, vector<int> tl, vector<int> br) {
32         if (tl == br) {
33             Node* root = new Node(grid[tl[0]][tl[1]], true, nullptr, nullptr, nullptr, nullptr);
34             return root;
35         }
36         int t = tl[0], b = br[0], l = tl[1], r = br[1];
37         bool split = false;
38         for (int i = t; i <= b; ++i) {
39             for (int j = l; j <= r; ++j) {
40                 if (grid[i][j] != grid[t][l]) {
41                     split = true;
42                 }
43             }
44         }
45         if (!split) {
46             Node* root = new Node(grid[tl[0]][tl[1]], true, nullptr, nullptr, nullptr, nullptr);
47             return root;
48         }
49         Node* root = new Node(0, false, nullptr, nullptr, nullptr, nullptr);
50         int newx = (t + b) / 2, newy = (l + r) / 2;
51         root->topLeft = construct(grid, tl, {newx, newy});
52         root->topRight = construct(grid, {t, newy+1}, {newx, r});
53         root->bottomLeft = construct(grid, {newx + 1, l}, {b, newy});
54         root->bottomRight = construct(grid, {newx+1, newy+1}, br);
55         return root;
56     }
57 };

【433】 Minimum Genetic Mutation(2018年12月28日,周五)(谷歌的题,没分类)

给了两个字符串基因序列start 和 end,和一个 word bank,每次操作需要把当前序列,通过一次变异转换成word bank里面的另外一个词,问从 start 转换成 end,至少需要几步?

题解:问最少几步,直接bfs解

 1 class Solution {
 2 public:
 3     int minMutation(string start, string end, vector<string>& bank) {
 4         const int n = bank.size();
 5         vector<int> visit(n, 0);
 6         queue<string> que;
 7         que.push(start);
 8         int step = 0;
 9         int ret = -1;
10         while (!que.empty()) {
11             step++;
12             const int size = que.size();
13             for (int i = 0; i < size; ++i) {
14                 string cur = que.front(); que.pop();
15                 for (int k = 0; k < n; ++k) {
16                     if (visit[k]) {continue;}
17                     if (diff(bank[k], cur) == 1) {
18                         visit[k] = 1;
19                         que.push(bank[k]);
20                         if (bank[k] == end) {
21                             ret = step;
22                             return ret;
23                         }
24                     }
25                 }
26             }
27         }
28         return ret;
29     }
30     int diff (string s1, string s2) {
31         if (s1.size() != s2.size()) {return -1;}
32         int cnt = 0;
33         for (int i = 0; i < s1.size(); ++i) {
34             if (s1[i] != s2[i]) {
35                 ++cnt;
36             }
37         }
38         return cnt;
39     }
40 };

【504】Base 7 (2018年11月25日)

Given an integer, return its base 7 string representation.

Example 1:
Input: 100
Output: "202"

Example 2:
Input: -7
Output: "-10"

Note: The input will be in range of [-1e7, 1e7].

题解:直接按照进制转换

 1 class Solution {
 2 public:
 3     string convertToBase7(int num) {
 4         bool negative = false;
 5         if (num < 0) {
 6             negative = true;
 7             num = -num;
 8         }
 9         string ret = "";
10         while (num != 0) {
11             int mod = num % 7;
12             num = num / 7;
13             ret = to_string(mod) + ret;
14         }
15         ret = negative ? "-" + ret : ret;
16         ret = ret == "" ? "0" : ret;
17         return ret;
18     }
19 };

【506】Relative Ranks(2018年11月25日)

Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Silver Medal" and "Bronze Medal".

Example 1:
Input: [5, 4, 3, 2, 1]
Output: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
Explanation: The first three athletes got the top three highest scores, so they got "Gold Medal", "Silver Medal" and "Bronze Medal".
For the left two athletes, you just need to output their relative ranks according to their scores.

Note:

  1. N is a positive integer and won‘t exceed 10,000.
  2. All the scores of athletes are guaranteed to be unique.

 1 class Solution {
 2 public:
 3     vector<string> findRelativeRanks(vector<int>& nums) {
 4         const int n = nums.size();
 5         map<int, int, greater<int>> mp;
 6         for (int i = 0; i < nums.size(); ++i) {
 7             int score = nums[i];
 8             mp[score] = i;
 9         }
10         vector<string> ret(n);
11         int cnt = 1;
12         for (auto e : mp) {
13             int idx = e.second;
14             if (cnt <= 3) {
15                 if (cnt == 1) {
16                     ret[idx] = "Gold Medal";
17                 }
18                 if (cnt == 2) {
19                     ret[idx] = "Silver Medal";
20                 }
21                 if (cnt == 3) {
22                     ret[idx] = "Bronze Medal";
23                 }
24                 ++cnt;
25             } else {
26                 ret[idx] = to_string(cnt++);
27             }
28         }
29         return ret;
30     }
31 };

【540】Single Element in a Sorted Array(2018年12月8日,本题不是自己想的,看了花花酱的视频,需要review)

给了一个有序数组,除了一个数字外,其他所有数字都出现两次。用 O(logN) 的时间复杂度,O(1) 的空间复杂度把只出现一次的那个数字找出来。

题解:二分。怎么二分。我们的目的的找到第一个不等于它partner的数字。如何定义一个数的 partner,如果 i 是偶数,那么 nums[i] 的partner是 nums[i+1]。如果 i 是奇数,nums[i] 的partner 是 i-1。

 1 class Solution {
 2 public:
 3     int singleNonDuplicate(vector<int>& nums) {
 4         const int n = nums.size();
 5         int left = 0, right = n;
 6         while (left < right) {
 7             int mid = (left + right) / 2;
 8             int partner = mid % 2 == 0 ? mid + 1 : mid - 1;
 9             if (nums[mid] == nums[partner]) {
10                 left = mid + 1;
11             } else {
12                 right = mid;
13             }
14         }
15         return nums[left];
16     }
17 };

【797】All Paths From Source to Target (2018年11月27日)

给了一个 N 个结点的 DAG,结点标号是0 ~ N-1,找到从 node 0 到 node N-1 的所有路径,并且返回。

Example:
Input: [[1,2], [3], [3], []]
Output: [[0,1,3],[0,2,3]]
Explanation: The graph looks like this:
0--->1
|    |
v    v
2--->3
There are two paths: 0 -> 1 -> 3 and 0 -> 2 -> 3.

题解:dfs可以解。

 1 class Solution {
 2 public:
 3     vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
 4         n = graph.size();
 5         vector<vector<int>> paths;
 6         vector<int> path(1, 0);
 7         dfs(graph, paths, path);
 8         return paths;
 9     }
10     void dfs(const vector<vector<int>>& graph, vector<vector<int>>& paths, vector<int>& path) {
11         if (path.back() == n-1) {
12             paths.push_back(path);
13             return;
14         }
15         int node = path.back();
16         for (auto adj : graph[node]) {
17             path.push_back(adj);
18             dfs(graph, paths, path);
19             path.pop_back();
20         }
21     }
22     int n;
23 };

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica }
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica }

原文地址:https://www.cnblogs.com/zhangwanying/p/10016508.html

时间: 2024-11-06 03:40:46

【LeetCode】未分类(tag里面没有)(共题)的相关文章

leetcode中关于树的dfs算法题

Validate Binary Search Tree Recover Binary Search Tree Symmetric Tree Same Tree Maximum Depth of Binary Tree Construct Binary Tree from Preorder and Inorder Traversal Construct Binary Tree from Inorder and Postorder Traversal Convert Sorted Array to

【LeetCode】链表 linked list(共34题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 =

【LeetCode】线段树 segment-tree(共9题)+ 树状数组 binary-indexed-tree(共5题)

第一部分---线段树:https://leetcode.com/tag/segment-tree/ p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [218]The Skyline Problem [307]Range Sum Query - Mutable [308]Range Sum Query 2D - Mutable [315]Count of Smaller Numbers After Self [493

【LeetCode】回溯法 backtracking(共39题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses (2019年2月13日) 给了一个N,生成N对括号的所有情况的字符串. n = 3 [ "((()))", "(()())", "(

【LeetCode】前缀树 trie(共14题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [208]Implement Trie (Prefix Tree) (2018年11月27日) 实现基本的 trie 树,包括 insert, search, startWith 操作等 api. 题解:<程序员代码面试指南>chp5, 最后一题. 里面讲了怎么实现.这个就看代码吧.没啥好说的了. 1 class Trie { 2 public: 3 /** Ini

暂未分类的杂题

CF600E dsu on tree P1345 最小割点,拆点连流量为1的边 其余边流量无穷,跑最大流 P2073 set教学 P2617 树套树 P2746 缩点之后把一些树搞成联通的最小代价是入度为0的点和出度为0的点中的较大值 P3119 缩点建反边跑DAG上dp P3376 最大流 P3381 最小费用最大流,dinic上套spfa P3419 优先队列贪心,消除最久未使用的 P1955 并查集 P2759 十进制数的位数为log10(x)+1,加一是为了取整 P1220 区间DP P

【LeetCode】深搜DFS(共85题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [104]Maximum Depth of Binary Tree [105]Construct Binary Tree from Preorder and Inorder

未分类随笔

H-Index https://leetcode.com/problems/h-index/ 这个题还是有些绕的,主要是题意搞得有点晕了.最后题意应该是:h 为 h篇文章的引用至少为h,其余N-h篇文章的引用至多为h.(另外一种错误的理解是引用大于h的至少h篇) 方法一:hashtable 新建一个数组来存储每个引用分别有多少篇文章,由于h最多为文章总数len,所以对于那些引用大于文章总数len的,就把这些文章也加载引用为len次的元素位置.然后从这个数组末尾开始遍历. 使用一个tmp来存储文章

leetcode腾讯精选练习(50 题)(持续更新)

1.除自身以外数组的乘积 给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输入: [1,2,3,4]输出: [24,12,8,6]说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题. 题解:记录前缀积和后缀积. 参考代码: class Solution { public: vector<int> productExceptSelf(vector&