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. int(“1.6”),int(1.6)输出结果?

    >>> int("1.6") Traceback (most recent call last): File , in <module> ValueE ...

  2. hive on tez

    hive运行模式 hive on mapreduce 离线计算(默认) hive on tez  YARN之上支持DAG作业的计算框架 hive on spark 内存计算 hive on tez T ...

  3. 了解有关 in_memory 工作空间的详细信息

    ArcGIS 提供了一个可写入输出要素类和表的内存工作空间.作为将地理处理输出写入磁盘上的某个位置或网络位置的备选方案,可将输出写入内存工作空间中.通常,将数据写入内存工作空间要明显快于写入其他格式( ...

  4. What are the benefits to using anonymous functions instead of named functions for callbacks and parameters in JavaScript event code?

     What are the benefits to using anonymous functions instead of named functions for callbacks and par ...

  5. IfcWallStandardCase

    ENTITY IfcWallStandardCase SUBTYPE OF (IfcWall); WHERE HasMaterialLayerSetUsage : SIZEOF (QUERY(temp ...

  6. maven项目如何从私服nexus中下载依赖包

    maven项目如何从私服nexus中下载依赖包   解决方法: 1.打开maven的config目录中settings.xml文件 2.在<profile></profiles> ...

  7. Linux (Ubuntu)提示ifconfig:找不到命令

    使用命令: sudo apt install net-tools 安装一下net-tools

  8. spark实现wordcount

    spark-shell --master yarnsc:val lineRDD=sc.textFile("/test/input/test")lineRDD.collect().f ...

  9. Ext.net中Combobox如何绑定数据库中的值

    ];      ];      " />       </Items> </ext:ComboBox>

  10. C#RSA加密解密(对接PHP)

    上篇文章中写的RSA加密是针对C#的,现在外部调用的是PHP,我们平常见到的RSA无论公钥和私钥都是一长串数字,很显然C#生成的XML不是通用的加密.如果外部调用需要处理一下. 一.首先可以去网上找一 ...