解题思路:

因为这个矩阵是有序的,所以从右上角开始查找。这样的话,如果target比matrix[row][col]小,那么就向左查找;如果比它大,就

向下查找。如果相等就找到了,如果碰到边界,就说明没有。需要注意的是,1)矩阵按行存储;2)测试用例中有空的情况[],

所以在进行查找之前,必须进行判断,否则为col赋初值时会报错。

bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.size() == 0 || matrix[0].size() == 0)
return false;
int row = 0;
int col = matrix[0].size() - 1;
while (row < matrix.size() && col > -1) {
if (target == matrix[row][col])
return true;
else if (target > matrix[row][col])
row ++;
else
col --;
}
return false;
}

解题思路:

这道题分明要我用分治法。。。考虑将字符串以运算符为界,分成左右两个子串,根据运算符计算加,减,乘,将结果防到result中。

在最底层,需要将数字放入。注意:1)substr(start, length),length不指定时是到结尾;2) atoi的输入是char*,所以需要用c_str将

string转为字符数组。

vector<int> diffWaysToCompute(string input) {
vector<int> result;
int i;
for (i = 0 ; i < input.length(); i++) {
if (input[i] == '+' || input[i] == '-' || input[i] == '*') {
vector<int> left = diffWaysToCompute(input.substr(0, i));
vector<int> right = diffWaysToCompute(input.substr(i + 1));
int j,k;
for (j = 0; j < left.size(); j++) {
for (k = 0; k < right.size(); k++) {
if (input[i] == '+') {
result.insert(result.end(), left[j] + right[k]);
} else if (input[i] == '-') {
result.insert(result.end(), left[j] - right[k]);
} else {
result.insert(result.end(), left[j] * right[k]);
}
}
}
}
}
if (result.empty() == true)
result.insert(result.end(), atoi(input.c_str()));
return result;
}

  

leetcode-3-basic-divide and conquer的更多相关文章

  1. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

    参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

  2. [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  3. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

  4. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  5. 算法与数据结构基础 - 分治法(Divide and Conquer)

    分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...

  6. leetcode面试准备:Divide Two Integers

    leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...

  7. [Leetcode] Binary search, Divide and conquer--240. Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. 算法上机题目mergesort,priority queue,Quicksort,divide and conquer

    1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...

  9. [LeetCode] 224. Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  10. [LeetCode] 227. Basic Calculator II 基本计算器 II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

随机推荐

  1. [题解](最短路)luogu_P5122 Fine Dining

    首先理解这里的美味值相当于给你更多时间让你经过这个草垛的, 也就是在经过草垛时可以给你的时间减少w[i],这样能否比最短路不慢 然而我们并不容易知道怎么走才是最好的,所以要想办法避免找这个方案 我们新 ...

  2. css-bootstrap

    CSS概览 基本的bootstrap包含三个文件,引入到html页面中 <link href="{% static 'css/bootstrap.min.css' %}" r ...

  3. Ubuntu下安装webstorm

    需要下载 WebStorm和 JDK # tar xvf WebStorm-10.0.1.tar.gz # tar xvf jdk-8u20-linux-x64.tar.gz 配置 /etc/prof ...

  4. 捕获异常try-catch-finally

    异常分类 try-carch-finally出现规则 return关键字的使用 finally中慎用return,虽然语法上没错,但是由于finally的强制执行,影响逻辑上需要return的值 pa ...

  5. 【bzoj3033】太鼓达人

    3033: 太鼓达人 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 521  Solved: 399[Submit][Status][Discuss] ...

  6. FFT与NTT的模板

    网上相关博客不少,这里给自己留个带点注释的模板,以后要是忘了作提醒用. 以洛谷3803多项式乘法裸题为例. FFT: #include <cstdio> #include <cmat ...

  7. 洛谷 P1169||bzoj1057 [ZJOI2007]棋盘制作

    洛谷P1169 bzoj1057 这个题目跟最大全0子矩阵是类似的.正方形的话,只要把任意极大子正方形(”极大“定义见后面的”论文“)当成把某个极大子矩形去掉一块变成正方形即可,容易解决. 解法1:看 ...

  8. DB2 错误 54001

    DB2 语句太长或者太复杂 SQLSTATE=54001 对数据库的参数的修改: db2 update db cfg for DB_NAME using STMTHEAP 4096 db2 updat ...

  9. Android 流量测试方法

    流量测试怎么测?下面总结了几个方法: 一.通过pid获取流量 > 1.获取应用的pid adb shell ps | grep packagename 2.通过pid获取该进程的流量信息 adb ...

  10. 修复Windows XP服务扩展视图显示空白

    在服务管理控制台(Services.msc)扩展视图显示服务的描述,也有启动或停止服务的链接.在某些系统中,扩展视图可能出现一片空白,如图所示: 这是因为没有注册 JScript.dll文件,要解决此 ...