请问为啥会多一个感叹号呢?
原题:图片;
代码:
char * alphabetBoardPath(char * target){
char board = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"};
int i,j;
int x,y;
static char p;
int k=0;
i=j=0;
while(*target!='\0'){
for(x=0;x<6;x++){
for(y=0;y<5;y++){
if(*target==board)
goto A;
}
}//寻找字母坐标;
A: ;
if(x>i){
while(x>i&&i<4){
p='D';
i++;
}
}
else if(x<i){
while(x<i){
p='U';
i--;
}
}
//行
if(y>j){
while(y>j){
p='R';
j++;
}
}
else if(y<j){
while(y<j){
p='L';
j--;
}
}
//列;
if(x>i){
p='D';
}
//z;
p='!';
i=x;
j=y;
target++;
}
return p;
}
不愧是C语言,^_^
执行用时:0 ms, 在所有 C 提交中击败了100.00% 的用户
内存消耗:5.3 MB, 在所有 C 提交中击败了100.00% 的用户
char * alphabetBoardPath(char * target){
static char result;
size_t index = 0;
ssize_t x = 0, y = 0;
for(size_t i = 0; target; ++i) {
size_t num = target - 'a';
ssize_t cx = num % 5;
ssize_t cy = num / 5;
char x_command, y_command;
if(cx > x) x_command = 'R';
else if(x > cx) x_command = 'L';
if(cy > y) y_command = 'D';
else if(y > cy) y_command = 'U';
ssize_t x_count = abs(cx - x);
ssize_t y_count = abs(cy - y);
if(target != 'z') {
while(y_count--) result = y_command;
while(x_count--) result = x_command;
} else {
while(x_count--) result = x_command;
while(y_count--) result = y_command;
}
result = '!';
x = cx; y = cy;
}
result = '\0';
return result;
} 原题和运行结果 图片上就是完整的原题?
看不出来输入 "code"
和输出有什么联系
还有,你发的代码不完整
还有,你刷题的那个网址发一下,是当前问题的地址
人造人 发表于 2021-8-8 20:50
还有,你发的代码不完整
还有,你刷题的那个网址发一下,是当前问题的地址
刷题的网址:https://leetcode-cn.com/problems/alphabet-board-path/ 人造人 发表于 2021-8-8 20:50
还有,你发的代码不完整
还有,你刷题的那个网址发一下,是当前问题的地址
完整代码:#include <stdio.h>
char * alphabetBoardPath(char * target){
char board = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"};
int i,j;
int x,y;
static char p;
int k=0;
i=j=0;
while(*target!='\0'){
for(x=0;x<6;x++){
for(y=0;y<5;y++){
if(*target==board)
goto A;
}
}//寻找字母坐标;
A: ;
if(x>i){
while(x>i&&i<4){
p='D';
i++;
}
}
else if(x<i){
while(x<i){
p='U';
i--;
}
}
//行
if(y>j){
while(y>j){
p='R';
j++;
}
}
else if(y<j){
while(y<j){
p='L';
j--;
}
}
//列;
if(x>i){
p='D';
}
//z;
p='!';
i=x;
j=y;
target++;
}
return p;
}
int main()
{
char a;
char *p;
scanf("%[^\n]",a);
p=alphabetBoardPath(a);
printf("%s\n",p);
return 0;
}
武德 发表于 2021-8-9 00:07
完整代码:#include
char * alphabetBoardPath(char * target){
为什么要模拟走一遍,直接算出来不就好了
#include <cmath>
class Solution {
public:
string alphabetBoardPath(string target) {
string result;
ssize_t x = 0, y = 0;
for(const auto &i: target) {
size_t num = (i - 'a');
ssize_t cx = num % 5;
ssize_t cy = num / 5;
string y_command, x_command;
if(cy > y) y_command = "D";
else if(y > cy) y_command = "U";
if(cx > x) x_command = "R";
else if(x > cx) x_command = "L";
ssize_t y_count = std::abs(cy - y);
ssize_t x_count = std::abs(cx - x);
while(y_count--) result += y_command;
while(x_count--) result += x_command;
result += "!";
y = cy; x = cx;
}
return result;
}
}; 还有先后顺序的说,改一改
#include <cmath>
class Solution {
public:
string alphabetBoardPath(string target) {
string result;
ssize_t x = 0, y = 0;
for(const auto &i: target) {
size_t num = i - 'a';
ssize_t cx = num % 5;
ssize_t cy = num / 5;
string x_command, y_command;
if(cx > x) x_command = "R";
else if(x > cx) x_command = "L";
if(cy > y) y_command = "D";
else if(y > cy) y_command = "U";
ssize_t x_count = std::abs(cx - x);
ssize_t y_count = std::abs(cy - y);
if(i != 'z') {
while(y_count--) result += y_command;
while(x_count--) result += x_command;
} else {
while(x_count--) result += x_command;
while(y_count--) result += y_command;
}
result += "!";
x = cx; y = cy;
}
return result;
}
}; 你用 static 存储的字符串
忘了字符串以 '\0' 结束了
#include <stdio.h>
char * alphabetBoardPath(char * target){
char board = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"};
int i,j;
int x,y;
static char p;
int k=0;
i=j=0;
while(*target!='\0'){
for(x=0;x<6;x++){
for(y=0;y<5;y++){
if(*target==board)
goto A;
}
}//寻找字母坐标;
A: ;
if(x>i){
while(x>i&&i<4){
p='D';
i++;
}
}
else if(x<i){
while(x<i){
p='U';
i--;
}
}
//行
if(y>j){
while(y>j){
p='R';
j++;
}
}
else if(y<j){
while(y<j){
p='L';
j--;
}
}
//列;
if(x>i){
p='D';
}
//z;
p='!';
i=x;
j=y;
target++;
}
p = '\0';
return p;
}
页:
[1]