leetcode中的Container With Most Water(C语言)
来源:互联网 发布:每周开户数据 编辑:程序博客网 时间:2024/05/21 02:53
【题目】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.
给定n个非负整数a1,a2,...,an,其中每个表示坐标(i,ai)处的点。 绘制n条垂直线,使得线i的两个端点在(i,ai)和(i,0)。 找到两条线,其与x轴一起形成容器,使得容器包含最多的水。
【解释】the most water equal to the biggest area.
【解题方式】(1)利用两个for循环,时间复杂度为O(n*n)
for(i=0;i<heightSize-1;i++){ for(j=1;j<heightSize;j++){ }}(2)已知求最大面积,即长宽的改变确定最终面积,先保证max宽,然后确定max面积。从两端向中间靠近,改变小的数值(即左边小,指针右移,右边小,指针左移),直到到达中点
/*Most Water equal to the biggest area;code by Roy.*/int maxArea(int* height, int heightSize) { int *max = NULL; int numb = 0,count = 0; int i = 0,j = 0; int n = heightSize; max = (int*)malloc(sizeof(int)*1); *max = 0; for(i = 0,j = n-1;i < j;){ if(*(height+i)>*(height+j)){ numb = *(height+j)*(j-i); *max = *max>numb?*max:numb; j--; } else{ numb = *(height+i)*(j-i); *max = *max>numb?*max:numb; i++; } } return *max;}
1 0
- leetcode中的Container With Most Water(C语言)
- Leetcode c语言-Container With Most Water
- LeetCode-11-Container With Most Water(C语言实现)
- [C++]LeetCode: 39 Container With Most Water
- [leetcode-11]container with most water(C)
- LeetCode刷题(C++)——Container With Most Water
- 【LeetCode算法练习(C++)】Container With Most Water
- [LeetCode]Container With Most Water(贪心)
- LeetCode(11) Container With Most Water
- (Leetcode)11. Container With Most Water
- LeetCode (11)Container With Most Water
- LeetCode: Container With Most Water
- [LeetCode]Container With Most Water
- LeetCode Container With Most Water
- LeetCode: Container With Most Water
- [Leetcode] Container With Most Water
- Leetcode: Container With Most Water
- [LeetCode] Container With Most Water
- iOS 两应用之间相互跳转
- 非阻塞,et模型 epoll
- Python pandas 0.19.1 Indexing and Selecting Data文档翻译
- 状态模式案例分析
- python搭建本地服务器
- leetcode中的Container With Most Water(C语言)
- [FUNC]AHK获取当前选中的文件路径
- HeadFirst 命令模式 风扇撤回课本实现错误代码及利用栈的解决办法
- nginx访问量统计
- jQuery轮播图学习
- Arduino Uno 驱动OLED进阶 显示中英文字
- 区间k大数查询
- java之旅之类和对象
- Swift如何判断字符串的语言