![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
RojerAlone
脚踏实地,笨鸟先飞。
展开
-
LeetCode - Two Sum
题目 给了一个数组和一个目标值target,求数组中哪两个数的和等于target,返回坐标。 Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].思路 粗暴的解决办法就是遍历,时间复杂度为O(n!)。这样的思路是,先找到一个数,再去遍历查找满足原创 2017-03-23 17:24:03 · 309 阅读 · 0 评论 -
LeetCode - Complex Number Multiplication
题目 输入两个 a+bi格式的字符串,计算它们的乘积,i^2=-1,输出结果也为a+bi的形式。 如 输入”1+1i”, “1+1i” (1 + i) * (1 + i) = 1 + i^2 + 2 * i = 2i,输出0+2i思路 从字符串中抽取出数字进行计算,计算过后再进行拼装结果字符串。代码 public String complexNumberMultiply(String原创 2017-06-27 15:08:59 · 345 阅读 · 0 评论 -
LeetCode - Array Partition I
题目 Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as po原创 2017-06-27 14:51:47 · 303 阅读 · 0 评论 -
LeetCode - Encode And Decode TinyURL
题目 TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/design-tinyurl and it returns a short URL such as http://tinyurl.com/4e9iAk. Design the encode原创 2017-06-27 11:50:06 · 401 阅读 · 0 评论 -
LeetCode - Hamming Distance
题目汉明距离-百度百科 Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different.思路 将两个数异原创 2017-06-27 11:46:03 · 325 阅读 · 0 评论 -
LeetCode - CountingBits
题目 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array. Example: For num原创 2017-06-27 16:54:19 · 289 阅读 · 0 评论 -
LeetCode - Number Complement
题目 求一个数的二进制每一位取反后的数。思路 运用异或的特性,101 ^ 111 = 010,任意数字和n个1的二进制异或,相当于每一位取反。代码public int findComplement(int num) { int res = num; int tmp = 1; // 每循环一次,tmp乘2 while (num > 0) { tmp原创 2017-06-27 15:54:41 · 294 阅读 · 0 评论 -
LeetCode - Merge Two Binary Trees
题目 Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new bin原创 2017-06-27 15:27:46 · 335 阅读 · 0 评论 -
LeetCode - 4SumⅡ
题目 输入4个大小相等的数组,计算有多少个元组 (i, j, k, l) 满足 A[i] + B[j] + C[k] + D[l] = 0。 Input: A = [ 1, 2] B = [-2,-1] C = [-1, 2] D = [ 0, 2] Output: 2 Explanation: The two tuples are: 1.原创 2017-03-24 22:06:07 · 294 阅读 · 0 评论 -
LeetCode - 4Sum
题目 输入一个数组和一个目标值,返回所有的4个数的和等于目标值的组合。思路 这个问题是 TwoSum 、3Sum 的变种,和3Sum一样,先确定一个值,然后从剩下的数组中找一个3Sum的结果,再把当前值加入,逐次遍历即可。代码public List<List<Integer> > fourSum(int[] nums, int target) { List<List<Integer> >原创 2017-03-24 21:34:19 · 290 阅读 · 0 评论 -
LeetCode - 3Sum Closest
题目 输入一个数组和一个目标值target,返回数组中任意3个数的和最接近target的值。思路 和 3Sum 类似,每次先确定一个数的值,然后计算选出来的两个数和当前确定的数的和,进行比较,如果和目标值相同,则返回目标值,否则选取更接近的值作为结果值。继续计算,知道找到和目标值相同的值或者计算完毕。代码public int threeSumClosest(int[] nums, int tar原创 2017-03-23 21:10:11 · 355 阅读 · 0 评论 -
LeetCode - 3Sum
题目 给一个数组,找出所有的三个数的和为0的组合,不能有重复。思路 遍历数组,每次先确定3个数字中的一个,那么剩下的两个就是TwoSum问题。本题中不要求返回坐标,那么可以将数组进行排序,用 LeetCode - Two Sum II - Input array is sorted 的方法求解。代码public List<List<Integer>> threeSum(int[] nums) {原创 2017-03-23 19:23:32 · 273 阅读 · 0 评论 -
LeetCode - Two Sum II - Input array is sorted
题目 在Two Sum的基础上进行了修改,输入数组是排序的。思路 和《剑指Offer》的剑指Offer系列-面试题41:和为s的两个数字以及和为s的正数序列 一样,不再赘述。 当提交过后以为结果一定是乐于看见的AC,结果发现出现了Submission Result: Time Limit Exceeded错误。查看错误用例,发现是下面这玩意: [0,0,0,0,0,0,0,0,0,0,原创 2017-03-23 17:59:36 · 350 阅读 · 0 评论 -
LeetCode - Keyboard Row
题目 给一个字符串数组,返回这些字符串中 所有字符在美式键盘中在同一行的字符串, 比如 qwe 和 asd 都在同一行,qaz 就不在同一行。思路 将每一行的字符放在 map 中,每一行的每个字符对应该行的 index,获取字符串的第一个字符,遍历字符串中的每个字符,如果和第一个字符所在行不一致就进入下一轮遍历。代码public class KeyboardRow { private st原创 2017-06-27 22:46:21 · 337 阅读 · 0 评论