problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::where_will_the_ball_fall::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static vector< int > findBall (vector< vector< int > > &grid)
 

详细描述

在文件 leetcode.h1343 行定义.

成员函数说明

◆ findBall()

vector< int > leetcode::where_will_the_ball_fall::Solution::findBall ( vector< vector< int > > &  grid)
static

在文件 leetcode.cpp3464 行定义.

3464 {
3465 const int m = grid.size();
3466 const int n = grid[0].size();
3467 vector<int> ans(n);
3468 for(int i = 0; i < n; i++) {
3469 int current_x = 0;
3470 int current_y = i;
3471 int dir = 1;// 0 - 左, 1 - 下, 2 - 右
3472 while(true) {
3473 if(current_x == m) {
3474 ans[i] = current_y;
3475 break;
3476 }
3477 if(current_x < 0 || current_y < 0 || current_y >= n) {
3478 ans[i] = -1;
3479 break;
3480 }
3481 if(dir == 0) {
3482 //左
3483 if(grid[current_x][current_y] == 1) {
3484 ans[i] = -1;
3485 break;
3486 }
3487 dir = 1;
3488 } else if(dir == 1) {
3489 //下
3490 if(grid[current_x][current_y] == 1) {
3491 dir = 2;
3492 } else {
3493 dir = 0;
3494 }
3495 } else {
3496 //右
3497 if(grid[current_x][current_y] == 1) {
3498 dir = 1;
3499 } else {
3500 ans[i] = -1;
3501 break;
3502 }
3503 }
3504 if(dir == 0) {
3505 //左
3506 current_y--;
3507 } else if(dir == 1) {
3508 //下
3509 current_x++;
3510 } else {
3511 //右
3512 current_y++;
3513 }
3514 }
3515 }
3516 return ans;
3517 }

被这些函数引用 leetcode::where_will_the_ball_fall::TEST().


该类的文档由以下文件生成: