11. Container With Most Water

class Solution {
public:
int maxArea(vector<int>& height) {
int maxarea = 0;
int l = 0, r = height.size()-1;
if(height.size()<2) return maxarea; while(l<r){
maxarea = max(maxarea, min(height[l],height[r])*(r-l));
if(height[l]<height[r])
l++;
else
r--;
} return maxarea;
}
};
//Brute Force --Time Limit Exceed
/*class Solution {
public:
int maxArea(vector<int>& height) {
int maxarea = 0;
if(height.size()<2) return maxarea;
for(int i=0; i<height.size()-1; i++){
for(int j=i+1; j<height.size(); j++)
maxarea = max(maxarea, min(height[i],height[j])*(j-i));
}
return maxarea;
}
};*/

16. 3Sum Closest //最接近target的 三数和

class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
if(nums.size()<3) return 0;
if(nums.size() == 3) return nums[0]+nums[1]+nums[2];
sort(nums.begin(), nums.end());
//int diff = abs(nums[0]+nums[1]+nums[2]-target);
int diff = INT_MAX;
//int sum = 0, front, end, ret=diff;
int sum = 0, front, end, ret=0;
for(int i=0; i<nums.size(); i++){
front = i+1;
end = nums.size()-1;
while(front<end){
sum = nums[i]+nums[front]+nums[end];
if(sum == target) return target;
if(abs(sum-target)<diff){
diff = abs(sum-target);
ret = sum;
}
(sum > target) ? end-- : front++;
}
}
return ret;
}
};

49. Group Anagrams  

Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> mp;
for (string s : strs) { //auto s: strs also ok
string t = s;
sort(t.begin(), t.end());
mp[t].push_back(s);
}
vector<vector<string>> anagrams;
for (auto p : mp) {
anagrams.push_back(p.second);
}
return anagrams;
}
};

819. Most Common Word

Input:
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball" class Solution {
public:
string mostCommonWord(string paragraph, vector<string>& banned) { // Map out each banned word
// Go through each word, if it isn't in the banned list
// put it in another map and keep track of how many times it appears
// Keep two variables to maintain the mostCommonWord and how many times it appeared
// This prevents us from looping thorugh the map at the end to find the most common word map<string, int> bannedWords;
map<string, int> tracker; string mostCommonWord = "";
int mostCommonWordCount = 0; for(int x = 0; x < banned.size(); x++)
{
bannedWords[banned[x]] = 0;
} for(int x = 0; x < paragraph.length(); x++)
{
string temp = "";
while(x < paragraph.length() && paragraph[x] != ' ')
{
if(!isalpha(paragraph[x]))
{
break;
} temp += tolower(paragraph[x]);
x++;
} if(temp == " " || temp == "")
continue; if(bannedWords.count(temp) != 0)
continue;
else
{
tracker[temp]++; if(mostCommonWordCount < tracker[temp])
{
mostCommonWordCount = tracker[temp];
mostCommonWord = temp;
}
} } return mostCommonWord;
}
};

Question1???:

Input:
inputStr = awaglknagawunagwkwagl
num = 4 Output:
{wagl, aglk, glkn, lkna, knag, gawu, awun, wuna, unag, nagw, agwk, kwag}
Explanation:
Substrings in order are: wagl, aglk, glkn, lkna, knag, gawu, awun, wuna, unag, nagw, agwk, kwag, wagl
"wagl" is repeated twice, but is included in the output once.
class Solution {
public:
vector<string> numKLenSubstrNoRepeats(string S, int K) {
vector<string>ans;
int cnt = 0, ,i=0, j;
while(i < S.size() && j<k) {
if(S[i] == S[j]){
          j++;
       }else{
          i = i-j+1;
          j = 0;
       }
if (j == K){
         ans.push_back(S.substr(i-K, K));
         i = i-j+1;
         j = 0;
       }
}
return ans;
}
};  

数组排序

https://www.cnblogs.com/taotingkai/p/6214367.html

https://baike.baidu.com/item/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F%E7%AE%97%E6%B3%95/369842?fromtitle=%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F&fromid=2084344&fr=aladdin#3_5

/* 冒泡排序 */
/* 1. 从当前元素起,向后依次比较每一对相邻元素,若逆序则交换 */ (每次把最大值放后面)
/* 2. 对所有元素均重复以上步骤,直至最后一个元素 */
void bubbleSort (int arr[], int len) {
int temp;
int i, j;
for (i=0; i<len-1; i++) /* 外循环为排序趟数,len个数进行len-1趟 */
for (j=0; j<len-1-i; j++) { /* 内循环为每趟比较的次数,第i趟比较len-i次 */
if (arr[j] > arr[j+1]) { /* 相邻元素比较,若逆序则交换(升序为左大于右,降序反之) */
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
/* 每次选出最小的 */
void bubbleSort (int arr[], int len) {
int temp;
int i, j;
for (i=0; i<len-1; i++) /* 外循环为排序趟数,len个数进行len-1趟 */
for (j=i+1; j<len-1; j++) { /* 内循环为每趟比较的次数,第i趟比较len-i次 */
if (arr[i] > arr[j]) { /* 相邻元素比较,若逆序则交换(升序为左大于右,降序反之) */
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
/*快速排序*/
#include <iostream>
using namespace std;
void Qsort(int arr[], int low, int high){
if (high <= low) return;
int i = low;
int j = high + 1;
int key = arr[low];
while (true)
{
/*从左向右找比key大的值*/
while (arr[++i] < key)
{
if (i == high){
break;
}
}
/*从右向左找比key小的值*/
while (arr[--j] > key)
{
if (j == low){
break;
}
}
if (i >= j) break;
/*交换i,j对应的值*/
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/*中枢值与j对应值交换*/
int temp = arr[low];
arr[low] = arr[j];
arr[j] = temp;
Qsort(arr, low, j - 1);
Qsort(arr, j + 1, high);
} int main()
{
int a[] = {6, 2, 7, 3, 8, 9};
Qsort(a, 0, sizeof(a) / sizeof(a[0]) - 1);/*这里原文第三个参数要减1否则内存越界*/
for(int i = 0; i < sizeof(a) / sizeof(a[0]); i++)
{
cout << a[i] << "";
}
return 0;
}/*参考数据结构p274(清华大学出版社,严蔚敏)*/

求一个数组里面连续子集的和的最大值,比如[1,2,-4,7,8,-2,4]=>[7,8,-2,4]

思路:每次求出max(sub(i-1), arr[i]),看那个大取那个。

int arrMaxsub(int [] arr, int len){
  int maxsum = arr[0]; for(int i=1; i<len; i++){
if(maxsum+arr[i] <arr[i])
maxsum = arr[i];
else
maxsum = maxsum+arr[i];
}
return maxsum;
}

  

  

  

  

  

  

  

  

 

第6章:LeetCode--数组(冒泡排序、快速排序)的更多相关文章

  1. Java语言程序设计(基础篇) 第七章 一维数组

    第七章 一维数组 7.2 数组的基础知识 1.一旦数组被创建,它的大小是固定的.使用一个数组引用变量,通过下标来访问数组中的元素. 2.数组是用来存储数据的集合,但是,通常我们会发现把数组看作一个存储 ...

  2. perl5 第九章 关联数组/哈希表

    第九章 关联数组/哈希表 by flamephoenix 一.数组变量的限制二.定义三.访问关联数组的元素四.增加元素五.创建关联数组六.从数组变量复制到关联数组七.元素的增删八.列出数组的索引和值九 ...

  3. java 数组冒泡排序、转置(降序)

    1.java 数组冒泡排序 排序的基本原理(升序): 原始数据:  2 .1 .9 .0 .5 .3 .7 .6 .8: 第一次排序: 1  .2 .0 .5 .3 .7 .6 .8 .9 : 第二次 ...

  4. “全栈2019”Java第二十九章:数组详解(中篇)

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  5. Leetcode数组题*3

    目录 Leetcode数组题*3 66.加一 题目描述 思路分析 88.合并两个有序数组 题目描述 思路分析 167.两数之和Ⅱ-输入有序数组 题目描述 思路分析 Leetcode数组题*3 66.加 ...

  6. LeetCode 数组分割

    LeetCode 数组分割 LeetCode 数组怎么分割可以得到左右最大值的差值的最大 https://www.nowcoder.com/study/live/489/1/1 左右最值最大差 htt ...

  7. LeetCode数组中重复的数字

    LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...

  8. js数组冒泡排序,快速排序的原理以及实现

    冒泡排序: 随便从数组中拿一位数和后一位比较,如果是想从小到大排序,那么就把小的那一位放到前面,大的放在后面,简单来说就是交换它们的位置,如此反复的交换位置就可以得到排序的效果. var arr = ...

  9. js数组冒泡排序、快速排序、插入排序

    1.冒泡排序 //第一种 function bubblesort(ary){ for(var i=0;i<ary.length-1;i++){ for(var j=0;j<ary.leng ...

  10. 第3章 Java数组(上): 一维数组和二维数组

    3.数组及排序算法(2天) 3.1 数组的概述 2课时 3.2 一维数组的使用 3课时 3.3 多维数组的使用 3课时 3.4 数组中涉及到的常见算法 3课时 3.5 Arrays工具类的使用 3课时 ...

随机推荐

  1. C++11多线程std::thread创建方式

    //#include <cstdlib> //#include <cstdio> //#include <cstring> #include <string& ...

  2. oop 编程是什么?

    面向对象编程(Object Oriented Programming,OOP,面向对象程序设计)是一种计算机编程架构.OOP 的一条基本原则是计算机程序是由单个能够起到子程序作用的单元或对象组合而成.

  3. Jedis API操作redis数据库

    1.配置文件 classpath路径下,新建redis.properties配置文件 配置文件内容 # Redis settings redis.host=127.0.0.1 redis.port=6 ...

  4. (转)kafka 详解

    kafka入门:简介.使用场景.设计原理.主要配置及集群搭建(转) 问题导读: 1.zookeeper在kafka的作用是什么? 2.kafka中几乎不允许对消息进行"随机读写"的 ...

  5. tcp流式传输和udp数据报传输

    所有的书上都说, tcp是流式传输, 这是什么意思? 假设A给B通过TCP发了200字节, 然后又发了300字节, 此时B调用recv(设置预期接受1000个字节), 那么请问B实际接受到多少字节? ...

  6. javascript的正则表达式总结

    网上正则表达式的教程够多了,但由于javascript的历史比较悠久,也比较古老,因此有许多特性是不支持的.我们先从最简单地说起,文章所演示的正则基本都是perl方式. 元字符 ( [ { \ ^ $ ...

  7. JDK安装及Java环境变量配置

    1.JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html. 2.点击A ...

  8. 【源码】openresty 限流

    小结: 1.在连接环节计数,有清零环节 有3个参量 maxburst unit_delay https://github.com/openresty/lua-resty-limit-traffic/b ...

  9. python @classmethod

    写在前面 写博客的时候,我发现拖延症很严重,本来昨天要开始写的,结果东看看,西翻翻,啥也没落实下来.时间过去了,口袋里的收获却寥寥无几.讨厌这样的自己.我要戒掉这个不好的毛病. 拖延症的底层原因之一是 ...

  10. osg RTT 多相机-局部放大镜

    #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include<iostream> #include <osgV ...