《劍指offer》學習筆記_面試題15_二進位制中1的個數
-
1.解法1
int NumberOf1(int n){
int count = 0;
unsigned int flag = 1;
while(flag){//當flag中的1移到越界後,flag值會轉變為0
//當n&flag不為0時,表示當前flag中1對應的位置不為0
if(n&flag)count++;
flag = flag<<1;
}
return count;
}
-
2.解法2
int NumberOf1(int n){
int count=0;
while(n){
count++;
n = n&(n-1);
}
return count;
}
相關推薦
《劍指offer》學習筆記_面試題15_二進位制中1的個數
1.解法1 int NumberOf1(int n){ int count = 0; unsigned int flag = 1; w
【劍指Offer學習】【面試題5 : 從尾到頭列印連結串列】【思路】
方案一:(後進先出)遍歷連結串列,再從棧頂開始出個輸出結點的值,此時輸出的結點的順序已經反轉過來了。 先推進棧 再依次取出棧頂元素 方案二:遞迴。 判斷連結串列頭結點是否為空 將 next 結點作為下一次的實參 輸出當前棧頂元素。 缺點:當連結串
【劍指Offer學習】【面試題4 : 替換空格】【思路】
題目: 請實現一個函式,將一個字串中的每個空格替換成“%20”。例如,當字串為We Are Happy,則經過替換之後的字串為We%20Are%20Happy。 思路: 遍歷的方向兩邊皆可。 若從後往前遍歷,新的字串需要反轉。 用 String 類的方法判斷字串
【劍指Offer學習】【面試題5 : 從尾到頭列印連結串列】
題目:輸入個連結串列的頭結點,從尾到頭反過來打印出每個結點的值。 public class Test05 { /** * 結點物件 */ public s
【劍指Offer學習】【面試題26:複雜連結串列的複製】
題目:請實現函式ComplexListNode clone(ComplexListNode head),複製一個複雜連結串列。在複雜連結串列中,每個結點除了有一個next 域指向下一個結點外,還有一個sibling 指向連結串列中的任意結點或者null。
【劍指Offer學習】【面試題56:連結串列中環的入口結點】
題目:一個連結串列中包含環,如何找出環的入口結點? 解題思路 可以用兩個指標來解決這個問題。先定義兩個指標P1和P2指向連結串列的頭結點。如果連結串列中環有n個結點,指標P1在連結串列上向前移動n步,然後兩個指標以相同的速度向前移動。當第二個指標
【劍指Offer學習】【面試題60:把二叉樹打印出多行】
題目:從上到下按層列印二叉樹,同一層的結點按從左到右的順序列印,每一層列印一行。 解題思路 用一個佇列來儲存將要列印的結點。為了把二叉樹的每一行單獨列印到一行裡,我們需要兩個變數:一個變量表示在當前的層中還沒有列印的結點數,另一個變量表示下一次結
【劍指Offer學習】【面試題50:樹中兩個結點的最低公共祖先】
題目:求樹中兩個結點的最低公共祖先,此樹不是二叉樹,並且沒有指向父節點的指標。 樹的結點定義 private static class TreeNode { int val; List<TreeNode> childre
【劍指Offer學習】【面試題67:機器人的運動範圍】
題目:地上有個m行n列的方格。一個機器人從座標(0,0)的格子開始移動,它每一次可以向左、右、上、下移動一格,但不能進入行座標和列座標的數位之和大於k的格子。 舉例分析 例如,當k為18時,機器人能夠進入方格(35,37),因為3+5+3+7=1
【劍指Offer學習】【面試題31:連續子陣列的最大和】
題目:輸入一個整型陣列,數組裡有正數也有負數。陣列中一個或連續的多個整陣列成一個子陣列。求所有子陣列的和的最大值。要求時間複雜度為O(n)。 例子說明: 例如輸入的陣列為{1, -2, 3, 10, -4, 7, 2, -5},和最大的子陣列為{3,
【劍指Offer學習】【面試題54:表示數值的字串】
題目:請實現一個函式用來判斷字串是否表示數值(包括整數和小數)。 例子說明 例如,字串“+100”,“5e2”,“-123”,“3.1416”及”-1E-16”都表示數值,但“12e”,”1a3.14”,”1.2.3”,”+-5”及“12e+5.
【劍指Offer學習】【面試題18 :樹的子結構】
題目:輸入兩棵二叉樹A 和B,判斷B 是不是A 的子結構。 二叉樹結點的定義: /** * 二叉樹的樹結點 */ public static class BinaryTreeNode { int value; BinaryT
【劍指Offer學習】【面試題13 :在O(1)時間刪除連結串列結點】
程式碼實現: public class Test13 { /** * 連結串列結點 */ public static class ListNode { int value; // 儲存連結串列的值 L
【劍指Offer學習】【面試題11 :數值的整數次方】
程式碼實現: public class Test11 { /** * 實現函式double Power(double base, int exponent),求base的exponent次方。 * 不得使用庫函式,同時不需要考慮大數問題。
【劍指Offer學習】【面試題36:陣列中的逆序對】
題目:在陣列中的兩個數字如果前面一個數字大於後面的數字,則這兩個數字組成一個逆序對。輸入一個數組,求出這個陣列中的逆序對的總數。 舉例分析 例如在陣列{7, 5, 6, 4 中, 一共存在5 個逆序對,分別是(7, 6)、(7,5),(7, 4)
【劍指Offer學習】【面試題28 :字串的排列】
題目:輸入一個字串,打印出該字串中字元的所有排列。例如輸入字串abc。則打印出由字元a、b、c 所能排列出來的所有字串abc、acb、bac 、bca、cab 和cba 。 解題思路: 把一個字串看成由兩部分組成:第一部分為它的第一個字元,第二部分是後面
【劍指Offer學習】【面試題14 :調整陣列順序使奇數位於偶數前面】
題目:輸入一個整數陣列,實現一個函式來調整該陣列中數字的順序,使得所有奇數位於陣列的前半部分,所有偶數位予陣列的後半部分。 這個題目要求把奇數放在陣列的前半部分, 偶數放在陣列的後半部分,因此所有的奇數應該位於偶數的前面。也就是說我們在掃描這個陣列的時候, 如
【劍指Offer學習】【面試題16 :反轉連結串列】
題目:定義一個函式,輸入一個連結串列的頭結點,反轉該連結串列並輸出反轉後連結串列的頭結點。 連結串列結點定義如下: public static class ListNode { int value; ListNode next; } 解
【劍指Offer學習】【面試題27:二叉搜尋樹與雙向連結串列】
題目:輸入一棵二叉搜尋樹,將該二叉搜尋樹轉換成一個排序的雙向連結串列。要求不能建立任何新的結點,只能調整樹中結點指標的指向。 比如輸入圖4.12 中左邊的二叉搜尋樹,則輸出轉換之後的排序現向連結串列。 結點定義: public static
【劍指Offer學習】【面試題38:數字在排序陣列中出現的次數】
題目:統計一個數字:在排序陣列中出現的次數。 舉例說明 例如輸入排序陣列{ 1, 2, 3, 3, 3, 3, 4, 5}和數字3 ,由於3 在這個陣列中出現了4 次,因此輸出4 。 解題思路 利用改進的二分演算法。 如何用二分查詢演算法