@@ -460,13 +460,13 @@ using namespace std;
460
460
const beforeEnd = codeTemplate . substring ( 0 , endIndex + endMarker . length ) ;
461
461
const afterEnd = codeTemplate . substring ( endIndex + endMarker . length ) ;
462
462
463
- const debugTemplate = this . generateCppDebugTemplate ( parsedArgs ) ;
463
+ const debugTemplate = this . generateCppDebugTemplate ( parsedArgs , codeTemplate ) ;
464
464
return beforeEnd + debugTemplate + afterEnd ;
465
465
} else {
466
466
// Если маркер не найден, добавляем в конец файла
467
467
const debugTemplate = `
468
468
// @lc code=end
469
- ` + this . generateCppDebugTemplate ( parsedArgs ) ;
469
+ ` + this . generateCppDebugTemplate ( parsedArgs , codeTemplate ) ;
470
470
return codeTemplate + debugTemplate ;
471
471
}
472
472
}
@@ -491,13 +491,13 @@ using namespace std;
491
491
const beforeEnd = codeTemplate . substring ( 0 , endIndex + endMarker . length ) ;
492
492
const afterEnd = codeTemplate . substring ( endIndex + endMarker . length ) ;
493
493
494
- const debugTemplate = this . generateCppDebugTemplate ( parsedArgs ) ;
494
+ const debugTemplate = this . generateCppDebugTemplate ( parsedArgs , codeTemplate ) ;
495
495
return beforeEnd + debugTemplate + afterEnd ;
496
496
} else {
497
497
// Если маркер не найден, добавляем в конец файла
498
498
const debugTemplate = `
499
499
// @lc code=end
500
- ` + this . generateCppDebugTemplate ( parsedArgs ) ;
500
+ ` + this . generateCppDebugTemplate ( parsedArgs , codeTemplate ) ;
501
501
return codeTemplate + debugTemplate ;
502
502
}
503
503
}
@@ -826,7 +826,30 @@ using namespace std;
826
826
return elements ;
827
827
}
828
828
829
- private generateCppDebugTemplate ( parsedArgs : { args : string [ ] } ) : string {
829
+ private extractMethodName ( codeTemplate : string ) : string {
830
+ // Ищем публичный метод в классе Solution
831
+ const methodPattern = / p u b l i c : \s * [ \w \s < > * & : \[ \] ] * \s + ( \w + ) \s * \( / ;
832
+ const match = codeTemplate . match ( methodPattern ) ;
833
+
834
+ if ( match && match [ 1 ] ) {
835
+ console . log ( '🔧 Найден метод:' , match [ 1 ] ) ;
836
+ return match [ 1 ] ;
837
+ }
838
+
839
+ // Если не найден паттерн public:, ищем любой метод после класса Solution
840
+ const anyMethodPattern = / c l a s s \s + S o l u t i o n \s * { [ ^ } ] * ?[ \w \s < > * & : \[ \] ] * \s + ( \w + ) \s * \( / ;
841
+ const anyMatch = codeTemplate . match ( anyMethodPattern ) ;
842
+
843
+ if ( anyMatch && anyMatch [ 1 ] && anyMatch [ 1 ] !== 'Solution' ) {
844
+ console . log ( '🔧 Найден метод (альтернативный поиск):' , anyMatch [ 1 ] ) ;
845
+ return anyMatch [ 1 ] ;
846
+ }
847
+
848
+ console . log ( '⚠️ Метод не найден, используем someMethod' ) ;
849
+ return 'someMethod' ;
850
+ }
851
+
852
+ private generateCppDebugTemplate ( parsedArgs : { args : string [ ] } , codeTemplate ?: string ) : string {
830
853
if ( parsedArgs . args . length === 0 ) {
831
854
return `
832
855
@@ -842,45 +865,70 @@ int main()
842
865
` ;
843
866
}
844
867
845
- // Определяем типичные паттерны для LeetCode задач
846
- const hasArray = parsedArgs . args . some ( ( arg ) => arg . startsWith ( '{' ) ) ;
847
- const hasNumbers = parsedArgs . args . some ( ( arg ) => / ^ \d + $ / . test ( arg ) ) ;
848
-
849
868
let variableDeclarations = '' ;
850
869
let methodCall = '' ;
851
870
852
- if ( hasArray && hasNumbers ) {
853
- // Обычный паттерн: массив + число (например, Two Sum, Combination Sum)
854
- const arrayArgs = parsedArgs . args . filter ( ( arg ) => arg . startsWith ( '{' ) ) ;
855
- const numberArgs = parsedArgs . args . filter ( ( arg ) => / ^ \d + $ / . test ( arg ) ) ;
871
+ // Анализируем каждый аргумент и создаем соответствующие переменные
872
+ parsedArgs . args . forEach ( ( arg , index ) => {
873
+ const cleanArg = arg . trim ( ) ;
856
874
857
- arrayArgs . forEach ( ( arg , index ) => {
858
- variableDeclarations += ` vector<int> arr ${ index + 1 } = ${ arg } ;\n` ;
859
- } ) ;
875
+ if ( cleanArg . startsWith ( '{' ) && cleanArg . endsWith ( '}' ) ) {
876
+ // Это массив - определяем тип элементов
877
+ const content = cleanArg . slice ( 1 , - 1 ) . trim ( ) ;
860
878
861
- numberArgs . forEach ( ( arg , index ) => {
862
- variableDeclarations += ` int target${ index + 1 } = ${ arg } ;\n` ;
863
- } ) ;
879
+ if ( ! content ) {
880
+ // Пустой массив
881
+ variableDeclarations += ` vector<int> arr${ index + 1 } = {};\n` ;
882
+ } else if ( content . includes ( '"' ) ) {
883
+ // Массив строк
884
+ variableDeclarations += ` vector<string> arr${ index + 1 } = ${ cleanArg } ;\n` ;
885
+ } else {
886
+ // Массив чисел
887
+ variableDeclarations += ` vector<int> arr${ index + 1 } = ${ cleanArg } ;\n` ;
888
+ }
889
+ } else if ( cleanArg . startsWith ( '"' ) && cleanArg . endsWith ( '"' ) ) {
890
+ // Строка
891
+ variableDeclarations += ` string str${ index + 1 } = ${ cleanArg } ;\n` ;
892
+ } else if ( / ^ - ? \d + $ / . test ( cleanArg ) ) {
893
+ // Целое число
894
+ variableDeclarations += ` int num${ index + 1 } = ${ cleanArg } ;\n` ;
895
+ } else if ( / ^ - ? \d * \. \d + $ / . test ( cleanArg ) ) {
896
+ // Число с плавающей точкой
897
+ variableDeclarations += ` double num${ index + 1 } = ${ cleanArg } ;\n` ;
898
+ } else if ( cleanArg === 'true' || cleanArg === 'false' ) {
899
+ // Булево значение
900
+ variableDeclarations += ` bool flag${ index + 1 } = ${ cleanArg } ;\n` ;
901
+ } else {
902
+ // Общий случай
903
+ variableDeclarations += ` auto param${ index + 1 } = ${ cleanArg } ;\n` ;
904
+ }
905
+ } ) ;
864
906
865
- if ( arrayArgs . length === 1 && numberArgs . length === 1 ) {
866
- methodCall = ' // auto result = sol.someMethod(arr1, target1);' ;
907
+ // Создаем комментарий для вызова метода
908
+ const paramNames : string [ ] = [ ] ;
909
+ parsedArgs . args . forEach ( ( arg , index ) => {
910
+ const cleanArg = arg . trim ( ) ;
911
+
912
+ if ( cleanArg . startsWith ( '{' ) ) {
913
+ paramNames . push ( `arr${ index + 1 } ` ) ;
914
+ } else if ( cleanArg . startsWith ( '"' ) ) {
915
+ paramNames . push ( `str${ index + 1 } ` ) ;
916
+ } else if ( / ^ - ? \d / . test ( cleanArg ) ) {
917
+ paramNames . push ( `num${ index + 1 } ` ) ;
918
+ } else if ( cleanArg === 'true' || cleanArg === 'false' ) {
919
+ paramNames . push ( `flag${ index + 1 } ` ) ;
867
920
} else {
868
- methodCall = ' // auto result = sol.someMethod(/* укажите нужные параметры */);' ;
921
+ paramNames . push ( `param ${ index + 1 } ` ) ;
869
922
}
870
- } else if ( hasArray ) {
871
- // Только массивы
872
- parsedArgs . args . forEach ( ( arg , index ) => {
873
- if ( arg . startsWith ( '{' ) ) {
874
- variableDeclarations += ` vector<int> arr${ index + 1 } = ${ arg } ;\n` ;
875
- }
876
- } ) ;
877
- methodCall = ' // auto result = sol.someMethod(arr1);' ;
923
+ } ) ;
924
+
925
+ // Извлекаем название метода из кода
926
+ const methodName = codeTemplate ? this . extractMethodName ( codeTemplate ) : 'someMethod' ;
927
+
928
+ if ( paramNames . length > 0 ) {
929
+ methodCall = ` auto result = sol.${ methodName } (${ paramNames . join ( ', ' ) } );` ;
878
930
} else {
879
- // Только числа или другие типы
880
- parsedArgs . args . forEach ( ( arg , index ) => {
881
- variableDeclarations += ` auto param${ index + 1 } = ${ arg } ;\n` ;
882
- } ) ;
883
- methodCall = ' // auto result = sol.someMethod(param1);' ;
931
+ methodCall = ` // auto result = sol.${ methodName } (/* укажите нужные параметры */);` ;
884
932
}
885
933
886
934
return `
@@ -892,7 +940,7 @@ int main()
892
940
// Тестовые данные:
893
941
${ variableDeclarations }
894
942
${ methodCall }
895
- // cout << "Result: " << result << endl;
943
+
896
944
897
945
return 0;
898
946
}
0 commit comments