求算法(50)

  • 主题发起人 主题发起人 lixianxiang
  • 开始时间 开始时间
L

lixianxiang

Unregistered / Unconfirmed
GUEST, unregistred user!
有个面试题:输入一个数字串,取得重复的数字,要求只能用一个循环。例如输入 1234534789 则得到 34; 输入32103190 则得到310。如果允许用多个循环的话倒是挺简单,但是面试人员要求只能用一个循环。寻求算法function Pvt_GetDuplicateNum(ANumStr: string): string;var Lv_i, Lv_j: Integer;
begin
for Lv_i := 1 to Length(ANumStr) do
begin
for Lv_j := Lv_i + 1 to Length(ANumStr) do
begin
if (ANumStr[Lv_i] = ANumStr[Lv_j]) and (Pos(ANumStr[Lv_i], Result) = 0)then
begin
Result := Result + ANumStr[Lv_i];
end;
end;
end;
end;
 
function Pvt_GetDuplicateNum(ANumStr: string): string;var I,K:Integer;
begin
Result:='';
for I:=Length(ANumStr) do
wnto 2 do
begin
K:=Pos( ANumStr, ANumStr);
if (K>0) AND (K<>I) and (Pos(ANumStr,Result)=0) then
Result:=ANumStr+Result;
end;
end;
 
谢谢znxiafunction Pvt_GetDuplicateNum(ANumStr: string): string;var I, K: Integer;
begin
Result := '';
ANumStr := ReverseString(ANumStr);
// 加上这句就OK了 for I := Length(ANumStr) do
wnto 2 do
begin
K := Pos(ANumStr, ANumStr);
if (K > 0) and (K <> I) and (Pos(ANumStr, Result) = 0) then
Result := Result + ANumStr;
end;
end;
 
高!给分吧。
 
用Pos还不是循环么?更简单高效的算法在这里:function GetDuplicateChar(NumberStr:string):string;var CharCounts:array ['0'..'9'] of Integer;
I:Integer;
begin
FillChar(CharCounts['0'],SizeOf(CharCounts),0);
Result:='';
for I := 1 to Length(NumberStr) do
begin
Inc(CharCounts[NumberStr]);
if CharCounts[NumberStr]=2 then
Result:=Result+NumberStr;
end;
end;
 
Result:=Result+NumberStr;高效?
 
你用脑子想想这个Result:=Result+NumberStr;最多会执行多少次?
 
function GetDuplicateChar(NumberStr:string):string;var CharCounts:array ['0'..'9'] of Integer;
I:Integer;
begin
FillChar(CharCounts['0'],SizeOf(CharCounts),0);
Result:='';
for I := 1 to Length(NumberStr) do
begin
Inc(CharCounts[NumberStr]);
if CharCounts[NumberStr]=2 then
Result:=Result+NumberStr;
if Length(Result)=10 then
Break;
//全部由重复就不必继续遍历了 end;
end;
 
这个字符串的长度有可能很长
 
if CharCounts[NumberStr]=2 then
Result:=Result+NumberStr;最多是10次,因为只有10个数字
 
var tempstr,strtemp:string;
i:integer;
tmplist,tmplist1,tmplist2:TStringList;
//tmpset1:set of '0'..'9';
begin
tmplist:=TStringList.Create;
tmplist1:=TStringList.Create;
tmplist2:=TStringList.Create;
tempstr:=edt1.Text;
tmplist.Add(tempstr);
for i:=0 to 9 do
tmplist2.Add(IntToStr(i));
for i:=1 to Length(tempstr)-1 do
begin
if tmplist2.IndexOf(tempstr)>-1 then
begin
tmplist.Clear;
tmplist.Delimiter:=Char(Ord(tempstr));
tmplist.DelimitedText:=tempstr;
if tmplist1.IndexOf(tempstr)>-1 then
Continue else
if tmplist.Count>2 then
begin
strtemp :=strtemp+tempstr;
tmplist1.Add(tempstr);
end;
end;
end;
ShowMessage(strtemp);
tmplist.Free;
tmplist1.Free;
tmplist2.Free;
 
地质灾害 的方法更高明!
 
function GetDuplicateChar(NumberStr:string):string;var CharCounts:array ['0'..'9'] of Integer;
CharResult:array['0'..'9'] of char;
I:Integer;
c:integer;//重复数begin
FillChar(CharCounts['0'],SizeOf(CharCounts),0);
//如果真要算循环,这里有一个 FillChar(charresult['0'],SizeOf(charresult),0);
//如果真要算循环,这里有一个 Result:='';
c:=0;
for I := 1 to Length(NumberStr) do
begin
if CharCounts[NumberStr] > 1 then
begin
if C=10 then
Break //全部重复就不必继续遍历了 else
begin
CharResult[NumberStr] := NumberStr;
inc(c);
continue;
//已经重复的,连加都不用了, end;
end else
Inc(CharCounts[NumberStr]);
end;
setlength(result,10);
move(charresult,result[1],length(charresult));////这里也许要算一个,看不到低层代码,猜测,汇编就免了 result := stringreplace(result,#0,'',[rfReplaceAll]);//如果真要算循环,这里有一个 //stringreplace 有问题,不能替换 #0end;
 
后退
顶部