一些注意事項

2021-08-21 05:21:54 字數 3234 閱讀 4014

0.解題思路

a. 普通思路

b. 遞迴(bfs/dfs)

c. 動態規劃(比遞迴快)

d. 雙指標(比for迴圈快)(如:快慢指標方法)

e. 用棧

1.陣列初始化

int a=new int[5];

a[0]預設為0.二維陣列也一樣。

boolean b=new boolean[5];

b[0]預設為false.

string c=new string[5];

c[0]預設為null

2.map如果重複會覆蓋

mapmap = new hashmap();

map.put(1,200);

system.out.println(map.size());

map.put(1,300);

system.out.println(map.size());

system.out.println(map.get(1));

輸出

1

1300

3.迴圈

for(int i=0;i<0;i++)
不會輸出。

4.排序

arrays.sort(陣列)

collections.sort(list)

5.list.contains(***);

注意:時間複雜度o(n)。

map.containskey(***)

時間複雜度小於o(n)。

盡量使用map.containskey(***)。

6.字串

str=str.trim();  //去掉首尾空格

str=str.replace(" ",""); //去掉所有空格

if( str.charat(0)=='a' ){}  //取字串某位字元,char比較使用==

"abcd".substring(1,3) 結果為 "bc"

字串比較 str1.compareto(str2)

int a=integer.parseint(str); //字串轉整數

string a="a man, a plan, a canal: panama";

a=a.tolowercase(); //轉小寫

a=a.touppercase(); //轉大寫

string ss=s.split("***"); //分割成字串陣列

特殊情況

string s="1.2.3";

string ss=s.split(".");  //不行,因為"."為正規表示式,需要轉義"\.","\"也需要轉義"\\."

string ss=s.split("\\.");

7. stack

stackstack=new stack();

if( stack.empty() )

string s=stack.pop()

stack.push(s)

stack.peek()      // 檢視堆疊頂部的物件,但不從堆疊中移除

8.string 轉 char

char c=str.tochararray();

string s=string.valueof(c);

9.map

map.containskey(***);  //複雜度o(n)

map.containsvalue(***);  //複雜度o(n²)

遍歷for(map.entry entry : map.entryset())

10.佇列

queuequeue=newlinkedlist();

queue.offer("a");  // 新增到隊尾

str=queue.poll();  // 移出隊頭

11.char 轉int

char c = '3';

int n = c - '0';

在使用 'a'+1 時最好在前面加上強制型別轉換(char)
public static void main(string args) 

輸出66

b-------66b

------

b

12. 位運算

與(&)

或(|)

非(~)

異或(^)

左移(<

右移(>>) :num >> 1,相當於num除以2

13.long型別

int型別範圍[-2的31次方,2的31次方-1]

如果int超範圍,使用long型別。一定要先轉換為long型別再加 『-』,如下圖。

int最大值的平方也不會超過long型別範圍。

long a=10000000;

long b=10000000;

system.out.println(a*b);

int c=10000000;

int d=10000000;

system.out.println(c*d);

輸出100000000000000

276447232

小心int乘法超範圍,必須手動轉換為long型別,無法自動裝箱。

14.精度問題

判斷a/b==c/d時,注意精度問題。正確的做法是,分子分母求最大公約數然後進行約分變為最簡分數再比較,不能用簡單的除法計算。

15. list翻轉

collections.reverse(list);

16.運算優先順序

== 優先順序大於 &

if((n&1)==1)
需要加上 ()再==

php XPATH一些注意事項

40集 建立乙個dom物件並讀取xml檔案到記憶體中 dom new domdocument 1.0 utf 8 dom load book.xml 建立乙個xpath物件 xpath new domxpath dom sql bookstore book 1 title 注意此處路徑數字從1開始,...

vue一些注意事項

1.生命週期鉤子的this上下文指向呼叫它的 vue 例項。不要在選項屬性或 上使用箭頭函式,比如 created console.log this.a 或 vm.watch a newvalue this.mymethod 因為箭頭函式是和父級上下文繫結在一起的,this 不會是如你所預期的 vu...

c 一些注意事項

1.long int的位元組資訊 int在32位系統下是4位元組,long在32位也是4位元組,在64位int不變,但是long變成8位元組,所以我們的編譯器不同可能會導致我們處理int,long不同 2.注意c 有時候的強制型別轉換 注意最大最小值是不一樣的,int max 231 1 或 int...