1. 程式人生 > >LeetCode:Simplify Path

LeetCode:Simplify Path

這題不難,我用20分鐘搞定了,一次提交通過,比較有成就感:)




public class Solution {

	 public String simplifyPath(String path) {
		 String[] ss = path.split("/");
		 int pos1 = 0;
		 int pos2 = 0;
		 while (pos2 < ss.length) {
			 if (ss[pos2].equals(".")) {
				 pos2++;
			 } else if (ss[pos2].equals("..")) {
				 if (pos1 > 0) {
					 pos1--;
				 }
				 pos2++;
			 } else if (ss[pos2].length() == 0) {
				 pos2++;
			 } else {
				 ss[pos1] = ss[pos2];
				 pos1++;
				 pos2++;
			 }
		 }
		 StringBuilder sb = new StringBuilder();
		 sb.append('/');
		 for (int i = 0; i < pos1; i++) {
			 if (i != 0) {
				 sb.append('/');
			 }
			 sb.append(ss[i]);
		 }
		 return sb.toString();
	}

	public static void main(String[] args) {
		Solution s = new Solution();
		String ss = s.simplifyPath("/a/./b/../../c/.././../../c");
		System.out.println(ss);

	}
}


相關推薦

leetcode - Simplify Path

pac ack lan you track push_back home https ignore Given an absolute path for a file (Unix-style), simplify it. For example, pat

[leetcode] simplify-path

strong found string action using ops ngs pac div 時間限制:1秒 空間限制:32768K 熱度指數:4858 本題知識點: 字符串 leetcode 算法知識視頻講解 題目描述 Given an

LeetCode:Simplify Path

這題不難,我用20分鐘搞定了,一次提交通過,比較有成就感:) public class Solution { public String simplifyPath(String path) { String[] ss = path.split("/");

Leetcode 71 簡化路徑simplify-path(棧)

font 出錯 標準 多個 push ac代碼 -s cpp mark 給定一個文檔 (Unix-style) 的完全路徑,請進行路徑簡化。 例如,path = "/home/", => "/home"path = "/a/./b/../../c/", => "

LeetCode】71. Simplify Path(C++)

地址:https://leetcode.com/problems/simplify-path/ 題目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/

Leetcode 71 簡化路徑simplify-path(棧)

給定一個文件 (Unix-style) 的完全路徑,請進行路徑簡化。 例如, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c” 邊界情況: 你是否考慮了 路徑 = “

LeetCode算法系列:71. Simplify Path

目錄 題目描述 演算法實現 題目描述 Given an absolute path for a file (Unix-style), simplify it.  For example,path = "/home/", => "/home"path =

LeetCode---71. Simplify Path

LeetCode—71. Simplify Path 題目 思路及解法 分隔符"/“之間的字元可以分為三種情況,根據這三種情況我們使用棧實現操作: 1.如果是”…",應該返回上級選單,對應出棧 2.如果是".“或者”",保持不變,不對棧進行操作 3.其他情況將

leetcode 71. Simplify Path【檔案路徑表示式】

Given an absolute path for a file (Unix-style), simplify it.  For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "

Leetcode 71 Simplify Path

Given an absolute path for a file (Unix-style), simplify it.  For example,path = "/home/", => "/home"path = "/a/.

LeetCode(45) Simplify Path

題目描述 Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, =&

LeetCode 71. Simplify Path(簡化路徑)

題目描述 給定一個文件 (Unix-style) 的完全路徑,請進行路徑簡化。 例如, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" 邊界情況: 你是否考慮

【九度】題目1090:路徑列印 && 【LeetCodeSimplify Path

時間限制:1 秒記憶體限制:32 兆特殊判題:否提交:1319解決:230 題目描述: 給你一串路徑,譬如: a\b\c a\d\e b\cst d\ 你把這些路徑中蘊含的目錄結構給畫出來,子目錄直接列在父目錄下面,並比父目錄向右縮一格,就像這樣: a   b     c

LeetCode-71-Simplify Path

tin evel col 問題 ack see stream other pla 算法描述: Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it

LeetCode 71. Simplify Path 求絕對路徑,字串處理題

字串 處理題注意以下幾點即可:     ////  即連續的/    /...    則...是目錄名或檔名,而非..和.   空的輸入,為/class Solution { public: s

[演算法分析與設計] leetcode 每週一題: 071. Simplify Path

題目連結:  題目大意: 給定一個 Unix 風格的 path, 給出一個簡化的版本 ; 題中還註明了 對頂層目錄 的假設, 見下方 例3 ; 例如: 給定: "/home/", 則輸出應為: "/home" ; 例如: 給定: "/a/./b/../..

19.2.13 [LeetCode 71] Simplify Path

res 根目錄 要求 targe display alt fin per bsp Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to th

LeetCode:簡化路徑(Simplify Path

題目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", =&

LeetCode每天一題】Simplify Path(簡化路徑)

條件 gin 12px director 否則 出棧 lse linux elf Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to th

LeetCode 113. Path Sum II 20170705 部分之前做了沒寫的題目

for list 路徑 父節點 leetcode pathsum and left -1 Given a binary tree and a sum, find all root-to-leaf paths where each path‘s sum equals the