Ruizhi Ma's Blog

Keep Calm and Carry On

Problem 36

Valid Sudoku

问题描述 https://leetcode.com/problems/valid-sudoku/ 解决思路 遍历对每一行判断;遍历对每一列判断;遍历对每3*3的小方块内部遍历 问题解惑 row < (i / 3) * 3 + 3是什么意思? 更具i从0~9,(i / 3) * 3 + 3的结果依次是:3-3-3-6-6-6-9-9-9。这将i=0~3的时候,row限制在前三个...

Problem 35

Search Insert Position

问题描述 https://leetcode.com/problems/search-insert-position/ 解决思路一 比较简单,直接遍历数组,不断地将小于target的元素的下标加1给target就可以。 代码一 class Solution { public int searchInsert(int[] nums, int target) { //c...

Problem 34

Find First and Last Position of Element in Sorted Array

问题描述 https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ 解决思路 遇到排序数组中找元素,首推二分法。分别找首尾两个元素,获得下标后返回。 问题解惑 我当时以为findFirst和findLast两个函数一模一样,不就是if else调换了一下嘛?实则不然,关键在...

Problem 33

Search in Rotated Sorted Array

问题描述 https://leetcode.com/problems/search-in-rotated-sorted-array/ 解决思路 遇到这种排序的,还是首推二分法。这个题里面不管从哪里旋转,一定有一段是升序排列的。分情况讨论:1. 如果mid刚好在有序的里面怎么办? 2. 如果mid在有序的外面怎么办?具体看代码,走一遍就看懂了 代码 class Solution { ...

Problem 31

Next Permutation

问题描述 https://leetcode.com/problems/next-permutation/ 解决思路 不太会,没能理解。只能先把代码贴上来。 问题解惑 代码 class Solution { public void nextPermutation(int[] nums) { if(nums == null || nums.length == 0) ...

Problem 196

Delete Duplicate Emails

问题描述 https://leetcode.com/problems/delete-duplicate-emails/ 代码 # Write your MySQL query statement below DELETE p1 FROM Person as p1, Person as p2 WHERE p1.Email = p2.Email AND p1.Id > p2.Id;

Problem 182

Duplicate Email

问题描述 题目看链接:https://leetcode.com/problems/duplicate-emails/ 解决思路 Inner join相当于取两个表的交集;Distinct是返回唯一不同的值。 代码 # Write your MySQL query statement below SELECT DISTINCT tb1.Email FROM Person as tb1 IN...

Problem 29

Divide Two Integers

问题描述 https://leetcode.com/problems/divide-two-integers/ 解决思路 这道题不可以用除法,不可以用取模运算。举例35和3,思路如下: 3«0,3 3«1,6 3«2,12 3«3,24 3«4,48. 此时48>35,所以只能左移3次到24,38等于24,留下8.此时35-24=11 3«0,3 3«1,6 3«2,12 此...

Problem 9

Palindrome Number

问题描述 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false ...

Problem 8

问题描述 https://leetcode.com/problems/string-to-integer-atoi/ 解决思路 直接看代码吧,题目就是有点麻烦,不是很难,注释把思路写的够清晰了 代码 class Solution { public int myAtoi(String str) { //process the whitespace ...