当前位置:网站首页>Leetcode question brushing record
Leetcode question brushing record
2022-07-29 06:36:00 【Flying Penguin I】
Catalog
1、 The following code results in ?22/7/27
2、 Judge whether the integer is palindrome 22/7/27
3、 Roman numeral to integer 22/7/28
1、 The following code results in ?22/7/27
2、 Judge whether the integer is palindrome 22/7/27
1、 The following code results in ?22/7/27
public class Test {
public static void main(String args[]) {
int x = -5;
int y = -12;
System.out.println(y % x);
}
}answer :x And y Between operations , When x,y When the symbols are the same, the symbols of modulo and remainder are also the same
When x,y Different symbols , Take the remainder and take the head , Take mold and tail , The remainder result symbol and x identical , Take mold and y identical
Arrays.asList() The list returned by the method is Arrays.ArrayList Type of , Not at all java.util.ArrayList;
2、 Judge whether the integer is palindrome 22/7/27
My plan :
public static boolean isPalindrome(int x) { String s = String.valueOf(x); StringBuilder sb = new StringBuilder(); char[] chars = s.toCharArray(); for (int i = 0; i < chars.length; i++) { sb.append(chars[i]); } if (sb.reverse().toString().equals(s)){ return true; } return false; }The problem is : Memory consumption is too high , The code is too complicated , When the number is too large, it will cause overflow .
Solution :
/** * There is no need to reverse all integers before judging , You can take the general for comparison , Take the last few digits of the integer and compare with the first few digits */ public static boolean isPalindrome(int x) { // First, the result must not be the enumeration of palindromes /** * Less than 0 Signed is not palindrome * At the end is 0 Non - 0 Integers */ if (x < 0 || (x % 10 == 0 && x != 0)){ return false; } // Trailing mantissa int revertedNmuber = 0; // When x Less than or equal to revertedNmuber It indicates that the mantissa extracted is more than half , Only need to compare half while ( x > revertedNmuber){ //x%10 Is to take the last digit , The first cycle takes the last number , The second cycle is to multiply the previous mantissa by 10 Plus the current mantissa revertedNmuber = revertedNmuber * 10 + x % 10; x /= 10; } // If x=revertedNmuber It means that the same before and after is palindrome // If x=revertedNmuber/10 It means that all the other digits are equal except the middle one 121 1=21/10 Is palindrome return x == revertedNmuber || x == revertedNmuber/10; }
summary : The general thinking of finding palindromes is to reverse all the numbers and then judge whether the number string after reverse order is compared with the original number string , Although it is easy to understand , But the method is more cumbersome , Wasting time and space is not worth it , And by improving the method , Only extract the value of half the string of integers for comparison ,12321, Take only 12 And mantissa 12 Compare , Save space , Improve system performance .
3、 Roman numeral to integer 22/7/28
The main idea : Store symbols and corresponding values in Map Collection , Traverse the input string , If the characters on the left are smaller than those on the right, subtract them , If the left side is larger than the right side, add ,
for example XIV It can be regarded as X−I+V=10−1+5=14
public static int romanToInt(String s) {
//s = I 1, V 5 , X 10, L 50,C 100,D 500, M 1000.
Map<Character,Integer> sLists = new HashMap<>();
sLists.put('I',1);
sLists.put('V',5);
sLists.put('X',10);
sLists.put('L',50);
sLists.put('C',100);
sLists.put('D',500);
sLists.put('M',1000);
int sum = 0;
for (int i = 0; i < s.length(); i++) {
// Get the value corresponding to the current string
int value = sLists.get(s.charAt(i));
// The judgment of less than here is to prevent i+1 overflow , When i=s.length() - 1 Explain to the last element , Can no longer +1 operation
if (i < s.length() - 1 && value < sLists.get(s.charAt(i + 1)))
sum -= value;
else {
sum += value;
}
}
return sum;
}
}summary : Preventing loop overflow can be judged with the last element , If it is less than the last element, continue to execute .
边栏推荐
- day14_ Unit test & Date common class & String common class
- day15_泛型
- 虹科教您 | 想进入TSN领域?虹科教您如何搭建TSN测试系统
- Maya aces workflow configuration (Arnold and redshift map configuration specification - restore the correct effect of the map under the SP aces process) PS restore the rendered map under the aces proc
- Multiple IO usage
- 虹科方案 | 在数字化的变电站中低成本实现无缝集成的独特解决方案
- FPGA里两个数的大小直接进行比较就可以吗?
- On defect description style
- 虹科分享 | FPGA 实现的直通与存储转发切换延迟
- Vivado IP核之RAM Block Memery Generator
猜你喜欢
![Self study understanding of [chain forward star]](/img/b4/7f66026a482540bf27f088c321a840.png)
Self study understanding of [chain forward star]

What are the advantages of software testing? See how much you know

Personal views on time complexity

day09_ Static & Final & code block & abstract class & Interface & internal class

day15_泛型

通过os-shell引发的mysql拿shell的思考

虹科Automation softPLC | 虹科KPA MoDK运行环境与搭建步骤(3)——MoDK例程测试
![[leetcode skimming] array 1 - double pointer](/img/c3/a671395e20fad58f1c7f6abc6e1a39.png)
[leetcode skimming] array 1 - double pointer

虹科教您 | 想进入TSN领域?虹科教您如何搭建TSN测试系统

Circular linked list and bidirectional linked list
随机推荐
Design and simulation code of 4-bit subtracter based on FPGA
虹科分享 | 为什么说EtherCAT是提高控制系统性能的最佳解决方案?
四、 局域网和城域网
Design of IIR filter based on FPGA
Merkle Tree 存在性功能第一次修改
Those vulnerability attacks on app
PDO的使用
网站服务器80,443端口一直被恶意攻击怎么办?
day15_ generic paradigm
Vivado IP核之浮点数乘除法 Floating-point
右值引用和移动构造
2022 summer second day information competition learning achievement sharing 1
Plugin location in mavan
虹科案例 | PAC:一种整合了softPLC控制逻辑、HMI和其他服务功能的集成控制解决方案
day14_单元测试&日期常用类&字符串常用类
2022 summer second day information competition learning achievement sharing 2
虹科分享 | 如何测试与验证复杂的FPGA设计(1)——面向实体或块的仿真
网络安全学习(二)
Vivado IP核之浮点数加减法 Floating-point
7110 digital trend 2 solution