Java窗体接收字符的方法_Java经典算法:最小窗口子字符串

给定一个字符串S和一个字符串T,找到S中的最小窗口,其中将包含T中所有字符的复杂度为O(n)。

例如,S =“ ADOBECODEBANC”,T =“ ABC”,最小窗口为“ BANC”。

f88835f45ed1eeca1b1f5c66fd90e1d7.png

Java解决方案

publicString minWindow(String s, String t) {

HashMap goal = new HashMap<>();

intgoalSize = t.length();

intminLen = Integer.MAX_VALUE;

String result = "";

//target dictionary

for(intk=0; k

goal.put(t.charAt(k), goal.getOrDefault(t.charAt(k), 0)+1);

}

inti=0;

inttotal=0;

HashMap map = new HashMap<>();

for(intj=0; j

charc = s.charAt(j);

if(!goal.containsKey(c)){

continue;

}

//if c is a target character in the goal, and count is < goal, increase the total

intcount = map.getOrDefault(c, 0);

if(count

total++;

}

map.put(c, count+1);

//when total reaches the goal, trim from left until no more chars can be trimmed.

if(total==goalSize){

while(!goal.containsKey(s.charAt(i)) || map.get(s.charAt(i))>goal.get(s.charAt(i))){

charpc = s.charAt(i);

if(goal.containsKey(pc) && map.get(pc)>goal.get(pc)){

map.put(pc, map.get(pc)-1);

}

i++;

}

if(minLen>j-i+1){

minLen = j-i+1;

result = s.substring(i, j+1);

}

}

}

returnresult;}

最后,开发这么多年我也总结了一套学习Java的资料与面试题,如果你在技术上面想提升自己的话,可以关注我,私信发送领取资料或者在评论区留下自己的联系方式,有时间记得帮我点下转发让跟多的人看到哦。

0a2d6428d06e6137858869dffb2911b5.png

735145e196672a2fd634ba08d6548f63.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Java窗体显示MySQL数据,可以使用Java Swing提供的JTable组件来实现。以下是实现步骤: 1. 导入MySQL数据库驱动包 在项目中引入MySQL数据库的驱动包,可以使用JDBC连接MySQL数据库。 2. 连接MySQL数据库 使用JDBC连接MySQL数据库,创建一个Connection对象。 3. 查询MySQL数据库数据 使用JDBC执行SQL语句,查询需要显示的数据,并将结果保存在ResultSet对象中。 4. 将查询结果转换为二维数组 遍历ResultSet对象,将查询结果转换为一个二维数组。 5. 创建JTable组件 使用JTable组件显示查询结果,创建一个JTable对象并将二维数组作为参数传入。 6. 将JTable组件添加到窗体中 使用Java Swing提供的容器组件,如JFrame、JPanel等,将JTable组件添加到窗体中。 以下是示例代码: ```java import java.sql.*; import javax.swing.*; import java.awt.*; public class DisplayTable extends JFrame { private JTable table; public DisplayTable() { setTitle("Display Table"); setSize(400, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 连接MySQL数据库 try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password"); // 查询MySQL数据库数据 Statement stmt = con.createStatement(); String query = "SELECT * FROM mytable"; ResultSet rs = stmt.executeQuery(query); // 将查询结果转换为二维数组 int rowCount = 0; while (rs.next()) { rowCount++; } rs.beforeFirst(); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Object[][] data = new Object[rowCount][columnCount]; int i = 0; while (rs.next()) { for (int j = 0; j < columnCount; j++) { data[i][j] = rs.getObject(j + 1); } i++; } // 创建JTable组件 table = new JTable(data, getColumnNames(rsmd)); // 将JTable组件添加到窗体中 JScrollPane scrollPane = new JScrollPane(table); getContentPane().add(scrollPane, BorderLayout.CENTER); // 关闭MySQL数据库连接 rs.close(); stmt.close(); con.close(); } catch (Exception ex) { ex.printStackTrace(); } } // 获取查询结果的列名 private String[] getColumnNames(ResultSetMetaData rsmd) throws SQLException { int columnCount = rsmd.getColumnCount(); String[] columnNames = new String[columnCount]; for (int i = 1; i <= columnCount; i++) { columnNames[i - 1] = rsmd.getColumnName(i); } return columnNames; } public static void main(String[] args) { DisplayTable frame = new DisplayTable(); frame.setVisible(true); } } ``` 在以上示例代码中,需要将“mydatabase”和“mytable”替换为实际使用的数据库和表名,将“root”和“password”替换为实际的MySQL数据库用户名和密码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值