最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

Leetcode71简化路径

维修 admin 40浏览 0评论

Leetcode71简化路径

代码:

class Solution {public String simplifyPath(String path) {String[] names = path.split("/");Deque<String> stack = new ArrayDeque<String>();for(String name: names){if(name.equals("..")){if(!stack.isEmpty()){stack.pollLast();}}else if(name.length()>0&&!".".equals(name)){stack.offerLast(name);}}StringBuffer ans = new StringBuffer();if(stack.isEmpty()){ans.append('/');}else{while(!stack.isEmpty()){ans.append('/');ans.append(stack.pollFirst());}}return ans.toString();}
}

stack的一些用法

Leetcode71简化路径

代码:

class Solution {public String simplifyPath(String path) {String[] names = path.split("/");Deque<String> stack = new ArrayDeque<String>();for(String name: names){if(name.equals("..")){if(!stack.isEmpty()){stack.pollLast();}}else if(name.length()>0&&!".".equals(name)){stack.offerLast(name);}}StringBuffer ans = new StringBuffer();if(stack.isEmpty()){ans.append('/');}else{while(!stack.isEmpty()){ans.append('/');ans.append(stack.pollFirst());}}return ans.toString();}
}

stack的一些用法

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论