preguntas de la entrevista de compresión de cuerdas 01.06
class Solution {
public String compressString(String S) {
int N = S.length();
int i = 0;
StringBuilder sb = new StringBuilder();
while (i < N) {
int j = i;
while (j < N && S.charAt(j) == S.charAt(i)) {
j++;
}
sb.append(S.charAt(i));
sb.append(j - i);
i = j;
}
String res = sb.toString();
return res.length() < S.length()? res:S;
}
}
preguntas de la entrevista 58 - II cadena dejaron rotación.
//substring(int beginIndex, int endIndex) 方法返回字符串的子字符串。
class Solution {
public String reverseLeftWords(String s, int n) {
return s.substring(n) + s.substring(0, n);
}
}
class Solution {
public String reverseLeftWords(String s, int n) {
char[] chars = s.toCharArray();
reverse(chars, 0, n-1);
reverse(chars, n, chars.length-1);
reverse(chars, 0, chars.length-1);
return new String(chars);
}
private void reverse(char[] arr, int i, int j) {
while(i < j) {
char x = arr[i];
arr[i] = arr[j];
arr[j] = x;
i++;
j--;
}
}
}
class Solution {
public String reverseLeftWords(String s, int n) {
String sub = s.substring(n);
StringBuilder sb = new StringBuilder();
sb.append(sub);
String sub2 = s.substring(0, n);
sb.append(sub2);
return sb.toString();
}
}
class Solution {
public String reverseLeftWords(String s, int n) {
char [] res = new char[s.length()];
char [] temp = s.toCharArray();
System.arraycopy(temp,0,res,s.length() - n,n);
System.arraycopy(temp,n,res,0,s.length() - n);
return String.copyValueOf(res);
}
}
- sistema proporciona un arraycopy método estático (), podemos utilizar para implementar la replicación entre arrays.
22. Una cara preguntas lista enlazada nodo k penúltimas
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
if(head == null||head.next == null){
return head;
}
ListNode curr = head;
while( k>0 && curr != null){
curr = curr.next;
k--;
}
while(curr != null){
curr = curr.next;
head = head.next;
}
return head;
}
}
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode fast = head;
while(fast!=null) {
fast = fast.next;
if(k==0) {
head = head.next;
}else {
k--;
}
}
return head;
}
}
Cuanto más se sabe, más usted no sabe.
Forma correcta sin necesidad de cirugía, los pacientes aún pueden buscar, no hay ninguna manera de la cirugía, poniendo fin a la cirugía.
Si usted tiene otras preguntas, mensaje de bienvenida, podemos discutir, aprender juntos y progresar juntos