Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
A subarray is a contiguous part of an array.
Example 1:
1 2 3
Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6.
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
intcmp(int a, int b){ return (a > b) ? a : b; }
intmaxSubArray(int* nums, int numsSize){ int pre = 0; int max = nums[0]; for(int i = 0; i < numsSize; i++) { pre = cmp(pre + nums[i], nums[i]); max = cmp(pre, max); }
return max; }
136. Single Number
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
1 2
Input: nums = [2,2,1] Output: 1
Solution:
1 2 3 4 5 6 7 8 9 10 11 12
// 经典异或题目
intsingleNumber(int* nums, int numsSize) { for (int i = 1; i < numsSize; i++) { nums[0] = nums[0] ^ nums[i]; } return nums[0]; }
202. Happy Number
Write an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy.
boolisHappy(int n){ int slow = n; int fast = next(n); while (slow != fast && fast != 1) { slow = next(slow); fast = next(next(fast)); } if (fast == 1) returntrue; returnfalse; }
283. Move Zeroes
Given an integer array nums, move all 0‘s to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example 1:
1 2
Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0]
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
voidswap(int *a, int *b) { int t = *a; *a = *b, *b = t; }
voidmoveZeroes(int *nums, int numsSize) { int left = 0, right = 0; while (right < numsSize) { if (nums[right]) { swap(nums + left, nums + right); left++; } right++; } }
122. Best Time to Buy and Sell Stock II
You are given an integer array prices where prices[i] is the price of a given stock on the ith day.
On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.
Find and return the maximum profit you can achieve.
Example 1:
1 2 3 4 5
Input: prices = [7,1,5,3,6,4] Output: 7 Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. Total profit is 4 + 3 = 7.
intmaxDepth(struct TreeNode* root) { if(root == NULL) return0; int left = maxDepth(root->left) + 1; int right = maxDepth(root->right) + 1; if(left > right) return left; return right; }
intdiameterOfBinaryTree(struct TreeNode* root){ if(root == NULL) return0; int leftDep = maxDepth(root->left); int rightDep = maxDepth(root->right); int middleDia = leftDep + rightDep; int maxDia = middleDia; int leftDia = diameterOfBinaryTree(root->left); int rightDia = diameterOfBinaryTree(root->right); if(leftDia > maxDia) maxDia = leftDia; if(rightDia > maxDia) maxDia = rightDia; return maxDia; }
1046. Last Stone Weight
You are given an array of integers stones where stones[i] is the weight of the ith stone.
We are playing a game with the stones. On each turn, we choose the heaviest two stones and smash them together. Suppose the heaviest two stones have weights x and y with x <= y. The result of this smash is:
If x == y, both stones are destroyed, and
If x != y, the stone of weight x is destroyed, and the stone of weight y has new weight y - x.
At the end of the game, there is at most one stone left.
Return the smallest possible weight of the left stone. If there are no stones left, return 0.
Example 1:
1 2 3 4 5 6 7
Input: stones = [2,7,4,1,8,1] Output: 1 Explanation: We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then, we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then, we combine 2 and 1 to get 1 so the array converts to [1,1,1] then, we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of the last stone.
intlastStoneWeight(int* stones, int stonesSize){ // 该重量的石头有几个? int stonesN[1001] = {0}; for(int i = 0; i < stonesSize; i++) { stonesN[stones[i]]++; } // 最大重量y,第二大重量x int y = findBig(stonesN); int x = findBig(stonesN); while(x != 0) { // 摧毁之后再放回去 stonesN[y-x]++; y = findBig(stonesN); x = findBig(stonesN); } return y; }
525. Contiguous Array
Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of0and1.
Example 1:
1 2 3
Input: nums = [0,1] Output:2 Explanation: [0, 1] is the longest contiguous subarray with an equal number of0and1.
Example 2:
1 2 3
Input: nums = [0,1,0] Output:2 Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of0and1.