leetcode-3-basic-divide and conquer

解题思路:
因为这个矩阵是有序的,所以从右上角开始查找。这样的话,如果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的更多相关文章
- [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...
- [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 ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
- [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 ...
- 算法与数据结构基础 - 分治法(Divide and Conquer)
分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...
- leetcode面试准备:Divide Two Integers
leetcode面试准备:Divide Two Integers 1 题目 Divide two integers without using multiplication, division and ...
- [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 ...
- 算法上机题目mergesort,priority queue,Quicksort,divide and conquer
1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...
- [LeetCode] 224. Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] 227. Basic Calculator II 基本计算器 II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
随机推荐
- AFHTTPSessionManager下载文件 及下载中 进度条处理,进度条处理需要特别注意,要加载NSRunLoop 中
1.下载文件 和进度条处理代码 - (void)timer:(NSTimer *)timer{ // 另一个View中 进度条progress属性赋值 _downloadView.progress = ...
- C#静态类、静态构造函数,类与结构体的比较
一.静态类 静态类是不能实例化的,我们直接使用它的属性与方法,静态类最大的特点就是共享. 探究 public static class StaticTestClass{ public stati ...
- 浅谈算法——AC自动机
在学习AC自动机之前,你需要两个前置知识:Trie树,KMP 首先我们需要明白,AC自动机是干什么的(用来自动AC的) 大家都知道KMP算法是求单字符串对单字符串的匹配问题的,那么多字符在单字符上匹配 ...
- 调用submit()方式提交表单
今天在看高级程序设计时看到的这样一段话: 在以调用submit()方法的形式提交表单时,不会触发submit事件 写了一个小例子做了下测试,的确如此: <form id="fm&quo ...
- react 父子传值
import React from 'react'; import ReactDOM from 'react-dom'; import $ from 'jquery'; //var $ = requi ...
- 获取文件的MD5码(C#)
using System; using System.IO; using System.Security.Cryptography; using System.Text; namespace Test ...
- linux下mysql中文乱码
登录mysql执行mysql> show variables like 'character%';发现编码有些不是utf-8 修改/etc/mysql/my.cnf,网上说的是/etc/my.c ...
- javascript 流程控制及函数
回顾 基本语法 在html的使用 <script></script> 注释 ///* */ 指令结束符 ;换行 输出 console.log()document.write() ...
- 从零开始利用vue-cli搭建简单音乐网站(七)
这几天完成了歌曲收藏功能,先看最后效果: 新注册用户:“newuser”,进入“我的音乐界面如下所示” 点击新建歌单,输入:“新歌单”,确认,如下: 目前还没有歌曲,打开音乐界面,点击收藏功能,如下, ...
- Hadoop2.6.2的Eclipse插件的使用
欢迎转载,且请注明出处,在文章页面明显位置给出原文连接. 本文链接:http://www.cnblogs.com/zdfjf/p/5178197.html 首先给出eclipse插件的下载地址:htt ...