【12】Roman to Integer 罗马数字转化成整数
//第一,如果当前数字是最后一个数字,或者之后的数字比它小的话,则加上当前数字
//第二,其他情况则减去这个数字
class Solution {
public:
int romanToInt(string s) {
int res = 0;
unordered_map m{{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
for (int i = 0; i < s.size(); ++i) {
int val = m[s[i]];
if (i == s.size() - 1 || m[s[i+1]] <= m[s[i]]) res += val;
else res -= val;
}
return res;
}
};
//第一,如果当前数字是最后一个数字,或者之后的数字比它小的话,则加上当前数字
//第二,其他情况则减去这个数字
class Solution {
public:
int romanToInt(string s) {
int res = 0;
unordered_map
for (int i = 0; i < s.size(); ++i) {
int val = m[s[i]];
if (i == s.size() - 1 || m[s[i+1]] <= m[s[i]]) res += val;
else res -= val;
}
return res;
}
};
[6]Zigzag Converesion
将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:
PAHN
A P L S I I G
YIR
之后从左往右,逐行读取字符:"PAHNAPLSIIGYIR"
实现一个将字符串进行指定行数变换的函数:
string convert(string s, int numRows);
示例 1:
输入: s = "PAYPALISHIRING", numRows = 3
输出: "PAHNAPLSIIGYIR"
示例 2:
输入: s = "PAYPALISHIRING", numRows = 4
输出: "PINALSIGYAHRPI"
解释:
P I N
AL S I G
Y AH R
P I
//Java解法
string convert(string s, int numRows)
{
if(numRows <= 1) return s;
string result = "";
int cycle = 2 * numRows - 2;
for(int i = 0; i < numRows; ++i)
{
for(int j = i; j < s.length(); j = j + cycle){
result = result + s[j];
int secondJ = (j - i) + cycle - i;
if(i != 0 && i != numRows-1 && secondJ < s.length())
result = result + s[secondJ];
}
}
return result;
}
将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:
PAHN
A P L S I I G
YIR
之后从左往右,逐行读取字符:"PAHNAPLSIIGYIR"
实现一个将字符串进行指定行数变换的函数:
string convert(string s, int numRows);
示例 1:
输入: s = "PAYPALISHIRING", numRows = 3
输出: "PAHNAPLSIIGYIR"
示例 2:
输入: s = "PAYPALISHIRING", numRows = 4
输出: "PINALSIGYAHRPI"
解释:
P I N
AL S I G
Y AH R
P I
//Java解法
string convert(string s, int numRows)
{
if(numRows <= 1) return s;
string result = "";
int cycle = 2 * numRows - 2;
for(int i = 0; i < numRows; ++i)
{
for(int j = i; j < s.length(); j = j + cycle){
result = result + s[j];
int secondJ = (j - i) + cycle - i;
if(i != 0 && i != numRows-1 && secondJ < s.length())
result = result + s[secondJ];
}
}
return result;
}
粉末高速钢切削丝锥,适用于超级双相不锈钢。
Powder Metal Cutting Taps,Suitable for Super Duplex stainless steel.
TiN-coated powder metal spiral-fluted cutting taps especially suitable for difficult to machine materials such as 304L & 316L stainless steel, 15-5PH & 17-4PH, Super Duplex 1.4410, Inconel 625, etc.
Powder Metal Cutting Taps,Suitable for Super Duplex stainless steel.
TiN-coated powder metal spiral-fluted cutting taps especially suitable for difficult to machine materials such as 304L & 316L stainless steel, 15-5PH & 17-4PH, Super Duplex 1.4410, Inconel 625, etc.
✋热门推荐