myveryfirstacceptance!

I got my very first acceptance today!!!

This is the very first solution I come up with myself and write the code independently! So excited! Although my code has 30 rows and the solution on leetcode has only 10 row…

Check my code for Remove element from a array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class  {
public int removeElement(int[] nums, int val) {
if(nums.length==0){
return 0;
}
int j=-1, temp, len=0, i;
for (i=0;i<nums.length; i++){
if(nums[i]==val){
j=i;
while(j<nums.length-1){
j++;
if(nums[j]!=val){

temp=nums[j];
nums[j]=nums[i];
nums[i]=temp;
break;
}
}

}
}

while(len<nums.length){
if(nums[len]==val){
break;
}
len++;
}

return len;
}

Still a long way to go. 加油!

Remove element 思路

采用双指针i j,j是快的。i指向新array的边界,如果j指向的不是val,则nums[i]=nums[j],然后i j均向前移动1。

如果j指向的是val 则只有j向前移动,直到j结束对原array的遍历。。。

我????

Roman to integer

不难,主要注意判断当前char所代表的数字应该+还是-

1.做一个hashmap把罗马字母和数值对应起来

注意map的key是char类型

后面循环取string里的字母也是使用charAt返回的是char!!!!

2.循环的时候

if(s.charAt(i)不是最后一个&&s.charAt(i)<s.charAt(i+1)){

​ val=val - s.charAt(i) // 如果后一个数比i的数大 就说明i是要减去的 比如IV=-1+5

}else{

​ val=val + s.charAt(i);

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class  {
public int romanToInt(String s) {
HashMap<Character, Integer> m = new HashMap<>();
m.put('I',1);
m.put('V',5);
m.put('X',10);
m.put('L',50);
m.put('C',100);
m.put('D',500);
m.put('M',1000);
int val =0;
if(s.length()==0){
return 0;
}
for(int i=0; i<s.length(); i++){

if( i<s.length()-1 && m.get(s.charAt(i))<m.get(s.charAt(i+1))){
val -=m.get(s.charAt(i));
}else {
val+=m.get(s.charAt(i));
}
// System.out.println("currChar: "+s.charAt(i));
// System.out.println("curr value: "+m.get(s.charAt(i)));
// System.out.println("val: "+val);

}

return val;
}
}