409. La cadena más larga palíndromo
//贪心算法
class Solution {
public int longestPalindrome(String s) {
int[] count = new int[58];
for (char c: s.toCharArray())
count[c-'A']++;
int ans = 0;
for (int v: count) {
ans += v / 2 * 2;
if (v % 2 == 1 && ans % 2 == 0)
ans++;
}
return ans;
}
}
//int数组计数
class Solution {
public int longestPalindrome(String s) {
int[] cnt = new int[58];
for (char c : s.toCharArray()) {
cnt[c - 'A'] += 1;
}
int ans = 0;
for (int x: cnt) {
// 字符出现的次数最多用偶数次。
ans += x - (x & 1);
}
// 如果最终的长度小于原字符串的长度,说明里面某个字符出现了奇数次,那么那个字符可以放在回文串的中间,所以额外再加一。
return ans < s.length() ? ans + 1 : ans;
}
}
05. sustituir los espacios preguntas cara
class Solution {
public String replaceSpace(String s) {
return s.replace(" ", "%20");
}
}
class Solution {
public String replaceSpace(String s) {
StringBuilder res = new StringBuilder();
//Character是char的包装类
for(Character c : s.toCharArray())
{
if(c == ' ') res.append("%20");
else res.append(c);
}
return res.toString();
}
}
24. La lista de preguntas cara de inversión
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode nextTemp = reverseList(head.next);
head.next.next = head;
head.next = null;
return nextTemp;
}
}
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null,curr = head,next = null;
while(curr != null){
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
}
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