Java 【LeetCode】48. Rotate Image 问题描述 https://leetcode.com/problems/rotate-image/#/description You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place?
Java 【LeetCode】46. Permutations 问题链接:https://leetcode.com/problems/permutations/#/description 求数组的全排列 算法 一个个的将每一个数插入数组,如数组[1,2,3],设结果二重数组为r,初始化r中只有一个数组r=[[1]], 然后要插入数据2,有两种插法,[1,2]和[2,1],即插在1前面和后面,从而r里有了两个数组r=[[1,2],[2,1]],接下来插入数据3,对于数组 [1,
Java 【LeetCode】43. Multiply Strings 问题描述 https://leetcode.com/problems/multiply-strings/#/description Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2. Note: * The length of both num1 and num2 is
Java 【LeetCode】42. Trapping Rain Water 问题描述 https://leetcode.com/problems/trapping-rain-water/#/description Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after
Java 【LeetCode】41. First Missing Positive 问题描述 https://leetcode.com/problems/first-missing-positive/#/description Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2.
Java 【LeetCode】40. Combination Sum II 问题描述 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be
Java 【LeetCode】39. Combination Sum 问题描述 https://leetcode.com/problems/combination-sum/#/description Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to
Java 【算法学习】二分查找算法 二分查找算法用于在一列已排序的数组中找出一个数的位置。 假设下面有一列已排序的数:[a1,a2,a3,a4,...an],有a1<=a2<=a3<=a4<=...<=an 当我们要查询一个数a是否在数组中,直接的办法就是一个个比对,找到了即返回所在位置,没找到就返回-1;这种方法的时间复杂度是O(n)。 设有n个数,二分查找是先找数组[1..n]中间的那个数M,设其位置为i 1. 如果a比M小,则a肯定在M的左边,下一轮查数组[1..i-1]; 2. 如果a比M大,那a肯定在M的右边,
leetcode 28. Implement strStr() 问题描述 Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 思路 直接遍历haystack,然后一个一个字符去与needle匹配,完全匹配则成功;失败则迭代到下一字符,直到haystack.length - needle.length为止。
leetcode 【LeetCode】292. Nim Game 问题链接:https://leetcode.com/problems/nim-game/ 尼姆游戏,一堆石子放在桌子上,两个人轮流拿,一次可以拿1-3颗石子,谁拿走最后一颗谁就赢了。 如4颗石子时,先手无论拿几颗,都会输了博弈,因为他至少得拿1颗,剩下的就是3颗,后手可一次全拿获胜。所以如果一堆石子放在桌子上,如果轮到这个人的时候面对的是4颗石子,那他肯定输,而另一个人肯定赢。 代码: public boolean canWinNim(int n) { return !(n%4==0); }
leetcode 【LeetCode】290. Word Pattern 问题链接:https://leetcode.com/problems/word-pattern/ 思路: 使用一个map存储映射关系即可 public boolean wordPattern(String pattern, String str) { String first = pattern; String[] second = str.split(" "); if(first.length()!=second.length) { return false; } Map m
leetcode 【LeetCode】283. Move Zeroes 问题链接:https://leetcode.com/problems/move-zeroes/ 思路: 1. 直观版本,遍历,如果一个数是0,就把它后面的所有不为0的数往前挪。O(n2) public void moveZeroes(int[] nums) { int c = 0; for(int i=0;i
leetcode 【LeetCode】278. First Bad Version 问题描述 You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based
leetcode 【LeetCode】263. Ugly Number 问题描述 Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly
leetcode 【LeetCode】258. Add Digits 问题描述 Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2.
leetcode 【LeetCode】257. Binary Tree Paths 问题描述 Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / \ 2 3 \ 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 输出二叉树所有从根节点到叶子节点的路径。递归即可。 代码 class
leetcode 【LeetCode】242. Valid Anagram 问题描述 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false.
leetcode 【LeetCode】237. Delete Node in a Linked List 问题描述 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 ->
leetcode 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 问题描述 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is
leetcode 【LeetCode】234. Palindrome Linked List 问题描述 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 问题分析 使用快慢指针找到中点,然后将后半截链表逆置,再将后半截与前半截进行比较,如果有一个数不相等则返回false;否则返回true。 代码
leetcode 【LeetCode】232. Implement Queue using Stacks 问题描述 Implement the following operations of a queue using stacks. * push(x) -- Push element x to the back of queue. * pop() -- Removes the element from in front of queue. * peek() --
leetcode 【LeetCode】231. Power of Two 问题描述 Given an integer, write a function to determine if it is a power of two. 判断一个数是否为2的次方数 思路 1. 当n>1时迭代,如果n%2!=0,即n不是2的倍数,直接false,否则令n=n/2,继续下一轮迭代。结束循环后,若n=1则为次方数。
leetcode 【LeetCode】226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired by this original tweet by Max Howell: > Google: 90%
leetcode 【LeetCode】225. Implement Stack using Queues 问题描述 Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top
leetcode 【LeetCode】223. Rectangle Area Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Rectangle Area