Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. public class Solution {
  2. public String simplifyPath(String path) {
  3. LinkedList<String> stack = new LinkedList<String>();
  4. StringBuffer sb1 = new StringBuffer("");
  5. for(int i = 0 ; i < path.length(); i++)
  6. {
  7. if(path.charAt(i)=='/')
  8. {
  9. if(sb1.toString().equals(".."))
  10. {
  11. if(!stack.isEmpty())
  12. stack.removeLast();
  13. }else if(!sb1.toString().equals("")&&!sb1.toString().equals("."))
  14. stack.addLast(sb1.toString());
  15. sb1=new StringBuffer("");
  16. }else
  17. {
  18. sb1.append(path.charAt(i));
  19. }
  20. }
  21. if(sb1.toString().equals(".."))
  22. {
  23. if(!stack.isEmpty())
  24. stack.removeLast();
  25. }else if(!sb1.toString().equals("")&&!sb1.toString().equals("."))
  26. stack.addLast(sb1.toString());
  27. StringBuffer result = new StringBuffer("");
  28. if(stack.isEmpty()) return "/";
  29. for(String s: stack)
  30. {
  31. result = result.append("/");
  32. result = result.append(s);
  33. }
  34. return result.toString();
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement