最近作的題記錄下。node
Given a non-negative integer num
, repeatedly add all its digits until the result has only one digit.git
For example:數組
num = 38
, the process is like:
3 + 8 = 11
,
1 + 1 = 2
. Since
2
has only one digit, return it.
1 int addDigits(int num) { 2 char strint[12] = {0}; 3 sprintf(strint, "%d", num); 4 int i=0,a = 0; 5 while(strint[i+1]!= '\0') 6 { 7 a = (strint[i]-'0')+(strint[i+1]-'0'); 8 if(a>9) 9 { 10 a = a%10+a/10; 11 } 12 strint[++i] = a+'0'; 13 } 14 return strint[i]-'0'; 15 }
上邊這是O(n)的複雜度。網上還有O(1)的複雜度的:return (num - 1) % 9 + 1;app
104. Maximum Depth of Binary Treedom
求二叉樹的深度,能夠用遞歸也能夠用循環,以下:spa
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * struct TreeNode *left; 6 * struct TreeNode *right; 7 * }; 8 */ 9 int maxDepth(struct TreeNode* root) { 10 int left = 0,right = 0; 11 if (root == NULL) return 0; 12 13 left = maxDepth(root->left); 14 right = maxDepth(root->right); 15 16 return left>right? left+1:right+1; 17 }
******************************************************************************************************************code
用隊列存結點,記錄每一層的結點數levelCount,每出一個結點levelCount--,若是減爲0說明要進入下一層,而後depth++,同時隊列此時的結點數就是下一層的結點數。blog
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int maxDepth(TreeNode* root) { 13 if (root == NULL) return 0; 14 15 //深度和每一層節點數 16 int depth = 0,levelCount = 1; 17 //隊列保存每一層的節點數 18 queue<TreeNode*> node; 19 20 node.push(root); 21 while(!node.empty()) 22 { 23 //依次遍歷每個結點 24 TreeNode *p = node.front(); 25 node.pop(); 26 levelCount--; 27 28 if(p->left) node.push(p->left); 29 if(p->right) node.push(p->right); 30 31 if (levelCount == 0) 32 { 33 //保存下一層節點數,深度加1 34 depth++; 35 levelCount = node.size(); 36 } 37 } 38 return depth; 39 } 40 };
Given a sorted array of integers, find the starting and ending position of a given target value.遞歸
Your algorithm's runtime complexity must be in the order of O(log n).隊列
If the target is not found in the array, return [-1, -1]
.
[5, 7, 7, 8, 8, 10]
and target value 8,
[3, 4]
1 vector<int> searchRange(vector<int>& nums, int target) { 2 int size = nums.size(); 3 int l=0,r=size-1; 4 while(l<=r) 5 { 6 int mid = (l+r)/2; 7 if (nums[mid] >= target) 8 { 9 r = mid-1; 10 } 11 else if(nums[mid] < target) 12 { 13 l = mid+1; 14 } 15 } 16 int left = l; 17 l = 0; 18 r = size-1; 19 while(l<=r) 20 { 21 int mid = (l+r)/2; 22 if (nums[mid] <= target) 23 { 24 l = mid+1; 25 } 26 else if(nums[mid] > target) 27 { 28 r = mid-1; 29 } 30 } 31 int right = r; 32 vector<int> v; 33 v.push_back(left); 34 v.push_back(right); 35 if(nums[left] != target || nums[right] != target) 36 { 37 v[0] = -1; 38 v[1] = -1; 39 } 40 return v; 41 }
這個題就是要把握好邊界,好比找左邊界時 =號要給右邊界的判斷,找右邊界則相反。這樣才能保證不遺漏
1 int singleNumber(vector<int>& nums) { 2 int result=0; 3 int len = nums.size(); 4 if (len <=0) return 0; 5 for(int i=0;i<len;i++) 6 { 7 result ^= nums[i]; 8 } 9 return result; 10 }
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
1 char findTheDifference(string s, string t) { 2 map<char, int> sumChar; 3 char res; 4 for(auto ch: s) sumChar[ch]++; 5 for(auto ch: t) 6 { 7 if(--sumChar[ch]<0) 8 res = ch; 9 } 10 return res; 11 }
這道題也能夠用異或 由於相同的會異或掉,剩下一個就是多餘的char。