馃憦馃憦馃憦鍝堝柦锛佸ぇ瀹跺ソ锛屾垜鏄儹鐖卞垎浜悇绉嶆妧鏈殑鍗氫富銆愬鏃犳澧冨皬濂囥€戯紒馃槏馃槏馃槏鍦ㄦ垜鐨勫崥瀹㈤噷锛屾瘡涓€鏉℃妧鏈矾寰勬垜閮戒翰鑷帰绱㈣繃锛屾瘡涓€琛屼唬鐮佹垜閮藉疄闄呰繍琛岃繃銆備粖澶╋紝鎴戣鍜屽ぇ瀹跺垎浜叧浜嶭eetCode绠楁硶鐨勪竴涓粡鍏搁棶棰樷€斺€旀渶闀垮叕鍏卞墠缂€鐨勬寫鎴橈紒鉁娾湂鉁?/p>涓€銆丩eetCode绠楁硶涔嬫梾
馃敟 1銆佹帰瀵绘渶闀垮叕鍏卞墠缂€ 馃敟
馃幆 1.1銆侀鐩儗鏅?/p>
鎯宠薄涓€涓嬭繖鏍蜂竴涓満鏅細浣犻潰涓翠竴涓瓧绗︿覆鏁扮粍锛岄渶瑕佹壘鍑哄畠浠箣闂寸殑鏈€闀垮叕鍏卞墠缂€銆傚鏋滀笉瀛樺湪鍏叡鍓嶇紑锛屽氨杩斿洖绌哄瓧绗︿覆銆傛瘮濡傦紝杈撳叆鐨勬槸["flower","flow","flight"]锛岄偅涔堣緭鍑哄氨搴旇鏄?fl"銆?/p>
馃幆 1.2銆佹€濊矾瑙f瀽
闈㈠杩欎釜闂锛屾垜浠彲浠ラ噰鍙栬繖鏍风殑绛栫暐锛氫粠绗竴涓瓧绗﹀紑濮嬶紝瀵绘壘鍏叡鐨勫墠缂€銆傚洜涓鸿繖涓墠缂€鏄渶闀跨殑锛屾墍浠ュ畠鐨勯暱搴︿笉浼氳秴杩囦换浣曚竴涓瓧绗︿覆鐨勯暱搴︺€傛垜浠彲浠ュ厛浠庣涓€涓瓧绗︿覆鍏ユ墜锛岀劧鍚庨€愪釜姣旇緝瀹冪殑姣忎釜瀛楃涓庡叾浠栧瓧绗︿覆鐨勭浉搴斾綅缃殑瀛楃鏄惁鐩稿悓銆傚鏋滃彂鐜颁笉鍚屽瓧绗︽垨宸茬粡姣旇緝瀹屾墍鏈夊瓧绗︿覆锛屽氨缁撴潫姣旇緝銆傝繖鏍凤紝鎴戜滑灏辫兘鎵惧埌鏈€闀垮叕鍏卞墠缂€銆?/p>
馃幆 1.3銆佺瓟妗堟彮鏅?/p>
涓嬮潰鏄竴娈佃В鍐宠繖涓棶棰樼殑Java浠g爜锛?/p>
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0){
return "";
}
int length = strs[0].length();
int count = strs.length;
for(int i = 0; i < length; i++){
char c = strs[0].charAt(i);
for(int j = 1; j < count; j++){
if(i >= strs[j].length() || strs[j].charAt(i) != c){
return strs[0].substring(0,i);
}
}
}
return strs[0];
}
}
閫氳繃杩欐浠g爜锛屾垜浠兘澶熸湁鏁堝湴鎵惧埌瀛楃涓叉暟缁勪腑鐨勬渶闀垮叕鍏卞墠缂€銆傚鏋滀綘瀵规妧鏈湁鐫€娴撳帤鐨勫叴瓒o紝娆㈣繋鍏虫敞鎴戠殑鍗氬銆愬鏃犳澧冨皬濂囥€戯紝鍜屾垜涓€璧蜂氦娴佸涔犵殑蹇冨緱鍜屼綋楠岋紒馃槝馃槝馃槝鎰熻阿澶у鐨勯槄璇诲拰鏀寔锛佲潳锔忊潳锔忊潳锔?/p>
文章来自《钓虾网小编|www.jnqjk.cn》整理于网络,文章内容不代表本站立场,转载请注明出处。