解题思路:

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

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

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

  1. bool searchMatrix(vector<vector<int>>& matrix, int target) {
  2. if (matrix.size() == 0 || matrix[0].size() == 0)
  3. return false;
  4. int row = 0;
  5. int col = matrix[0].size() - 1;
  6. while (row < matrix.size() && col > -1) {
  7. if (target == matrix[row][col])
  8. return true;
  9. else if (target > matrix[row][col])
  10. row ++;
  11. else
  12. col --;
  13. }
  14. return false;
  15. }

解题思路:

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

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

string转为字符数组。

  1. vector<int> diffWaysToCompute(string input) {
  2. vector<int> result;
  3. int i;
  4. for (i = 0 ; i < input.length(); i++) {
  5. if (input[i] == '+' || input[i] == '-' || input[i] == '*') {
  6. vector<int> left = diffWaysToCompute(input.substr(0, i));
  7. vector<int> right = diffWaysToCompute(input.substr(i + 1));
  8. int j,k;
  9. for (j = 0; j < left.size(); j++) {
  10. for (k = 0; k < right.size(); k++) {
  11. if (input[i] == '+') {
  12. result.insert(result.end(), left[j] + right[k]);
  13. } else if (input[i] == '-') {
  14. result.insert(result.end(), left[j] - right[k]);
  15. } else {
  16. result.insert(result.end(), left[j] * right[k]);
  17. }
  18. }
  19. }
  20. }
  21. }
  22. if (result.empty() == true)
  23. result.insert(result.end(), atoi(input.c_str()));
  24. return result;
  25. }

  

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. scrapy框架中Download Middleware用法

    scrapy框架中Download Middleware用法   Downloader Middleware处理的过程主要在调度器发送requests请求的时候以及网页将response结果返回给sp ...

  2. 如何解决netty发送消息截断问题

      在netty开发过程中我遇到过长的消息被分割成多个小消息的问题.如下图所示: 其实这两条消息应该是一条消息,它们两个才是一个完整的json字符串.查看代码原来是客户端与服务器端都没有考虑TCP粘包 ...

  3. 学习中对input()的一些总结(raw_input()与input())

  4. hdu4027Can you answer these queries?(线段树)

    链接 算是裸线段树了,因为没个数最多开63次 ,开到不能再看就标记.查询时,如果某段区间被标记直接返回结果,否则继续向儿子节点更新. 注意用——int64 注意L会大于R 这点我很纠结..您出题人故意 ...

  5. 状态模式和php实现

    状态模式: 允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类.其别名为状态对象(Objects for States),状态模式是一种对象行为型模式. 模式分析: 在很多情况下, ...

  6. xml文件解析和序列化

    转载:http://blog.csdn.net/liuhe688/article/details/6415593 XmlPullParser parser = Xml.newPullParser(); ...

  7. Selenium私房菜系列9 -- 我遇到的问题及解决问题的方法

    Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法

  8. java.lang.IllegalAccessException: Class XX can not access a member of class XXX with modifiers "private static"

    当前需求: 利用反射获取某一属性值运行结果:java.lang.IllegalAccessException: Class com.example.demo.test.Reflect can not ...

  9. tomcat配置 —— 各个目录的作用

    tomcat各目录(文件)作用 tomcat-7.0.50解压版,主目录一览: 我们可以看到主目录下有bin,conf,lib,logs,temp,webapps,work 7个文件夹,下面对他们分别 ...

  10. UWP中获取Encoding.Default

    Encoding.GetEncoding(0); 即可