力扣算法题—079单词搜索【DFS】】的更多相关文章

给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. 示例: board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] 给定 word = "ABCCED", 返回 true. 给定 word = "SEE", 返回 true. 给定 word…
实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 2: 输入: 8 输出: 2 说明: 8 的平方根是 2.82842...,   由于返回类型是整数,小数部分将被舍去. #include "_000库函数.h" //最简单想法,耗时长 class Solution { public: int mySqrt(int x) { )retur…
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", "255.255.111.35"] //暴力搜索 //一共分为4组 //每组数据不超过三位 class Solution { public: vector<string> restoreIpAddresses(string s) { vector<string>re…
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132" "213" "231" "312" "321" 给定 n 和 k,返回第 k 个排列. 说明: 给定 n 的范围是 [1, 9]. 给定 k 的范围是[1,  n!]. 示例 1: 输入: n = 3, k = 3 输出:…
#include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化为 - 1,然后从第0开始递归, //每一行都一次遍历各列,判断如果在该位置放置皇后会不会有冲突,以此类推, //当到最后一行的皇后放好后,一种解法就生成了,将其存入结果res中, //然后再还会继续完成搜索所有的情况,代码如下:17ms class Solution { public: vector<…
#include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { if (n == 0)return 1; double res = 1.0; for (int i = n; i != 0; i /= 2) { if (i % 2 != 0) res *= x; x *= x; } return n > 0 ? res : 1 / res; } }; //同样使用二…
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.With each iteration one element (red) is removed from the input data and inserted in…
  Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15…
跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(int n) { ; vector<);//标记 NQueue(x, res, ); return res; } void NQueue(vector<int>&x, int &num, int row) { int n = x.size(); if (n == row) num…
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not modify the values in the list's nodes, only nodes itself may be changed. Example 1: Given 1->2->3->4, reorder it to 1->4->2->3. Example 2: G…