ARTS_week9

A

LeetCode:
35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.
Example 1:

Input: [1,3,5,6], 5
Output: 2

Example 2:

Input: [1,3,5,6], 2
Output: 1

Example 3:

Input: [1,3,5,6], 7
Output: 4

Example 4:

Input: [1,3,5,6], 0
Output: 0

分析:本周题目较简单,可以直接暴力求解

我的代码如下:

class Solution {
public:
    int searchInsert(vector& nums, int target) {
        if(target <= *(nums.begin()) || nums.empty()) {return 0;}
        if(target > *(nums.end() - 1)) {return nums.size();}
        int i;
        for(i = 0; i != nums.size(); i++) {
            if(target >= nums[i] && target <= nums[i + 1]) {break;}
        }
        return (i + 1);
    }
};

运行结果:
Runtime:8 ms, faster than 94.80% of C++ online submissions for Search Insert Position.
Memory Usage: 8.9 MB, less than 79.87% of C++ online submissions for Search Insert Position.

但此题若使用归并排序会大大提高性能:

展示他人代码:

class Solution {
public:
    int searchInsert(vector& nums, int target) {
        int l=0,h=nums.size()-1;
        int indx=0;
        while(l<=h)
        {
            int mid=(l+h)/2;
            if(nums[mid]==target)
            {
                indx=mid;
                break;
            }
            else if(nums[mid]target)
                h=mid-1;
        }
       return indx; 
    }
};

R

The Mood Ring of Algorithms Could Zap Your Brain to Help You Feel Better

  • 主要内容:利用算法感知大脑信号,将来可能有希望用于大脑治疗与“优化”。
  • 构想:在大脑中植入电极,监测大脑脑电波变化情况,并在需要时对大脑进行电刺激——但该技术暂未实现。
  • 进展:南加州大学维特比工程学院的电气工程师Maryam Shanechi在一次会议之中介绍了该项目的进展:她和她的团队已经开发出了一种与七个人情绪变化相关的算法;现在,他们正在研究如何通过刺激大脑来影响情绪变化。他们将电刺激治疗方法的决策权交给算法处理,并称其为“闭环系统”。七名志愿者的大脑已被植入电极,闭环系统持续监测大脑活动,并将电刺激治疗方法用于治疗癫痫的发作。现已开发了一个能预测何种电刺激能影响情绪的数学模型。
  • 特点:个性化较强,需要录入用户数据以训练模型。
  • 猜想:该项技术在未来或许不仅能用于大脑疾病的治疗,还可能大大提升人类大脑的活力,提高人类对大脑的利用程度。

T

最近的离散数学开始讲图论,于是去了解了一些有关图论的知识。
图主要用于求解满足特殊条件的路径。
离散数学中,图可以矩阵的方式表示,使得在计算机中,能以矩阵的方式处理图。

  • 遍历图方法:
    (1)深度优先(一条路走到黑,记忆量小);
    (2)广度优先(浅尝则止,记忆量大)。
  • 最短路径算法:
    (1)Dijkstra 算法(权值为正,某点到其他点)
    (2)Floyd 算法(任意两点)
  • 最小生成树(暂做了解)
    (1)Prim 算法
    (2)Kruskal 算法
    数据结构与算法 - 图论

S

  • 本周实验课布置了一个模拟ATM系统,在书写过程中,发现了自身存在的一个问题:模块划分不明确,模块之间独立性不够强。
    具体表现:
    (1)类的区分不明,哪个类该实现怎样的功能考虑不周;
    (2)函数功能区分混乱,哪个函数该在哪里、由哪个类实现;
    (3)在控制函数参数的传递时容易出错。
    参考:模块划分

你可能感兴趣的:(ARTS_week9)