1.替换空格
1 public static String replaceSpace(String s){ 2 StringBuilder stringBuilder = new StringBuilder(); 3 for (int i = 0; i < s.length(); i++) { 4 if(s.charAt(i) == ' ') stringBuilder.append("%20"); 5 else stringBuilder.append(s.charAt(i)); 6 } 7 return stringBuilder.toString(); 8 }
2.从尾到头打印链表
public static int[] reversePrintPlus(ListNode head){ int len = 0; ListNode cur = head; while(cur != null){ len++; cur = cur.next; } int [] res = new int[len]; while(head != null && len > 0){ res[len-1] = head.val; head = head.next; len--; } return res; }