Compare commits

...

2 commits

Author SHA1 Message Date
b22b0fa5e6 1015 2024-10-15 14:57:21 +08:00
69f4005e9c 0926 2024-09-26 08:11:03 +08:00
5 changed files with 146 additions and 1 deletions

43
src/JavaDoubao/XtoY.java Normal file
View file

@ -0,0 +1,43 @@
package JavaDoubao;
import java.util.ArrayList;
public class XtoY {
public static class Main {
public static int solution(int xPosition, int yPosition) {
// Please write your code here
int diff = Math.abs(yPosition -xPosition);
int gone = 1;
ArrayList<Integer> steps = new ArrayList<>();
steps.add(1);
int lastStep = 1;
while (gone < diff/2) {
lastStep += 1;
gone += lastStep;
steps.add(lastStep);
}
if (gone*2 == diff){
}
for (Integer steps2 : steps) {
System.out.printf("%d,",steps2);
}
System.out.println("Result");
return steps.size();
}
public static void main(String[] args) {
// You can add more test cases here
// System.out.println(solution(12, 6));
System.out.println(solution(34, 45));
System.out.println(solution(50, 30));
}
}
}

View file

@ -1,4 +1,4 @@
fun main() {
val t = Test977()
val t = Test12()
t.test()
}

49
src/Test12.kt Normal file
View file

@ -0,0 +1,49 @@
class Test12 {
class Solution {
val table = mapOf(
Pair(1000,"M"),
Pair(900,"CM"),
Pair(500,"D"),
Pair(400,"CD"),
Pair(100,"C"),
Pair(90,"XC"),
Pair(50,"L"),
Pair(40,"XL"),
Pair(10,"X"),
Pair(9,"IX"),
Pair(5,"V"),
Pair(4,"IV"),
Pair(1,"I")
)
val bases = arrayOf(1000,100,10,1)
fun intToRoman(num: Int): String {
var num = num
val result = StringBuilder()
bases.forEach { base ->
var b = num / base
if (b != 0) {
if (table.containsKey(b * base)) {
result.append(table[b * base])
} else {
if (b > 5) {
result.append(table[5 * base])
b -= 5
}
for (i in 0..<b) {
result.append(table[base])
}
}
num -= num / base * base
}
}
return result.toString()
}
}
fun test(){
println(Solution().intToRoman(3749))
println(Solution().intToRoman(58))
println(Solution().intToRoman(1994))
}
}

30
src/Test202.kt Normal file
View file

@ -0,0 +1,30 @@
class Test202 {
class Solution {
fun isHappy(n: Int): Boolean {
var cur = n
val appeared = mutableListOf(n)
while (cur != 1){
print("$cur->")
var newNum = 0
while (cur!= 0) {
val lastDigit = cur % 10
newNum += lastDigit * lastDigit
cur /= 10
}
cur = newNum
if (newNum in appeared){
return false
}else{
appeared.add(newNum)
}
}
return true
}
}
fun test(){
println(Solution().isHappy(2))
}
}

23
src/Test80.kt Normal file
View file

@ -0,0 +1,23 @@
class Test80 {
class Solution {
fun removeDuplicates(nums: IntArray): Int {
if (nums.size <= 2){
return nums.size
}
var slow = 1
for (fast in 2.. nums.lastIndex){
if (nums[fast] != nums[slow] || nums[slow] != nums[slow-1]) {
nums[++slow] = nums[fast]
}
}
return slow +1
}
}
fun test(){
println(Solution().removeDuplicates(intArrayOf(0,0,1,1,1,1,2,3,3)))
}
}