[置顶] 【LeetCode】LeetCode中那些应该背下来的经典代码
2017-03-17 10:52
295 查看
53. Maximum Subarray
问题描述
问题链接:https://leetcode.com/problems/maximum-subarray/#/descriptionFind the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.
经典解法
public static int maxSubArray(int[] A) { int maxSoFar=A[0], maxEndingHere=A[0]; for (int i=1;i<A.length;++i){ maxEndingHere= Math.max(maxEndingHere+A[i],A[i]); maxSoFar=Math.max(maxSoFar, maxEndingHere); } return maxSoFar; }
136. Single Number中使用异或求解
问题描述
问题链接:https://leetcode.com/problems/single-number/#/descriptionGiven an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
经典解法
known that A XOR A = 0 and the XOR operator is commutative, the solution will be very straightforward.int singleNumber(int A[], int n) { int result = 0; for (int i = 0; i<n; i++) { result ^=A[i]; } return result; }
526. Beautiful Arrangement中回溯法求解
问题描述
问题链接:https://leetcode.com/problems/beautiful-arrangement/#/descriptionSuppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 ≤ i ≤ N) in this array:
The number at the ith position is divisible by i.
i is divisible by the number at the ith position.
Now given N, how many beautiful arrangements can you construct?
Example 1:
Input: 2 Output: 2 Explanation: The first beautiful arrangement is [1, 2]: Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1). Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2). The second beautiful arrangement is [2, 1]: Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1). Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1.
Note:
N is a positive integer and will not exceed 15.
经典解法
public class Solution { private int count = 0; public int countArrangement(int N) { /* 思路是使用回溯法。如果当前的解满足要求,就继续向下走去探索子节点,否则就返回 是一种优秀的剪枝算法 */ int[] nums = new int[N + 1]; for(int i = 1; i <= N; i++){ nums[i] = i; } backTracking(nums,1); return count; } private void backTracking(int[] nums, int start){ if(start >= nums.length){ count++; // 找到一个解 return; } for(int i = start; i < nums.length; i++){ swap(nums,i,start); if(nums[start] % start == 0 || start % nums[start] == 0){ // 子节点可能有解 backTracking(nums,start + 1); } swap(nums,start,i); } } private void swap(int[] nums, int from, int to){ int temp = nums[from]; nums[from] = nums[to]; nums[to] = temp; } }
【LeetCode】357. Count Numbers with Unique Digits
问题描述
问题链接:https://leetcode.com/problems/count-numbers-with-unique-digits/#/descriptionGiven a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
Hint:
A direct way is to use the backtracking approach.
经典代码
Thanks for sharing. I think it could be simplified further. This problem is kind of like permutation + subset, so we start from 0b45b
every recursion and count through the path. Forgive me if anything unclear, here is the code:
public class Solution { public int countNumbersWithUniqueDigits(int n) { return doCount(n, new boolean[10], 0); } private int doCount(int n, boolean[] used, int d) { if (d == n) return 1; int total = 1; for (int i = (d == 0) ? 1 : 0; i <= 9; i++) { if (!used[i]) { used[i] = true; total += doCount(n, used, d + 1); used[i] = false; } } return total; } }
相关文章推荐
- PHP经典分页代码-带数据库文件及实例-复制下来就能用
- [置顶] JAVA的六大经典算法,代码案例简化分析
- 史上最全!最经典!最无私的Android资料(书籍+代码)分享-不要积分(求置顶)!- ORIEtech[问题点数:100分]
- 写了几年代码了,苦苦追寻,应该沉淀下来了,好好研究。net底层框架,以及较好的分层框架
- 写了几年代码了,苦苦追寻,应该沉淀下来了,好好研究。net底层框架,以及较好的分层框架
- 为什么很多人宁可天天去读那些个所谓的看似能学到东西却没卵用的博文,都不肯好好静下来写写代码呢?
- android提交代码 应该忽略那些文件
- [置顶] 那些烂代码教给我的事
- leetcode那些让人长见识的代码
- 编程规范-程序员们都应该这样写代码
- Jive论坛与Spring框架,经典代码和架构!
- 住房贷款、消费贷款计算器代码--拷贝下来就能用
- 网页经典代码
- 插入数据的经典代码
- 网络推销经典案例——所有的骗子都应该向他学习
- 14 个经典的Javascript代码
- Jive论坛与Spring框架,经典代码和架构!
- [转]今天看到的一篇js屏蔽键盘的代码,经典!
- 网页经典代码
- 不知道应该怎么加这行代码