Leetcode Week1
来源:互联网 发布:布道者软件 编辑:程序博客网 时间:2024/06/10 17:36
/*
1.将Z型字符串转换成可读类型
输入 P A H N
A P L S I I G
Y I R
输出 PAHNAPLSIIGYIR
例子:convert("PAYPALISHIRING", 3) 输出 "PAHNAPLSIIGYIR".*/
方法2:
方法2:
1.将Z型字符串转换成可读类型
输入 P A H N
A P L S I I G
Y I R
输出 PAHNAPLSIIGYIR
例子:convert("PAYPALISHIRING", 3) 输出 "PAHNAPLSIIGYIR".*/
/*
方法1:
class Solution {public: string res; string convert(string s, int numRows) { int n = numRows; if (n<=1) return s; int unit = 2*n-2; int num = s.size() / unit; for(int i=0;i<n;++i){ for(int j=0;j<=num;++j){ if(j*unit+i<s.size() && (i==0 || i==n-1)){ res.push_back(s[j*unit+i]); }else{ if(j*unit+i < s.size()) res.push_back(s[j*unit+i]); if(j*unit+unit-i<s.size()) res.push_back(s[j*unit+unit-i]); } } } return res; }};
方法2:
class Solution {public: string res; string convert(string s, int numRows) { if (numRows <= 1) return s; string res; for(int i=0;i<numRows;++i){ res += convertline(i,numRows,s,i!=numRows-1); } return res; } string convertline(int num,int nRows,string s, bool downstairs){ string str; int cur = num; while(cur<s.size()){ str.push_back(s[cur]); if (downstairs){ cur += 2*(nRows-1-cur%(nRows-1)); }else{ cur += 2*(nRows-1-cur%(nRows-1)); } }; return str; } };
/*
2.转换数字的顺序,reverse
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
*/
方法1:适合数字较小的
class Solution {public: int reverse(int x) { char str[1000000]; sprintf(str,"%d",x); int len = strlen(str); vector<int> res; int result = 0; if (x>=0){ for (int i=len-1;i>=0;--i){ int a = x/(pow(10,i)); x = x-a*pow(10,i); res.push_back(a); } for (int j=res.size()-1;j>=0;--j){ result += res[j]*pow(10,j); } return result; }else { int abs_x = -x; for (int i=len-2;i>=0;--i){ int a = abs_x/(pow(10,i)); abs_x = abs_x-a*pow(10,i); res.push_back(a); } for (int j=res.size()-1;j>=0;--j){ result += res[j]*pow(10,j); } return -result; } }};
方法2:
class Solution {public: int reverse(int x) { bool reverse = false; if(x<0){ reverse = true; x = -x; } int result = 0; while(x>0){ int temp = x%10; if (INT_MAX/10 < result) return 0; if (INT_MAX/10 == result && INT_MAX%10<temp) return 0; result *= 10; result += temp; x /= 10; } if(reverse){ return -result; }else{ return result; } }};
0 0
- leetcode week1
- Leetcode Week1
- LeetCode题解 week1
- leetcode-month1-week1
- week1
- week1
- week1
- week1
- week1
- week1
- Leetcode: 70. Climbing Stairs(Week1, Easy)
- Leetcode:93. Restore IP Addresses(Week1, Medium)
- week1-leetcode #1-Two Sum[Easy]
- 算法设计Week1 LeetCode Algorithms Problem #1 Two Sum
- 算法设计Week1 LeetCode Algorithms Problem #2 Add Two Numbers
- test Week1
- 密码学 week1
- Cryptography-week1
- 常用sql语句总结-oracle
- 461. Hamming Distance
- ida postive-value sp导致不能使用hexray f5转c语言的解决办法
- 【Python】Ceph的python接口
- Xcode安装包(7.2、7.3、8.0、8.1、8.2、8.2.1)
- Leetcode Week1
- DIY个人智能家庭网关——python篇之推送门磁报警信息到手机
- unity聊天室服务器端
- 【Hadoop2.7.0、Zookeeper3.4.6、JDK1.7】搭建完全分布式的hadoop,HA部署安装,自动备援
- c++
- UVa 12096 The SetStack Computer 【STL】【stack】
- rt2860v2源码分析三
- unity客户端
- ASP.NET MVC中使用Log4Net