ARTS_week6

A

LeetCode:
11. Container With Most Water __Medium
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.

The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example:

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

我的代码如下:

class Solution {
 public:
  int maxArea(vector& height) {
    int temp, max_area = 0;
    vector::iterator i, j;
    for (i = height.begin(), j = height.end() - 1; i != j;) {
        temp = (j - i) * min(*i, *j);
        max_area = max(max_area, temp);
        if(min(*i, *j) == *i){i++;}
        else{j--;}
    }
    return max_area;
  }
};

开始时的思路很简单,暴力求解:

class Solution {
 public:
  int maxArea(vector& height) {
    int temp, max_area = 0;
    vector::iterator i, j;
    for (i = height.begin(); i != height.end(); i++) {
      for (j = i + 1; j != height.end(); j++) {
        temp = (j - i) * min(*i, *j);
        max_area = max(temp, max_area);
      }
    }
    return max_area;
  }
};

但是很遗憾,最后的两组数据未通过,最终还是显示Time Limit Exceeded
由于容器容量取决于两板中高度较小者,因此可以考虑两个指针分别从数组两端出发,比较两指针指向高度的大小,将指向较小高度的指针往中间移动,如此仅需遍历一次数组,此法明显地比暴力求解快得多。


R

Scientists create decoder to turn brain activity into speech
本文主要讲述使用解码器将大脑活动转化为语音的内容。
研究人员将大脑的“动作”与声音结合,将大脑的电信号转化为声音动作,再将声音动作转化为语音。利用声音动作与语音之间的大型的数据库,训练机器,让它学习算法,最终能够使大脑的电信号转化为语音。其能将一部分声音精确转化,但还有一部分较模糊。但是它能够解码出未经训练的语句,并可以在人与人之间翻译。这些对于无法说话的人群具有重大意义。


T

  1. 关于输入的判断:
    例如,当输入的值定义为整数,需要判断其类型是否正确,应使用一些cin中的函数。
    例:
#include 
#include 

using std::cin;
using std::cout;
using std::endl;

int main() {
int input;
while (true) {
    cin >> input;
    if (cin.fail()) {
      cout << "wrong" << endl;
      cin.clear();  //将错误状态更改为有效状态
      cin.sync();  //清除缓冲区中的未读信息
    } else {
      cout << "right" << endl;
    }
    cout << "over" << endl;
  }

其中使用的cin流控制函数有:
cin.fail():读写过程中出错或格式错误,返回true
cin.clear():将错误状态更改为有效状态,重置标志,本处即重置cin.fail()的值;
cin.sync():清除缓冲区中的未读信息,本处即把未读取的类型错误的值清除;
参照文章:
1) C++之cin.eof, cin.bad, cin.good, cin.fail, cin.clear
2) cin流输入控制(clear(),sync(),flush(),ignore())

  1. 关于struct _finddata_t——查找文件
    例:
void dir(string path) {
  long hFile = 0;
  struct _finddata_t fileInfo;
  string pathName; // 遍历文件路径名
  // 代表要遍历所有的类型
  if ((hFile = _findfirst(pathName.assign(path).append("\\*").c_str(),
                          &fileInfo)) == -1) {
    return;
  }
  cout << setiosflags(ios::left) << setw(24) << "文件名" << setw(7) << "Normal"
       << setw(7) << "Read" << setw(7) << "Hid" << setw(7) << "Sys" << setw(7)
       << "Dir" << setw(7) << "Arc" << setw(15) << "文件大小" << setw(20)
       << "       创建时间" << endl
       << endl;
  do {
    /* File attribute constants for _findfirst() */
    //  #define _A_NORMAL       0x00    /* Normal file - No read/write
    //  restrictions */ #define _A_RDONLY       0x01    /* Read only file */
    //  #define _A_HIDDEN       0x02    /* Hidden file */
    //  #define _A_SYSTEM       0x04    /* System file */
    //  #define _A_SUBDIR       0x10    /* Subdirectory */
    //  #define _A_ARCH         0x20    /* Archive file */

    //打印文件的属性
    cout << setiosflags(ios::left) << setw(24) << fileInfo.name << setw(7)
         << (fileInfo.attrib & _A_NORMAL ? " Y" : " N") << setw(7)
         << (fileInfo.attrib & _A_RDONLY ? " Y" : " N") << setw(7)
         << (fileInfo.attrib & _A_HIDDEN ? " Y" : " N") << setw(7)
         << (fileInfo.attrib & _A_SYSTEM ? " Y" : " N") << setw(7)
         << (fileInfo.attrib & _A_SUBDIR ? " Y" : " N") << setw(7)
         << (fileInfo.attrib & _A_ARCH ? " Y" : " N") << setw(15)
         << fileInfo.size << setw(20) << ctime(&fileInfo.time_create) << endl;
  } while (_findnext(hFile, &fileInfo) == 0);
  _findclose(hFile);  //关闭句柄
  return;
}

三件套:
long _findfirst( char *filespec, struct _finddata_t *fileinfo )
int _findnext( long handle, struct _finddata_t *fileinfo )
int _findclose( long handle )

  • _findfirst*filespec为文件路径字符串,可使用通配符,*fileinfo指向文件结构体(存放文件信息),返回一个句柄,失败返回-1;
  • _findnext:接受_findfirst返回的句柄,*fileinfo指向文件结构体,成功返回0,失败返回-1;
  • _findclose:接受_findfirst返回的句柄,成功返回0,失败返回-1。
    参考文章:
    _finddata_t结构体用法

S

分享:
OSCHINA


你可能感兴趣的:(ARTS_week6)