1. Word Search

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.

For example,
Given board =

[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

思路:其实我感觉LeetCode上中等难度的题目大多数都可以用递归和dp来解决。遍历矩阵,如果遇到的这个字符和要匹配的单词中的第一个字符相等,那么接下来是看这个字符的上下左右(不越界的情况)是不是和单词中的第二个字符相等的,以此类推,实际上是在不断循环的调用单个字符是否匹配这个方法,如果匹配则往下层递归,不匹配返回上层,当递归层数等于word字符数时,表明找到完全匹配项了,返回true。剩下一个要注意的点是,避免字符重用,不能出现往上走又往下走这种递归情况。

class Solution {
public boolean exist(char[][] board, String word) {
char[] w = word.toCharArray();
for (int y=0; y<board.length; y++) {
for (int x=0; x<board[y].length; x++) {
if (exist(board, y, x, w, 0)) return true;
}
}
return false;
} private boolean exist(char[][] board, int y, int x, char[] word, int i) {
if (i == word.length) return true;
if (y<0 || x<0 || y == board.length || x == board[y].length) return false;
if (board[y][x] != word[i]) return false;
board[y][x] = "*";  // 将当前位置设置未一个非字母字符以免后续遍历时再回到此位置时出现重复利用字符的情况
boolean exist = exist(board, y, x+1, word, i+1)
|| exist(board, y, x-1, word, i+1)
|| exist(board, y+1, x, word, i+1)
|| exist(board, y-1, x, word, i+1);
board[y][x] = word[i];  // 遍历结束后需置回原来字符
return exist;
}
}

2. Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,2], a solution is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]

思路:Subset题目的改进版,原数组中的元素可以重复,所以难点是怎么处理数组中重复的元素,对于之前数组元素不重复的Subset,解决方法是先排序一遍,从长度0到数组长度n循环递归,加入一个值,再对其后面的数组部分以同样的方法递归。对于本题目,可以确定重复的子集发生的情形是子集长度一样的情况,所以需要再从长度0到数组长度n循环递归中的循环中加一些代码来防止重复。

public List<List<Integer>> subsetsWithDup(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
List<Integer> each = new ArrayList<>();
helper(res, each, 0, nums);
return res;
}
public void helper(List<List<Integer>> res, List<Integer> each, int pos, int[] n) {
if (pos <= n.length) {
res.add(each);
}
int i = pos;
while (i < n.length) {
each.add(n[i]);
helper(res, new ArrayList<>(each), i + 1, n);
each.remove(each.size() - 1);
i++;
while (i < n.length && n[i] == n[i - 1]) {i++;}  // 避免重复
}
return;
}

The Basic idea is: use “while (i < n.length && n[i] == n[i - 1]) {i++;}” to avoid the duplicate. For example, the input is 2 2 2 3 4. Consider the helper function. The process is:

  • each.add(n[i]); --> add first 2 (index 0)
  • helper(res, new ArrayList<>(each), i + 1, n); --> go to recursion part, list each is <2 (index 0)>
  • while (i < n.length && n[i] == n[i - 1]) {i++;} --> after this, i == 3, add the element as in subset I

3. Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping:

'A' -> 1
'B' -> 2
...
'Z' -> 26

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,
Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12). The number of ways decoding "12" is 2.

思路:咋看一下,可以用穷举来解决,那么应该可以使用dp的思路来解决这道题目。确定了是用dp之后,需要确定的数组dp[i]的索引和值的意义,以及是自底向上还是自顶向下。dp[i]代表的是给定的字符串message长度为i时总共有多少种decode方法,这里有一点要注意的是message中可能存在的0。对于这题的dp思路,从左向右,也就是从低向上,先来赋初值。dp[0]=1代表message是空时,encode way只有一种,对于dp[1],即message中的第一个值,如果是0的话decode way的个数是0,否则为1。接下来怎么dp才是难点,对于message中的位置i, 如果这个位置上的数字是1到9的那么这个数字和dp[i-1]是一种组合,如果这个数字和前一个构成的数在10到26之间,那么这两个数字和dp[i-2]又能构成一种decode way。

import java.util.*;

public class LeetCode{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String message=sc.nextLine();
System.out.println(numDecodings(message));
} static int numDecodings(String s){
if(s==null || s.length()==0) return 0;
int n=s.length();
int[] dp=new int[n+1];
dp[0]=1;
dp[1]=s.charAt(0)!='0'? 1:0;
for(int i=2; i<=n; i++){
int first=Integer.parseInt(s.substring(i-1,i));
int second=Integer.parseInt(s.substring(i-2,i));
if(first>=1&&first<=9){
dp[i]=dp[i-1];
}
if(second>=10&&second<=26){
dp[i]+=dp[i-2]; // 要将两种情况计和
}
}
return dp[n];
}
}

LeetCode解题报告—— Word Search & Subsets II & Decode Ways的更多相关文章

  1. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

  2. leetcode 解题报告 Word Ladder II

    题目不多说了.见https://oj.leetcode.com/problems/word-ladder-ii/ 这一题我反复修改了两天半.尝试过各种思路,总是报TLE.终于知道这一题为什么是leet ...

  3. LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal

    1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...

  4. leetcode 解题报告 Word Break

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  5. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  6. [LeetCode] 95. Unique Binary Search Trees II(给定一个数字n,返回所有二叉搜索树) ☆☆☆

    Unique Binary Search Trees II leetcode java [LeetCode]Unique Binary Search Trees II 异构二叉查找树II Unique ...

  7. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  8. LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal

    1. Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that ...

  9. 【leetcode】212. Word Search II

    Given an m x n board of characters and a list of strings words, return all words on the board. Each ...

随机推荐

  1. mac, xcode 6.1 安装command line tools 支持,autoconf,automake等

    以下软件包 都去我的环境库找到 1 先安装 tcl库 2 安装macports /opt/local/bin/port 一般装到这里 安装autoconf时提示: Warning: The Xcode ...

  2. Hcharts和Echarts----制作报表的工具

    Hcharts官网:https://www.hcharts.cn/Hcharts API文档:https://api.hcharts.cn/highcharts Echarts官网:http://ec ...

  3. oracle数据库解锁

    当我们修改数据库时用for update 或者使用rowId修改后,对表进行了锁定,由于某种原因没有对他进行关闭,我们需要关闭 select b.username,b.sid,b.serial#,lo ...

  4. RGB向yuv的转化最优算法,快得让你吃惊!

    朋友曾经给我推荐了一个有关代码优化的pdf文档<让你的软件飞起来>,看完之后,感受颇深.为了推广其,同时也为了自己加深印象,故将其总结为word文档.下面就是其的详细内容总结,希望能于己于 ...

  5. 一维的Haar小波变换

    小波变换的基本思想是用一组小波函数或者基函数表示一个函数或者信号,例如图像信号.为了理解什么是小波变换,下面用一个具体的例子来说明小波变换的过程. 1. 求有限信号的均值和差值 [例] 假设有一幅分辨 ...

  6. Anaconda创建python(2.7/3.6)的虚拟环境后需要添加ipykernel

    今天在工作的过程中遇到这样一个问题:安装完Anaconda利用conda创建了虚拟环境,但是启动jupyter notebook之后却找不到虚拟环境中的python kernel.后来上网找到了解决办 ...

  7. Java设计模式の观察者模式(推拉模型)

    目录: 一.观察者定义 二.观察者模式的结构(推模式实现) 三.推模型和拉模型(拉模式实现) 四.JAVA提供的对观察者模式的支持 五.使用JAVA对观察者模式的支持(自带推模式实现实例) 一.观察者 ...

  8. ES6中函数的扩展

    一.设置默认参数 ES6之前,给函数设置默认参数是这样做的: function fn(a) { if(typeof y === undefined){ a = a || 'hello'; } cons ...

  9. EF数据更新时候异常情况一

    在不熟练EF的时候有时更新数据时候会报以下异常: 错误原因:此时操作的实体不是从数据库里获取的.而是自己new出来的实体然后赋值的.EF此时的存储池中已经有了这个实体,在new一个对象ID相同就不能共 ...

  10. UVA 1363 Joseph's Problem

    https://vjudge.net/problem/UVA-1363 n 题意:求 Σ  k%i i=1 除法分块 如果 k/i==k/(i+1)=p 那么 k%(i+1)=k-(i+1)*p= k ...