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 ...
随机推荐
- 【bzoj1726】Roadblocks
1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1578 Solved: 795[ ...
- 洛谷 P4549 【模板】裴蜀定理
https://www.luogu.org/problemnew/show/P4549 (1)证明方程ax+by=gcd(a,b)(a,b为常数;a>0,b>0;a,b,x,y为整数)有解 ...
- 自己写的Grid组件,第二版
大体没什么变化,主要是添加了一个方法,getSelectedItems(),返回当前选中的数据项. (function ($) { $.fn.GridView = function (setting) ...
- 最耗资源的10条sql
----当前最耗资源的10个cpu select * from (select address,hash_value, round(cpu_time/1000000) cpu_time_s, roun ...
- Unity加载AssetBundle的方法
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; usin ...
- 03.Javascript——入门一些方法记录之Map和Set
JavaScript的默认对象表示方式{}可以视为其他语言中的Map或Dictionary的数据结构,即一组键值对. 但是JavaScript的对象有个小问题,就是键必须是字符串.但实际上Number ...
- hdu4578Transformation(线段树多个lz标记)
这里以3次方来举例讲一下这题的做法,其它维类似. 如果要求某一个值的3次方那么sum = t^3,设t = x+y.那也就是sum = (x+y)^3. 假如我让每个数都加z t = x+y+z,我可 ...
- 头部和信号栏一个颜色appcloud
<header id="header" > <ul > <li class="active" onclick="api. ...
- 随机不重复的取数组元素,并赋值给div使用
function pos(){ var items = $('.starone'); items.each(function () { var rand = getRandom(); $(this). ...
- HDU 5469 Antonidas (树形DP,暴力)
题意: 给一棵n节点的树图,每个点都是一个小写字母,要求找到两个点(a,b),从a->b的路径上形成了一个字符串为s.给出s,问是否存在这样的点对. 思路: 考虑一个点,要么从该点出发,要么在该 ...