CareerCup-PATTERN MATCHING

简介:

CareerCup是一本相当好Code面试的书,开始研读。

Description:

Consider what problems the algorithm is similar to, and figure out if you can modify the solution to develop an algorithm for this problem

Example:

A sorted array has been rotated so that the elements might appear in the order 3 4 5 6 7 1 2 How would you find the minimum element?

#include <iostream>  
using namespace std;  

int find(int* data, int low, int high)  
{  
    printf("%d->%d\n", low, high);  
    if(low == high)  
        return data[low];  
    if(data[low] < data[high])  
        return data[low];  
    int mid = (low + high)/2;  
    if(data[mid] >= data[low])  
        return find(data, mid+1, high);  
    else  
        return find(data, low, mid);  
};   

int main()  
{  
    int data[] = {3, 4, 5, 6, 7, 1, 2};  
    printf("%d", find(data, 0, sizeof(data)/sizeof(int)-1));  
    system("pause");   
};  

目录
相关文章
LeetCode 290. Word Pattern
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。 这里的遵循指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应模式。
49 0
LeetCode 290. Word Pattern
|
索引 Python
LeetCode 44. Wildcard Matching
给定输入字符串s和模式串(p),实现通配符模式匹配支持'?' 和'*'.
84 0
|
机器学习/深度学习 人工智能