博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Word Search
阅读量:4677 次
发布时间:2019-06-09

本文共 1776 字,大约阅读时间需要 5 分钟。

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.

Example:

board =[  ['A','B','C','E'],  ['S','F','C','S'],  ['A','D','E','E']]Given word = "ABCCED", return true.Given word = "SEE", return true.Given word = "ABCB", return false.

code1

class Solution{public:    bool exist(vector
>& board, string word) { if(board.empty()||word.empty()||board.size()*board.at(0).size()
> flag(board.size(),vector
(board.at(0).size(),false)); for(int i=0;i
> &flag,const vector
> &board,const string &word) { if(len==word.length()) return true; if(flag.at(x).at(y)) return false; flag.at(x).at(y)=true; for(int *coor:coordinate) { int _x=coor[0]+x; int _y=coor[1]+y; if(_x>=0&&_x
=0&&_y

code2

class Solution{public:    bool exist(vector
>& board, string word) { if(board.empty()||word.empty()||board.size()*board.at(0).size()
> &board,const char *word) { if(x<0|x>=board.size()||y<0||y>=board.at(0).size()||board.at(x).at(y)=='\0'||*word!=board.at(x).at(y)) return false; if(*(word+1)=='\0') return true; char c=board.at(x).at(y); board.at(x).at(y)='\0'; if(exist_core(x+1,y,board,word+1)||exist_core(x-1,y,board,word+1)|| exist_core(x,y+1,board,word+1)||exist_core(x,y-1,board,word+1)) return true; board.at(x).at(y)=c; return false; }};

 

转载于:https://www.cnblogs.com/tianzeng/p/10902906.html

你可能感兴趣的文章
this关键字的构造方法的使用
查看>>
Spring的数据库操作---- Spring框架对JDBC的整合---- Spring的数据库操作
查看>>
2016/12/14---- C3P0
查看>>
python tkinter组件学习
查看>>
django缓存
查看>>
winform中真正的透明label
查看>>
(Dynamic Proxy)动态代理模式的Java实现
查看>>
sql三大范式
查看>>
关于TP5模板输出时间戳问题--A non well formed numeric value encountered
查看>>
js延迟加载
查看>>
如何在win 2008 server和win 7上add web site
查看>>
[Selenium]如何实现上传本地文件
查看>>
★不评价别人的生活,是一个人最基本的修养
查看>>
centos 7 下vnc弹出窗口太小解决方法
查看>>
SpringCloud Feign的分析
查看>>
使用MD5WithRSA来签名和验签(.NET)
查看>>
异常:This application has no explicit mapping for /error, so you are seeing this as a fallback.
查看>>
使用gifplayer操作gif的方法
查看>>
利用SOAtest建立自动化测试验证网站是否成功加载
查看>>
一些鲜为人知却非常实用的数据结构 - Haippy
查看>>