解题思路:

因为这个矩阵是有序的,所以从右上角开始查找。这样的话,如果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. 110 Balanced Binary Tree 平衡二叉树

    给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过 1.案例 1:给出二叉树 [3,9,20,null,null,15,7] ...

  2. arcgis mdb 数据中的shp 如何合并一起

    如上操作  一直往下就可以啦 选择数据源 就可以了,然后就可以load  其他数据啦   ,坐标系要一直

  3. Jquery测试纠错笔记

    一. 解析: 获取元素范围大小顺序依次为: $(#one).siblings("div")>$("#one~div")>$("#one + ...

  4. Aop第一节

    什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入 ...

  5. Spring Boot运行原理

    概述 本文主要写了下Spring Boot运行原理,还有一个小例子. Spring4.x提供了基于条件来配置Bean的能力,而Spring Boot的实现也是基于这一原理的. Spring Boot关 ...

  6. bootstrap输入框组、导航和导航条

    输入框组(input groups) 避免使用select  支持不好,使用输入框组 尺寸根据  input-group-lg    input-group-sm来选择   <div class ...

  7. DSO的接口文档[转]

    本文从别处转来: (开发环境)使用前先注册一下DSOFramer.ocx 操作:将DSOFramer.ocx复制到C:\windows\system32目录下, 开始->运行->regsv ...

  8. uvm_reg——寄存器模型(三)

    uvm_reg 是uvm_reg_field , 包含所有uvm_reg_field 所有的函数.

  9. MIPS程序设计实例

    第一题:用系统功能调用实现简单输入输出 题目要求 利用系统功能调用从键盘输入,转换后在屏幕上显示,具体要求如下: 1.如果输入的是字母(A~Z,区分大小写)或数字(0~9),则将其转换成对应的英文单词 ...

  10. 树形dp——覆盖所有边的最少费用(Protecting Zonk)

    一.问题描述 有一个n(n<=10000)个节点的无根树.有两种装置A,B,每种都有无限多个. 1.在某个节点X使用A装置需要C1(C1<=1000)的花费,并且此时与节点X相连的边都被覆 ...