ARTS-2018.12.12
- Algorithm
LeetCode 26: Remove Duplicates from Sorted Array
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.
描述:使用原地算法返回一個有序數組的不重複元素個數,並將他們按順序排在前面,舉例:
Given nums = [0,0,1,1,1,2,2,3,3,4],返回:5,最終數組前5位爲:[0,1,2,3,4]
我的答案:
public int removeDuplicates(int[] nums){
int length = nums.length;
if (length ==0) return length;
int i=0;
int j=1;
while (j <= length - 1) {
if (nums[i] == nums[j]) {
j++;
}else {
nums[i + 1] = nums[j];
i++;
j++;
}
}
return ++i;
}
思路就是2個指針i,j,遇到相同的元素,j往後移動,否則把後面的元素拿到 i+1位,同時 i 向後移動一位。以下是標準答案。
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
- Review
學習了一下OAuth 2.0 的認證流程,這篇文章很不錯,流程圖很清晰,OAuth 2.0的4種認證流程做了清晰說明和區別,並給了要使用OAuth 2.0 的技術選型參考。貼一下流程圖:
- Authorization Code Grant
- Implicit Grant
- Client Credential Grant
- Password Grant
4種驗證模式的技術選擇要求:
Which to choose ?
- Tip
最近在學習Spring Security
學習到一個非常有用的技巧:
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.debug(true);
}
}
在配置類中打開debug 模式,整個 Spring Security Filter的流程就都會打印出來,非常方便理解整個過程如何進行的,舉個例子:
Request received for GET '/login':
org.apache.catalina.connector.RequestFacade@6bd6b5fa
servletPath:/login
pathInfo:null
headers:
host: localhost:8080
connection: keep-alive
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
accept: image/webp,image/apng,image/*,*/*;q=0.8
referer: http://localhost:8080/
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7
Security filter chain: [
WebAsyncManagerIntegrationFilter
SecurityContextPersistenceFilter
HeaderWriterFilter
LogoutFilter
IpAuthenticationProcessingFilter
UsernamePasswordAuthenticationFilter
RequestCacheAwareFilter
SecurityContextHolderAwareRequestFilter
AnonymousAuthenticationFilter
SessionManagementFilter
ExceptionTranslationFilter
FilterSecurityInterceptor
]
************************************************************
- Share
這段時間每天都加班到很晚,除了項目很急之外都是一些很瑣碎的事,很多時候是因爲開發前沒有理清總體的思路,導致在開發的過程中經常因爲細節問題推倒重來,我想這也許就是差距吧,努力提高自己的整體思維能力吧,在動手之前多畫畫流程圖,類圖,也許更能提高效率。