编程 4

Leetcode – Excel Sheet Column Number

By admin in 编程 on 2019年10月8日

编程 1

My code:

编程 2Paste_Image.png

My code:

public class Solution { public int findDuplicate(int[] nums) { if (nums == null || nums.length == 0) return -1; int begin = 1; int end = nums.length; while (begin < end) { int mid = begin + (end - begin) / 2; int counter = 0; for (int i = 0; i < nums.length; i++) if (nums[i] <= mid) counter++; if (counter > mid) end = mid; else begin = mid + 1; } return begin; }}

My code:

public class Solution { public int titleToNumber { if (s == null || s.length return 0; int number = 0; for (int i = 0; i < s.length number = number * 26 + s.charAt - 64; return number; }}

那道标题自个儿没能想出来。感觉某些考智力商数。这么些主张作者确实没想出来。首先贴三个博文。

public class Solution { public int search(int[] nums, int target) { if (nums == null || nums.length == 0) return 0; return search(0, nums.length - 1, target, nums); } private int search(int begin, int end, int target, int[] nums) { if (begin > end ) return -1; int mid = (begin + end) / 2; if (nums[mid] < nums[end]) {// right part is sorted so minimun exists in the left if (target < nums[mid]) return search(begin, mid - 1, target, nums); else if (target > nums[mid]) if (target > nums[end]) return search(begin, mid - 1, target, nums); else return search(mid + 1, end, target, nums); else return mid; } else {// left part is sorted so minimun exists in the right if (target > nums[mid]) return search(mid + 1, end, target, nums); else if (target < nums[mid]) if (target <= nums[end]) return search(mid + 1, end, target, nums); else return search(begin, mid - 1, target, nums); else return mid; } }}

My test result:

接下来那道难点有O
的做法。可是以为思路太复杂了。就概况了。然后揣摩了下他的第三种做法,
O比如:假如n = 9那么,有拾贰个位贮存入1-9势必会有重复。取中间值 1 + /
2 = 5;在1-9中, <= 5
的应有有5个,假使不重复。可是要是1-5里头有数字再次了,那么,就应当高于5个了。所以,遍历下整个数组,总括<=5 的个数,如若当先5,那么就表示,那么些重复的数字,他的值v , 1 <=
v <= 5也等于begin <= v <= mid反之,则是:mid + 1 <= v <=
end于是利用binary search
的思索,一步步往下降低范围。最后,就能够把v限定在几个显著值里面,此时,begin
= end所以,最后能够找到这么些值。

My test result:

编程 3

**总结: 不在是binary search array, 而是 通过 binary search
来界定叁个大概取值的范围。最后到达指标。**

编程 4Paste_Image.png

那道问题太轻松了,认为微微凌辱智力商数。

Anyway, Good luck, Richardo!

此番作业也幸亏,因为早就有了事先的思绪。恐怕太累了,AC之后,本人都记不清本人写的什么样思路了。。。应该就是先确定,最小值会现出在哪一侧,然后再将目的值与中间值比较,当中会有个相比较复杂的动静,即被找寻数或者会产出在mid两边,此时内需再与
nums[end] 比较下,就能够做出更为的论断了,然后再一次递归。就那样类推。

**总结: Math**

My cdoe:

累死了。

Anyway, Good luck, Richardo!

public class Solution { public int findDuplicate(int[] nums) { if (nums == null || nums.length == 0) return -1; int begin = 1; int end = nums.length -1; // end = n while (begin < end) { int target = (begin + end) / 2; int count = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] >= begin && nums[i] <= target) count++; } if (count > target - begin + 1) { end = target; } else { begin = target + 1; } } return begin; }}

**总结: Array, BinarySearch**

My code:

寒假在家作用实在太低。那道难题再做也依然不曾做出来。shit思路依然听玄妙地。Anyway,
Good luck, Richardo!

Anyway, Good luck, Richardo!

public class Solution { public int titleToNumber { if (s == null || s.length { return 0; } int base = 1; int ret = 0; for (int i = s.length() - 1; i >= 0; i--) { char curr = s.charAt; int diff =  (curr - 'A') + 1; ret += diff * base; base *= 26; } return ret; }}

My code:

My code:

开头的解法竟然看不懂。。。还口出狂言,污辱智力商数。。。

public class Solution { public int findDuplicate(int[] nums) { if (nums == null || nums.length == 0) { return -1; } int i = 0; while (i < nums.length) { if (nums[nums[i] - 1] != nums[i]) { int temp = nums[nums[i] - 1]; nums[nums[i] - 1] = nums[i]; nums[i] = temp; } else if (nums[i] - 1 == i) { i++; continue; } else { return nums[i]; } } return -1; }}
public class Solution { public int search(int[] nums, int target) { if (nums == null || nums.length == 0) return -1; int begin = 0; int end = nums.length - 1; while (begin <= end) { int middle = (begin + end) / 2; if (nums[middle] < nums[end]) { // right part is sorted if (target < nums[middle]) { end = middle - 1; } else if (target == nums[middle]) { return middle; } else if (target <= nums[end]) { begin = middle + 1; } else { end = middle - 1; } } else { // left part is sorted if (target > nums[middle]) { begin = middle + 1; } else if (target == nums[middle]) { return middle; } else if (target >= nums[0]) { end = middle - 1; } else { begin = middle + 1; } } } return -1; }}

是本身变笨了吧?

那道题指标沉思和

那道标题自个儿看以前笔者的解法是用递归来做的,这一次直接用循环来做了。感到大约。三个注意点,也是自家第一遍提交没经过的地方,while
(begin <= end) 记住,在 find minimum element in rotated sorted array
中,是 <而那边是, <=因为在,find minimum element in rotated
sorted array 中,当begin ==
end时,就约等于已经找到了那几个小小的值。所以直接退出循环了。而在search中,begin
== end时,还索要看清下 nums[begin]
是或不是等于target。不能平素退出循环的。

编程,Fuck

然后看了下从前的做法,也是真的丰富多彩啊。认为复杂度也是O啊 ?

Anyway, Good luck, Richardo!

自个儿的想想是找规律:A-Z: 1-26AA-AZ: 27-52BA-BZ: 53-78…AAA: 703

Anyway, Good luck, Richardo! — 09/01/2016

My code:

AAA = 1 * 26 ^ 2 + 1 * 26 ^ 1 + 1 * 26 ^ 0

风行的做法不太对。因为自身修改了原数组。那是不容许的。所以一齐始的做法才是科学的。

public class Solution { public int search(int[] nums, int target) { if (nums == null || nums.length == 0) { return -1; } int begin = 0; int end = nums.length - 1; while (begin <= end) { int middle = begin + (end - begin) / 2; if (target < nums[middle]) { if (nums[middle] > nums[end]) { if (target < nums[end]) { begin = middle + 1; } else if (target > nums[end]) { end = middle - 1; } else { return end; } } else { end = middle - 1; } } else if (target > nums[middle]) { if (nums[middle] > nums[end]) { begin = middle + 1; } else { if (target > nums[end]) { end = middle - 1; } else if (target < nums[end]) { begin = middle + 1; } else { return end; } } } else { return middle; } } return -1; }}

从那一个公式,也足以看出Excel Sheet Column Title那么做的案由了。

Anyway, Good luck, Richardo! — 09/11/2016

好多的笔触。Anyway, Good luck, Richardo! — 08/12/2016

int offset =  % 26;while  { ret = ('A' + offset) + ret; n =  / 26; // 去掉 k * , here 1<=k<=26, so n - 1 col =  % 26; // keep finding the offset of next level}

不用写的如此复杂。

倍感大约就以此思路。

My code:

Anyway, Good luck, Richardo! — 09/05/2016

public class Solution { public int search(int[] nums, int target) { if (nums == null || nums.length == 0) { return -1; } int lo = 0; int hi = nums.length - 1; while (lo < hi) { int mid = lo +  / 2; if (nums[mid] < nums[hi]) { hi = mid; } else { lo = mid + 1; } } int rotate = lo; lo = 0; hi = nums.length - 1; while (lo <= hi) { int mid = lo +  / 2; int realMid = (mid + rotate) % nums.length; if (nums[realMid] == target) { return realMid; } else if (nums[realMid] > target) { hi = mid - 1; } else { lo = mid + 1; } } return -1; }}

这个天有了新的思绪。其实正是一个26进制调换。

reference:

‘B’ * 26 ^ 0 + ‘A’ * 26 ^ 1= 2 * 1 + 1 * 26 = 28

把它当成一个专门的学问的array去找,然后坐标再改变回rotate的array,去收取我们必要的中间值。

下一场能够快捷的写出代码:

Anyway, Good luck, Richardo! — 09/12/2016

My code:

My code:

public class Solution { public int titleToNumber { if (s == null || s.length { return 0; } int base = 1; int ret = 0; for (int i = s.length() - 1; i >= 0; i--) { int offset =  (s.charAt + 1; ret += base * offset; base *= 26; } return ret; }}
public class Solution { public int search(int[] nums, int target) { int begin = 0; int end = nums.length - 1; while (begin <= end) { int mid = begin + (end - begin) / 2; if (nums[mid] == target) { return mid; } else if (nums[mid] < nums[end]) { // right is sorted if (nums[mid] < target && target <= nums[end]) { begin = mid + 1; } else { end = mid - 1; } } else { // left is sorted if (nums[begin] <= target && target < nums[mid]) { end = mid - 1; } else { begin = mid + 1; } } } return -1; }}

好不轻巧成为了 easy

实际不是搞那么复杂。间接用这些艺术。假如有双重。 end–Anyway, Good luck,
Richardo! — 09/26/2016

Anyway, Good luck, Richardo! — 09/19/2016

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 澳门新葡亰官网app 版权所有