先要說的是,java的API裡面已經提供了單向鏈表的類,大家可以直接拿來用。在這自己實現的目的是為了更好的理解鏈表資料結構。這裡主要是介紹一些常用結構裡面都會用到的方法,以及鏈表具體是如何操作的。 People.java 類是自訂的資料類型 public class People {private String name; //姓名private int age; //年齡private int id; //唯一標識idpublic People(String name, int
java.math. RoundingMode.HALF_UP Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for RoundingMode.UP if the discarded fraction is ≥ 0.5; otherwise,
題目:請實現一個函數,將一個字串中的空格替換成“%20”。例如,當字串為We Are Happy.則經過替換之後的字串為We%20Are%20Happy。 思路就是如果從左至右去插入的話,時間複雜度為O(n2),先確定空格的個數,然後確定替換後的String長度,從右至左插入的話時間複雜度為O(n) 簡單的,但時間複雜度高的代碼: public class Solution { public String replaceSpace(StringBuffer str) {
162. Find Peak Element A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index
1.數組的定義 數組是存放同一種類型的資料 資料類型 數組名[] = new 資料類型[大小] 資料類型[] 數組名 = new 資料類型[大小] int[] num = new int[3] int num[] = new int[5] int [] arr; arr = new int[5]; int[] a= new int[5] 2.數組的引用 數組名【下標】 a[1] a[3
【225-Implement Stack using Queues(用隊列實現棧操作)】 【LeetCode-面試演算法經典-Java實現】【所有題目目錄索引】 代碼下載【https://github.com/Wang-Jun-Chao】 原題 Implement the following operations of a stack using queues. push(x) – Push element x onto stack. pop() –
【題目描述】請實現兩個函數,分別用來序列化和還原序列化二叉樹 【解題思路1】 //1. 利用前序走訪序列化二叉樹,然後從屬記錄的字串中還原序列化二叉樹。 //2. 遇到空節點需要用特殊字元加以標記。如“#” public class Solution { public int index = -1; String Serialize(TreeNode root) { StringBuilder s = new StringBuilder();