【双层loop】题型总结
来源:互联网 发布:丰田车导航软件 编辑:程序博客网 时间:2024/04/30 05:59
Spiral Matrix
Total Accepted: 11102 Total Submissions: 53832My SubmissionsGiven a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]
You should return [1,2,3,6,9,8,7,4,5]
.
class Solution {public: vector<int> spiralOrder(vector<vector<int> > &matrix) { vector<int> res; int x=-1,y=0;/// @@巧妙设置起点 int n=matrix.size(); if(n==0) return res; int m=matrix[0].size(); while(m>0&&n>0){ //right for(int i=0;i<m;i++){ x+=1; res.push_back(matrix[y][x]); } n--; if(n==0||m==0) break;///@@error:here should have a break condition: n==0 or m==0 all will cause the loop stop for(int i=0;i<n;i++){ y+=1; res.push_back(matrix[y][x]); } m--; if(n==0||m==0) break; for(int i=0;i<m;i++){ x-=1; res.push_back(matrix[y][x]); } n--; if(n==0||m==0) break; for(int i=0;i<n;i++){ y-=1; res.push_back(matrix[y][x]); } m--; if(n==0||m==0) break; } return res; }};
0 0
- 【双层loop】题型总结
- 测试双层loop
- 阿里面试题型总结
- Leetcode Array题型总结
- Leetcode HashTable 题型总结
- Leetcode Greedy 题型总结
- Leetcode tree题型总结
- 面试常见题型总结
- 动态规划题型总结
- 贪心算法 题型总结
- 搜索题型总结
- ACM 题型算法分类总结
- 线段树基本题型总结
- oracle10g上机练习题型总结
- Leetcode Dynamic Programming 题型总结
- Leetcode Bit Manipulation 题型总结
- 【最短路】题型小总结
- Run Loop 总结
- Vi编辑器的使用方法
- [leetcode] Search Insert Position
- 算法学习 - 插入排序,希尔排序
- Spring中的applicationContext.xml与SpringMVC的xxx-servlet.xml的区别
- orcal的数据库编程plsql
- 【双层loop】题型总结
- C#socket传字节数组到flash socket接收 大小端(字节序)问题
- Vs2010 配置驱动开发环境
- I,P,B帧和PTS,DTS的关系
- R语言学习(3)-列表和数据框
- malloc 从哪里得到的内存空间
- opengl中VAO,VBO,IBO用法小结
- Java Web获取Web应用根路径(转)
- 黑马程序员--IOS多线程 GCD