在文本内容为空的时候也可以按回退backspace(在这种情况下没有任何效果)。
在文本内容为空的时候也可以按回退backspace(在这种情况下没有任何效果)。
"acv<"
"ac"
牛妹在打完"acv"之后按了回退,所以最后是"ac"
给定一个字符串s,代表牛妹所按下的按键。
class Solution { public: /** * * @param s string字符串 * @return string字符串 */ string Typing(string s) { stack<char> S; string t = ""; for(auto &c: s){ if(c=='<'){ if(!S.empty()) S.pop(); }else S.push(c); } while(!S.empty()){ t += S.top(); S.pop(); } for(int i=0,j=t.length()-1;i<j;i++,j--) swap(t[i], t[j]); return t; } };
我是菜鸡。目前java 66ms(勿喷)
题目是很简单,但我还是想在这里记录下为什么我没有使用Stack或者StringBuilder。
结合实际情况看来,或许直接莽是最快速的实现方式:
public String Typing (String s) { // write code here char[] tmp = new char[s.length()]; int cur = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c != '<') { tmp[cur++] = c; } else { cur = cur > 0 ? cur - 1 : 0; } } return new String(tmp).substring(0, cur); }
import java.util.*; public class Solution { public String Typing (String s) { // 入栈 Stack<Character> stack = new Stack<>(); for (char c : s.toCharArray()) { if (c == '<') { if (!stack.empty()) { stack.pop(); } continue; } stack.push(c); } // 出栈 String result = ""; while (!stack.empty()) { result = stack.pop() + result; } return result; } }
func Typing(s string) string { nb := []byte{} b := []byte(s) for i := 0; i < len(b); i++ { if b[i] == 60 && len(nb) > 0 { nb = nb[:len(nb)-1] }else{ nb=append(nb,b[i])} } nnb:=[]byte{} for _,v:=range nb{ if v != 60{ nnb=append(nnb,v) } } sb:=string(nnb) return sb }
class Solution { public: /** * * @param s string字符串 * @return string字符串 */ string Typing(string s) { stack<char> st; for(int i = 0; i < s.size(); i++) { if(!st.empty() && s[i] == '<') st.pop(); else if(s[i] != '<') st.push(s[i]); } char t[100000] = "\0"; int len = st.size() - 1; while(len >= 0) { t[len--] = st.top(); st.pop(); } return t; } };
import java.util.*; public class Solution { /** * * @param s string字符串 * @return string字符串 */ public String Typing (String s) { // write code here Stack<String> stack = new Stack<>(); String[] str = s.split(""); for (int i = 0; i< str.length;i++){ if ("<".equals(str[i]) ){ if (!stack.isEmpty()){ stack.pop(); } continue; } stack.push(str[i]); } StringBuilder sb = new StringBuilder(); while (!stack.isEmpty()){ sb.append(stack.pop()); } return sb.reverse().toString(); } }
import java.util.*; public class Solution { /** * * @param s string字符串 * @return string字符串 */ public String Typing (String s) { if(s == null || s.length() == 0){ return null; } Stack<Character> stack = new Stack<>(); char[] chars = s.toCharArray(); for(char ch : chars){ if(ch != '<'){ stack.push(ch); }else{ if(!stack.isEmpty()){ stack.pop(); } } } StringBuilder str = new StringBuilder(); while(!stack.isEmpty()){ str.append(stack.pop()); } return str.reverse().toString(); } }
import java.util.*; public class Solution { /** * * @param s string字符串 * @return string字符串 */ public String Typing (String s) { // write code here String str = ""; char[] list = s.toCharArray(); for(int i = 0;i<list.length;i++){ if(list[i]=='<'){ if(i>0){ str = str.substring(0,i-1); continue; } else{ continue; } } str +=list[i]; } return str; } }本地对于用例“<” 输出为空字符串,这样写有什么问题吗?用例的通过率很低
//感觉应该将所有情况都考虑了 string Typing(string s) { string res; if (s.empty()) { return res; } int len = s.length(); deque<char>de; for (int i = 0; i < len; i++) { if (de.empty()) { if (s[i]=='<') { continue; } else { de.push_back(s[i]); } } else { if (s[i]=='<') { de.pop_back(); } else { de.push_back(s[i]); } } } while (!de.empty()) { res.push_back(de.front()); de.pop_front(); } return res; }
不知道为什么错,在这里就只能通过28% import java.util.*; public class Solution { /** * * @param s string字符串 * @return string字符串 */ public String Typing (String s) { if(s==null || s.length()==0){ return ""; } if(s.equals("<")){ return ""; } int index; while((index=s.indexOf('<'))!=-1){ if(s.charAt(0)=='<'){ s = s.substring(1,s.length()); } s = s.substring(0,index-1)+s.substring(index+1,s.length()); } return s; } }
class Solution: def Typing(self , s ): num = 0 for char in s: if char == "<": num = num + 1; if num == 0: return s; for i in range(num): if s.index("<") == 0 or s.index("<") == 1: s = s[s.index("<")+1:]; else: s = s[:s.index("<")-1] + s[s.index("<")+1:]; return s;
public class Solution { /** * * @param s string字符串 * @return string字符串 */ public String Typing (String s) { // write code here char[] array = s.toCharArray(); if(array.length == 0){ return null; } LinkedList<Character> deque = new LinkedList<>(); for(Character ch : array){ if(!ch.equals('<')){ deque.push(ch); }else if(!(deque.size() == 0)){ deque.pop(); } } StringBuilder str = new StringBuilder(); while(!(deque.size() == 0)){ str.append(deque.pollLast()); } return str.toString(); } }