Skip to content

Commit d52f12d

Browse files
committed
update
1 parent d1aef5b commit d52f12d

File tree

10 files changed

+209
-9
lines changed

10 files changed

+209
-9
lines changed
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
package problem45;
22

3-
import java.util.LinkedList;
3+
import java.util.ArrayList;
44

5-
/**0,1,2,.....,n-1这n个数字排成一个圆圈,从数字0开始每次删除圆圈内的第m个数字,求最后剩下的那个数字。
5+
/**
6+
* 0,1,2,.....,n-1这n个数字排成一个圆圈,从数字0开始每次删除圆圈内的第m个数字,求最后剩下的那个数字。
67
* Created by bjyangrubing on 2016/8/25.
78
*/
8-
public class LastNumberInCircle
9-
{
10-
public int LastRemaining_Solution(int n, int m)
11-
{
12-
LinkedList list = new LinkedList();
13-
list.last
14-
}
9+
public class LastNumberInCircle {
10+
public int LastRemaining_Solution(int n, int m) {
11+
if (n < 1 || m < 1)
12+
return -1;
13+
ArrayList<Integer> list = new ArrayList<Integer>();
14+
for (int i = 0; i < n; i++) {
15+
list.add(i);
16+
}
17+
18+
int begin = 0;
19+
int removeIndex = (m - 1) % n;
20+
21+
while (list.size() > 1) {
22+
list.remove((int) removeIndex);
23+
begin = removeIndex % list.size();
24+
removeIndex = (begin + m - 1) % list.size();
25+
}
26+
return list.get(0);
27+
28+
}
29+
1530

1631
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package problem46;
2+
3+
/**求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
4+
* Created by bjyangrubing on 2016/8/26.
5+
*/
6+
public class Accumulate
7+
{
8+
/**递归*/
9+
public int Sum_Solution(int n)
10+
{
11+
if (n == 1 || n == 0)
12+
return n;
13+
14+
return n + Sum_Solution(n - 1);
15+
}
16+
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package problem47;
2+
3+
/**写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
4+
* Created by bjyangrubing on 2016/8/26.
5+
*/
6+
public class AddTwoNumber
7+
{
8+
public int Add(int num1, int num2)
9+
{
10+
int sum, carry;
11+
12+
do
13+
{
14+
//先做异或,相当于各位相加不进位
15+
sum = (num1 ^ num2);
16+
//相当于进位
17+
carry = (num1 & num2) << 1;
18+
19+
num1 = sum;
20+
num2 = carry;
21+
}
22+
while (num2 != 0);
23+
24+
return num1;
25+
}
26+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package problem48;
2+
3+
/**不能被继承的类
4+
* Created by bjyangrubing on 2016/8/26.
5+
*/
6+
public final class FinalClass
7+
{
8+
private FinalClass()
9+
{
10+
}
11+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package problem49;
2+
3+
/**
4+
* 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0
5+
* Created by bjyangrubing on 2016/8/26.
6+
*/
7+
public class StrToInt
8+
{
9+
10+
public int strToInt(String str)
11+
{
12+
if (str == null || str.length() <= 0)
13+
return 0;
14+
char[] array = str.toCharArray();
15+
boolean positive = true;
16+
int beginIndex = 0;
17+
if (array[0] == '-')
18+
{
19+
positive = false;
20+
beginIndex += 1;
21+
}
22+
else if (array[0] == '+')
23+
{
24+
beginIndex += 1;
25+
}
26+
else if (!Character.isDigit(array[0]))
27+
{
28+
return 0;
29+
}
30+
int result = 0;
31+
for (int i = beginIndex; i < array.length; i++)
32+
{
33+
if (!Character.isDigit(array[i]))
34+
{
35+
return 0;
36+
}
37+
result *= 10;
38+
result -= (array[i] - '0');
39+
}
40+
41+
return positive ? -result : result;
42+
43+
}
44+
45+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package problem45;
2+
3+
import junit.framework.Assert;
4+
import org.junit.Test;
5+
6+
/**
7+
* Created by bjyangrubing on 2016/8/26.
8+
*/
9+
public class LastNumberInCircleTest
10+
{
11+
12+
@Test
13+
public void test()
14+
{
15+
LastNumberInCircle l = new LastNumberInCircle();
16+
17+
Assert.assertEquals(3, l.LastRemaining_Solution(5, 3));
18+
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package problem46;
2+
3+
import junit.framework.Assert;
4+
import org.junit.Test;
5+
6+
/**
7+
* Created by bjyangrubing on 2016/8/26.
8+
*/
9+
public class AccumulateTest
10+
{
11+
12+
@Test
13+
public void test()
14+
{
15+
Accumulate a = new Accumulate();
16+
17+
int sum = a.Sum_Solution(4);
18+
Assert.assertEquals(10, sum);
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package problem47;
2+
3+
import junit.framework.Assert;
4+
import org.junit.Test;
5+
6+
/**
7+
* Created by bjyangrubing on 2016/8/26.
8+
*/
9+
public class AddTwoNumberTest
10+
{
11+
@Test
12+
public void test()
13+
{
14+
AddTwoNumber a = new AddTwoNumber();
15+
16+
Assert.assertEquals(2, a.Add(1, 1));
17+
}
18+
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package problem48;
2+
3+
/**
4+
* Created by bjyangrubing on 2016/8/26.
5+
*/
6+
public class FinalClassTest
7+
{
8+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package problem49;
2+
3+
import junit.framework.Assert;
4+
import org.junit.Test;
5+
6+
/**
7+
* Created by bjyangrubing on 2016/8/26.
8+
*/
9+
public class StrToIntTest
10+
{
11+
@Test
12+
public void test()
13+
{
14+
StrToInt s = new StrToInt();
15+
Assert.assertEquals(123, s.strToInt("123"));
16+
Assert.assertEquals(-123, s.strToInt("-123"));
17+
Assert.assertEquals(123, s.strToInt("+123"));
18+
}
19+
}

0 commit comments

Comments
 (0)