Algorithm
LeetCode 第26题,简单。 最近一周没有太多时间写,还是自己的时间规划的不够好。要谨记于心。
Description
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1:
Given nums = **[1,1,2]**,
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = **[0,0,1,1,1,2,2,3,3,4]**,
Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
It doesn't matter what values are set beyond the returned length.
Clarification: Confused why the returned value is an integer but your answer is an array? Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well. Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
本题是要移除重复的元素,而且要在数组内部进行操作(in-place),不能有另外的数据结构了。 移除重复元素,返回不重复的元素个数。 思路: 用两个指针,一个指针(a)循环遍历,另一个指针(b)指着不重复元素。所有,遍历的时候,当 a 指针元素等于 b 指针元素的时候,a 指针后移,b不动;当 a 指针元素不等于 b 指针元素时(注意,此题传入的是排序后的数组,若未排序,肯定也是需要排序的),b 就可以往后移动一位,同时 b 元素此时等于 a 元素。以此循环遍历,最后b指针所在就是不重复元素的最后一个。也就有了不重复元素的长度。
solution
public int removeDuplicates(int[] nums) {
int index = 1;
int comp = nums[0];
for (int i=1;i<nums.length; i++) {
if (nums[i] == comp) {
continue;
}else {
comp = nums[i];
nums[index] = nums[i];
}
}
return index;
}
test
@Test
public void removeDuplicates() {
RemoveDuplicates solution = new RemoveDuplicates();
int[] nums;
nums = new int[]{1,1,2};
assertEquals(2,solution.removeDuplicates(nums));
nums = new int[]{0,0,1,1,1,2,2,3,3,4};
assertEquals(5,solution.removeDuplicates(nums));
}
Review
To Serve Man, with Software
就像题目所说的,软件,是为了更好的服务人类,这大概就是顶级程序员能够有的境界了吧。
Software is easy to change to make it better,and software can also encourage people to do better things and help others.
And you know why I do it? I need that help, too. I get tired, angry, upset, emotional, cranky, irritable, frustrated and I need to be reminded from time to time to choose to be the better version of myself. I don’t always succeed. But I want to. And I believe everyone else – for some reasonable statistical value of everyone else – fundamentally does, too.
作者说他也都会忧郁,烦恼,生气,疲劳,因此我们需要时时谨记让自己成为更好的自己。 这也是Stack Overflow 建立背后的哲学。它建立了一个社区,去帮助那些程序员们成为更好的程序员。如果有更多的人如此遵循这要的哲理,那么就能够服务更多的人。
Tip
在用服务器调试接口的时候,发现curl这个的用法自己还是只会非常基础的。比如说:
curl https://www.lifewire.com/example-uses-of-the-linux-curl-command-4084144
这个直接会直接在终端显示网页情况。
而如果加上-o或者-O,就可以把页面写入到文件之中去。
比分说:
curl -o curl_example.html https://www.lifewire.com/example-uses-of-the-linux-curl-command-4084144
文件就是curl_example.html。
如果是大写的O,就会用url后面一部分作为文件名。
curl -O https://www.lifewire.com/example-uses-of-the-linux-curl-command-4084144
文件名就是:example-uses-of-the-linux-curl-command-4084144。
当然也可以直接用linux 里面的流导向:
curl https://www.lifewire.com/example-uses-of-the-linux-curl-command-4084144 >test.html
无论那种,在down页面的时候,都会有下面的一些统计数据:

多文件的话:
curl -O URL1 -O URL2
当遇到url 重定向问题的时候,简单的curl没办法追踪:
$ curl t.tt
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
这种Moved的问题只是简单的curl 没办法读取,需要用到-L,它会跟踪Http里面的header中的location进行重定向。
curl -v t.tt
* About to connect() to t.tt port 80 (#0)
* Trying 140.143.179.117...
* Connected to t.tt (140.143.179.117) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: t.tt
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Date: Tue, 28 Aug 2018 01:03:45 GMT
< Content-Type: text/html
< Content-Length: 178
< Connection: keep-alive
< Location: https://www.smartisan.com
< Server: ARTWS/1.0
< X-XSS-Protection: 1;mode=block
<
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host t.tt left intact
curl -L t.tt (这是锤子的官网,会跳转header中的Location:www.smartisan.com)
以上三种方法(">"|"-o"|"-O")可以把url地址的结果写入文件中用法可以很方便的用来下载文件。当然,类似的还有wget。
更多的用法如果可以的话,可以直接man curl或者curl --help;其实学linux 就是看manu。
然后可以看一下基础的练习:15 example
Share
Repeat yourself, do more than one thing, and rewrite everything
在程序员的修炼之道一书中有提到,Do Not Repeat Yourself,本文有另一个角度的观点,可以一看。