解题思路:

因为这个矩阵是有序的,所以从右上角开始查找。这样的话,如果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. Mass Change Queries Codeforces - 911G

    https://codeforces.com/contest/911/problem/G 没想到线段树合并还能这么搞.. 对每个权值建一个线段树(动态开点),如果权值为k的线段树上第i位为1,那么表示 ...

  2. 094 Binary Tree Inorder Traversal 中序遍历二叉树

    给定一个二叉树,返回其中序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [1,3,2].说明: 递归算法很简单,你可以通过迭代算法完成吗?详见 ...

  3. RS485的自动发送与布线

    布线http://blog.sina.com.cn/s/blog_729a492301019owo.html 自动收发电路:485注意控制端电平问题(3.3/5V)

  4. 一次Socket通信联想到的Zookeeper源码实现

    最近做一个短信平台,要接入短信网关平台,网关平台使用C,短信平台属于公司内部对外应用,使用Java开发,两边通信采用TCP/IP协议

  5. UEditor的KityFormula在IIS中部署,显示不了的解决方案

    在此,首先感谢我的同事,找到了问题所在. 因Web项目中需要有输入公式的功能(高等数学中需要),普通公式插件无法满足,所以找了KityFormula这款插件. 看了下里面的公式,在数学方面确实比较全面 ...

  6. AJPFX分享java排序之希尔排序

    (1)基本思想:算法先将要排序的一组数按某个增量d(n/2,n为要排序数的个数)分成若干组,每组中记录的下标相差d.对每组中全部元素进行直接插入排序,然后再用一个较小的增量(d/2)对它进行分组,在每 ...

  7. spring cloud各种超时时间设置

    如果是zuul(网关)的超时时间需要设置zuul.hystrix.ribbon等三部分: #zuul超时设置#默认1000zuul.host.socket-timeout-millis=2000#默认 ...

  8. python经典一百道习题(转自奶酪博客)

    无论学习哪门计算机语言,只要把100例中绝大部分题目都做一遍,就基本掌握该语言的语法了. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #Filena ...

  9. 关于HTML5手机端页面缩放的问题

    通常在写HTML5手机端页面的时候,我们会发现页面所显示元素的比例不正确,那此时我们需要添加的就是: <meta name="viewport" content=" ...

  10. Glide图片框架

    //加载圆形图片Glide.with(this).load(WSCAppStatic.WEB_KEFU_PHOTO_URL+ "?usercode=8120000315") .as ...